QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2758|回复: 0
打印 上一主题 下一主题

[转帖]Hashtable简易数据库程序

[复制链接]
字体大小: 正常 放大
god        

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |正序浏览
|招呼Ta 关注Ta
import java.io.*;  
+ i6 b5 l! @- T% limport java.util.*;  
( x$ m5 o5 `" E; x0 gclass str14  
: E6 m; `) g& ]# a{  
; W: K% Y1 W( {2 C* W% @+ d8 ?InputStreamReader stdin=new InputStreamReader(System.in);  
! o$ l8 ]/ D. K% @9 W7 K3 ^3 _( ]* dBufferedReader bufin=new BufferedReader(stdin);  
$ \( P" y) E: a" ]+ a! x  Q# T9 `& I! }; P9 W5 a* Y$ w8 d
public static void main(String args[]) throws IOException  , M2 p' l6 f) h( G: N, U
{  1 i: p* B6 k6 ?" f7 P
new str14();//run the application  
3 S, |+ w; S" r, x$ B9 M- j- O}  * R4 a2 z. E( L9 B& N; m  g
3 V# I: ~0 }0 ?( A8 W3 M& S
public str14() throws IOException//constructor  
; S! `# ~' r; s$ F# q{  
  J0 \& G$ g" WHashtable htable=new Hashtable(20,0.75F);  ' y, I( ~8 p, C, j% G3 C( S0 V7 k1 J) a
dataBase(htable);  : N" c  x& E; k3 N7 {, K$ T4 s
}  
. N0 t6 h- Z- M
5 H& U  v4 U: ypublic str14(Hashtable htable) throws IOException //override the constructor  
# ^4 C% W: e) b9 c{  5 g0 l: R* V( y% j& J' f
dataBase(htable);  
1 U0 p  e! \# A) z9 [$ R# K, q}  
/ {) L3 w' O; S" h% Z) U* j' U7 f* O( G* V2 w6 Q
public void dataBase(Hashtable htable) throws IOException  
: V+ m" R+ r9 P, h% O{  # M6 U/ h( p2 i3 h
int count=htable.size();//get the ammount of the data in htable  ! p. l2 Y. i& ?  V6 j" q
int value,id,num;  
& r% P4 n! K4 ~7 C4 H9 P: aString key,enter,data;  
" Q4 [8 a2 H8 s# S+ R) }* cEnumeration keys=htable.keys();//get all the key in htable  
  u) s0 _. f6 E0 |% i: |Enumeration elements=htable.elements();//get all the elements in htable  2 O" I. ?8 f6 a
System.out.println("7 b6 a) {7 I# a/ C& S/ s1 j
Hashtable 简易数据库程序+ Y$ w/ l9 Y, p& F# H: Q( H6 ^: k
");  
/ o0 D8 C. ]9 \7 bSystem.out.println("(1)输入数据");  & j! y+ Y5 w0 X; H8 N5 T
System.out.println("(2)请除所有数据");  
* S$ Y9 b/ U0 R: b6 Y% P7 BSystem.out.println("(3)显示单笔数据");  
) K8 A& ^  [* l, j0 HSystem.out.println("(4)删除单笔数据");  ' o. V8 B! x' J$ ]( o
System.out.println("(5)显示所有数据");  
# p& i8 X1 C; ASystem.out.println("(6)结束程序");  
" C" v; ?- f- K! OSystem.out.print("请输入您的选择:");  ; f4 Y' }9 g. P- G# x
value=select();//call select(),return the function number  % e- ~6 l1 w# Y
switch(value)//the function  ( h: M* o- m1 z" c" X& g3 x
{  
/ x1 _- N7 ^7 Z. B0 o6 Ucase 1:  
, S1 D2 y" n. z6 ~$ M. kSystem.out.print(". k6 R+ A$ V" [( N2 Z: x7 B0 B
请输入一笔数据:");//need data input  9 x. g% @* u' x- D+ R8 E
data=bufin.readLine();  
/ c9 @' S4 I6 U! M1 \/ [8 d+ Ycount++;  / }2 r  m! C* D- y: C7 H
key=String.valueOf(count);  ; Y8 n' O1 n0 g' S' l5 t
htable.put(key,data);//store it to the htable  
/ m0 Y4 F/ j3 E: w. NSystem.out.print("
' {4 ?7 r( h! X+ P, \# z输入完成,按任意键继续...");  
* p9 i% M3 T; h3 X4 ^enter=bufin.readLine();  
7 x. N+ M9 O5 Ynew str14(htable);//reenter  . E4 M3 j* w; s7 [" m+ M* u" t: j
break;  6 n( I  z- \3 o4 ^
case 2://clear all data from the htable  
8 W- M) i' ?- }! }3 Bhtable.clear();  
7 F& g  r8 Q; f' d% Q1 RSystem.out.print("2 k' }' p3 T; x; g1 P% n
已删除了所有数据... 按任意键继续...");  
* T6 R( a: n3 B& zenter=bufin.readLine();  $ a: n- ^; G9 `# M2 b
new str14(htable);//reenter  
% ^- z% V7 R+ Z6 F: @break;  ; b# o# g0 ?! i
case 3:  " a( ?( f! {7 G( C" m
System.out.print("
; Q# j; M5 l3 K1 C请输入要显示的数据编号:");  
' d+ }  O( q; k2 @id=getid(count);//call getid()  : D1 c4 }2 R( k* s( ]3 A
key=String.valueOf(id);  5 W1 y: ^, {- e6 R5 B/ y
Object select=htable.get(key);//fetch the data from the htable  
3 x! x' L; |9 _2 }2 d( \! D6 b% y) `& ]data=select.toString();  6 n2 y9 q% D. k8 U1 @% Y$ k2 `
System.out.print("
( x7 q$ P% ?- o编号"+" "+"内容");  
. ?6 e+ L; \) i, R7 cSystem.out.println("
$ m, u; M6 K  h6 ]1 X "+key+" "+data);//display the data  
, b2 X$ f% G) P- a! U/ mSystem.out.print(") V$ J5 U" s& O% @: W
按任意键继续...");  1 b* c3 r4 U3 E. Z
enter=bufin.readLine();  9 K$ `9 b1 I% [4 J5 [% m9 [
new str14(htable);//reenter  $ t4 b1 f+ ]' r  f
break;  : j8 _* t4 P& y: g. h
case 4:  ! H! J% {1 E! H# A
System.out.print("请输入要删除的数据编号:");  - m2 ^. W0 X1 L: W
id=getid(count);  * e) Q6 }5 W( D; @$ l5 l. k. ~( B
key=String.valueOf(id);  % x+ F. e& [) O7 f& f
htable.remove(key);//remove data  
! M) G- ?5 T5 F3 Ycount--;  
, G( e5 x# j8 _- B9 [num=count;  
0 t( g; v" `; C! }( h: ~/ CSystem.out.print("已删除了所选择的数据...按任意见继续...");  
  I3 f3 J9 X- N7 Z+ ?8 sHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
3 E! R6 a7 \+ `elements=htable.elements();//fetch all the data from htable  
& T; l2 J$ k* h# J0 D1 A; t! {3 Iwhile(elements.hasMoreElements())  
* M# V2 j  A" q6 h3 [{  - ~! m! {. y% y! q7 O  \; X
key=String.valueOf(num);//a new value  
9 r/ l! Z% I& S. \1 q. jdata=(String)elements.nextElement();//fetch content of the data  
' X+ }# _$ g6 e5 u3 l: ?htable1.put(key,data);//store it to htable1  
! ]! }) n+ u8 _num--;  
; {( o1 N0 M5 N/ y}  4 m; G2 G3 ~7 \- y# T
htable.clear();  
# t- |8 w- {7 H0 genter=bufin.readLine();  
$ S2 Z, t+ _5 E- d: Wnew str14(htable1);  
1 h1 o& J/ M! M; Z1 K) `, ubreak;  3 V% h; Q: T9 P% ^1 A+ ~$ d
case 5:  1 F' G% D- \/ U2 }# ?( n
String[] sortkey=new String[count];//create a new sort array  3 E/ q# M1 k! a9 t5 R8 K+ G
String[] sortdata=new String[count];  % y; K& S. Y, X9 S$ l
num=count;  2 N2 `" v0 l5 ?) E# ^, A
elements=htable.elements();  
5 j5 d. E& \- \" o6 Vkeys=htable.keys();  
3 x2 `; N! u2 V6 q$ `  Mwhile(elements.hasMoreElements())  
$ d4 B0 g! [: x# S: g% C4 n{  5 |/ \5 [  X  @: }$ |$ [/ m3 k
key=(String)keys.nextElement();//fetch value of key  
/ }: J8 }$ J" Bdata=(String)elements.nextElement();//fetch value of data  
# B  I8 t$ C0 n5 @sortkey[num-1]=key;//store the value of key to sortkey array  : x; \0 @" p* I' c# a6 Q
sortdata[num-1]=data;//store the value of data to sortdata array  0 ]3 n2 b7 `$ j- K$ l6 G+ e  j
num--;  ( c( ]+ L) ^; Z: ]. e6 E7 \
}  
( s; e* P; H# t1 h9 B6 g. C- NSystem.out.println("7 q. A- f* F. U4 A
编号"+" "+"内容");  5 u5 E  z2 S0 v% q# ^! [0 b2 Q9 g. I  F) Q
for(int i=0;i<count;i++)//display all content after sorting  5 P# C/ O: d9 q
System.out.println(" "+sortkey+" "+sortdata);  
+ s1 K3 s9 Q% WSystem.out.print("0 ?9 C2 w, X3 `1 V+ n9 i! F/ {" i
目前共有"+count+"笔数据");  
" G" q' `5 u3 ?) c$ ?7 H& JSystem.out.print("
) ~9 E& @9 g# n* s
6 j( t- C' W! j3 I+ r3 X6 E按任意键继续...");  : m6 S& O8 L! q
enter=bufin.readLine();  - s. g: _7 g6 a
new str14(htable);  
" e& ]" h6 [; _$ Hbreak;  # B/ X7 g$ R/ L: R
default:  
6 A2 f: O& N" G& Y% R0 L}  
8 G1 ?2 Y5 n% _, q( |) Q: K7 t2 e}  
% M' s+ I7 z: H9 j5 u- A
! ], a1 g3 n$ o( A% y) U$ Z/ T  Jpublic int select() throws IOException//method of getting a function selector  
8 ?, b0 H" [0 S0 P{  ! K# w0 d' k/ M; r$ p
String input;  * I" V/ S7 u1 c( l# i& v4 ~
int value=0;  " U: q* C2 d2 B" h: l# K0 U
input=bufin.readLine();//read a input from keyboard  
0 z8 ]! x8 d2 m, g" x# J$ p4 Atry  + `7 G0 h% M& j4 V# I
{  8 m8 Z2 {8 |2 B2 d" T0 p+ V" m4 x
value=Integer.parseInt(input);//convert a string to a int value  + S0 Q( m% a7 f$ h" Z
}catch(NumberFormatException e)//can’t vonverted  * J+ t) ^5 g5 G% w9 a
{  
/ S7 P0 S7 m" T# NSystem.out.print("请输入选项1~6:");  7 z, b7 W4 ?3 W' E6 `
value=select();  2 |$ w# t8 N1 t7 r0 O1 m
}  
6 \# E, {: l- c' S& xif(value>6||value<1)//if exceed then print a message and reenter    E2 n2 `/ f& f
{  . Y! _* B4 h1 f% A% n
System.out.print("请输入选项1~6:");  
) m( ~3 f, C$ @! c) ~& u  vvalue=select();  6 M" B/ K$ d+ k
}  
- v: w# H9 Q& _: Treturn value;//return a value  
* p' K" ?+ F$ R! I) Z0 j0 n}  ; _/ f5 ^6 h  }9 q
) X8 I0 a& h! H* P) m3 F- E( O
public int getid(int count)throws IOException//a method of return the number of data  
/ ~- I7 D) D# `4 N9 ~{  ! J) u6 m0 K. l, l) Q$ t& Z9 Z
String input;  
& R1 D% _7 T& A' a7 |int value=0;  5 r0 [( O: I) z  h1 D
input=bufin.readLine();//read a user input string from keyboard  
, r/ l! E  j3 v1 y4 D0 D) Q' _0 `( ~try  
8 \2 H" }- V! o3 y9 f( O* Y; X{  ) s! z# a" t1 C7 a
value=Integer.parseInt(input);//convert the string to a int  
$ h+ A& ?0 x+ S, t/ h}catch(NumberFormatException e)//if can’t convert to a integer then reenter  & c9 y' I) x" q
{  
. c' T4 M" a6 q# ?: F* C& iSystem.out.print("请输入数据编号:");  ( y8 o; d) d9 C
value=getid(count);  
' T5 l2 ~2 R6 O/ S  [: j) Y}  2 [! R4 O" A5 l. ~
if(value>count)//the input value is out of bound  5 w+ G! P6 h7 l; m
{  9 W- ~( D# l. S% B6 z1 ~4 ^2 ?
System.out.print("无此编号的数据,请重新输入:");  3 b7 K( }2 m; u( ~1 P1 W2 F0 x/ ^6 Y
getid(count);  
3 S9 l3 v; ]0 h! v+ k}  ( j. t' d4 [1 X0 i
return value;//return a value  ) R7 }6 Y( {' L1 a9 ?' B
}  6 J9 ^. F; G$ a, B0 l: W; H
}
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-6-12 02:39 , Processed in 0.410673 second(s), 53 queries .

回顶部