import java.io.*; " P8 y) h, _6 u1 n, U ]
import java.util.*; 6 s0 u9 a- X; G$ Gclass str14 9 I* |5 b# {; Q9 I$ g{ - L& C, n) g8 G9 T2 ^" q) m
InputStreamReader stdin=new InputStreamReader(System.in); 5 ?1 Q6 A O5 j9 x3 I. g
BufferedReader bufin=new BufferedReader(stdin); 6 y( m0 a C- a- I9 t; {6 l4 `( @3 c( V( c1 Z( c4 L" U
public static void main(String args[]) throws IOException * Z Y6 j1 v! V" s" B5 D, ]: v: k
{ - Z- P1 Z. \. @ [7 E! \( vnew str14();//run the application ! k" }2 y# k3 U7 s) L5 h
} 3 H# \; w# }2 C5 I9 w+ O, R, Z ; f4 ?2 `" L7 wpublic str14() throws IOException//constructor 9 [' w# A5 [1 ]* Q4 ?, Z. [
{ - o, y% o( ?: U/ Y* m+ B A
Hashtable htable=new Hashtable(20,0.75F); 6 t w; R- M( p0 M* C1 x
dataBase(htable); 3 H* b& I2 t7 [} - C2 }* }, m7 C; R1 t, q$ y7 a9 O
! w$ W! D0 z9 K5 m3 u
public str14(Hashtable htable) throws IOException //override the constructor ! f! P( N5 s( O9 m D; _5 J{ 8 s5 |# n/ h# P, d4 r/ x
dataBase(htable); ) M+ W- K& C+ a) j} # s, w- M- {/ L8 u& w7 i! ~* `/ Z: |: m4 `# h
public void dataBase(Hashtable htable) throws IOException ; I+ u/ D+ Y7 S2 s{ 0 r( |1 Y& b: V: J& v7 H: [' f7 Zint count=htable.size();//get the ammount of the data in htable # y7 C9 _# ~' [4 ~0 }+ E/ I0 L& v
int value,id,num; 9 Z+ x' R. ^' W
String key,enter,data; / G7 k+ ?/ ?$ D3 J [5 h! z9 o6 T* p
Enumeration keys=htable.keys();//get all the key in htable . P+ V7 t# B, e$ y9 C8 i) ^
Enumeration elements=htable.elements();//get all the elements in htable . U- L' Q {) a8 y
System.out.println("- V4 ~& J2 \" Q" }9 D
Hashtable 简易数据库程序3 c1 f) j8 x+ A9 r+ R4 k
"); ' R. E. v9 q0 u z% _# T+ i
System.out.println("(1)输入数据"); 3 m: J; |3 u+ q8 `2 Q+ P' H
System.out.println("(2)请除所有数据"); . Z' @2 T" b( u& ESystem.out.println("(3)显示单笔数据"); 3 Q3 @ a+ k; D9 a! Z# {- r
System.out.println("(4)删除单笔数据"); 1 M. X3 P6 N$ d% M d( o8 U( ~System.out.println("(5)显示所有数据"); " x9 B# d5 v8 P# J3 f5 C1 DSystem.out.println("(6)结束程序"); & L+ \8 u% B: l( o. bSystem.out.print("请输入您的选择:"); ; ?9 V" k) X( o: ?, @' d* t* svalue=select();//call select(),return the function number 7 t) V3 v n* X' [3 {' O2 e) dswitch(value)//the function % J, _9 v4 n* i+ c; d) q: n, Y! N# @
{ 7 P7 m" Y7 Y+ b9 ^/ N# `6 P
case 1: . C4 A. T% a3 l. bSystem.out.print(" C4 i: B1 _5 R) S q. i
请输入一笔数据:");//need data input 7 u8 t" s% e( G' x; t7 sdata=bufin.readLine(); 6 d" I( R" \ W; O; M* |7 z
count++; 9 u$ d4 L* C1 S3 ^* |key=String.valueOf(count); ( S: m7 j; M' U* whtable.put(key,data);//store it to the htable ) {# N u2 U7 R+ f6 @7 f, }
System.out.print("0 x, Q$ f' }& X
输入完成,按任意键继续..."); 6 B J7 B4 w1 E( ^5 ~ F4 v) m8 aenter=bufin.readLine(); w2 C1 l1 h$ j) C5 K% @
new str14(htable);//reenter ! k7 o2 g1 V0 E
break; 2 S0 s, O I" X5 ~ N+ g
case 2://clear all data from the htable ' q: @2 }/ P' F p m$ M; i, Vhtable.clear(); $ H8 B! c1 J4 U& \( Y
System.out.print("" Z5 o) ?! P) Y- G! I* _' ~% }+ K
已删除了所有数据... 按任意键继续..."); 0 T4 j, E1 ]9 H$ B. V
enter=bufin.readLine(); 2 E! A* ~. s1 {* }& d( O' }0 Snew str14(htable);//reenter . a7 ~$ Q3 u D; lbreak; 7 m3 a, P0 q, h" v% Hcase 3: % |4 R; Y. C3 s. A; x
System.out.print(" : J/ G. q: ~8 N% I) S- @请输入要显示的数据编号:"); 7 J' U, Q- @) D" R7 ]
id=getid(count);//call getid() $ Y& u2 T+ k+ `
key=String.valueOf(id); $ B( f3 H& p8 J' A, g, S0 LObject select=htable.get(key);//fetch the data from the htable $ t" G$ h8 R- v0 x/ q6 x
data=select.toString(); # K8 Q5 `3 q3 k9 I& i
System.out.print(" / H. J# `8 n7 ^编号"+" "+"内容"); 4 P @- S% _2 R" U/ j$ hSystem.out.println(" d+ _' P7 P9 R* u8 B
"+key+" "+data);//display the data . R% U7 }7 T, F
System.out.print(". P5 b5 u7 Y5 v) ], C: A
按任意键继续..."); % r9 {7 L9 w: q" j/ b; r2 O) Y1 ^+ d. Henter=bufin.readLine(); " z6 U+ s1 f5 \1 n" W/ p3 _5 X
new str14(htable);//reenter 0 b/ r- c( X1 L+ _; ~* Mbreak; ; q; b8 d7 g! T* q# w5 q; Z
case 4: # \ ]9 P6 X) ]* \) i, oSystem.out.print("请输入要删除的数据编号:"); + N0 R% c4 X( Y6 n9 o2 h0 U
id=getid(count); ' i& p( t; ~+ F# R: W! U5 L+ vkey=String.valueOf(id); ; E9 ^' u% d$ z0 I [% g. ?0 C. ihtable.remove(key);//remove data 6 V5 v T9 n6 xcount--; 5 A+ ~2 S& G- m& v) q* C
num=count; ' f+ ~# V- y; P8 P) |/ ~. c; }
System.out.print("已删除了所选择的数据...按任意见继续..."); 1 u* c9 R1 E) B: W/ P+ b
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 % X) I7 L1 i" r* e7 ~elements=htable.elements();//fetch all the data from htable 6 ]' k3 E& t' L- V( Q) Qwhile(elements.hasMoreElements()) 6 G+ C. N( p: K{ ; L! {4 G* Z: @
key=String.valueOf(num);//a new value 5 z9 K8 ?. Q( J- k7 h9 }, Fdata=(String)elements.nextElement();//fetch content of the data / |, b8 w( L5 b0 o( f; |
htable1.put(key,data);//store it to htable1 1 F* K; v0 Z6 Z$ k5 ?% z
num--; 4 t o1 R1 }+ ]+ ]} % m' |7 U) ?/ p: S0 S' ihtable.clear(); g1 K4 {, [* a' D& eenter=bufin.readLine(); / \! x% G7 a. k) ~new str14(htable1); 4 c& C, s! E% j
break; ) z+ u7 i* ?3 O& n+ \; k" X
case 5: 6 y. s; U% ~7 _' {% {. uString[] sortkey=new String[count];//create a new sort array ( l5 i0 o' B0 O# T+ n6 k
String[] sortdata=new String[count]; 8 i& p H9 d j" ?' L6 d6 o
num=count; 9 j8 i$ _) P( b' nelements=htable.elements(); ; f( ~3 U/ f% u
keys=htable.keys(); 3 J. U0 t0 ~7 `3 C
while(elements.hasMoreElements()) " J4 F) V) h4 h B
{ $ K) I0 z5 H+ B9 y( t
key=(String)keys.nextElement();//fetch value of key " p+ {; {- m% i5 k7 K) Xdata=(String)elements.nextElement();//fetch value of data ; @) M' M% H2 O& p, Y: Gsortkey[num-1]=key;//store the value of key to sortkey array 5 N! \! k& w0 ?9 Z$ Q6 x- \sortdata[num-1]=data;//store the value of data to sortdata array . h2 u2 Y7 n# D# j
num--; ( ~$ M* f$ X$ V* X. P} . f3 ?4 }2 }4 P. q( L' n( V8 p9 y( kSystem.out.println("; t" }8 l% R, K/ n
编号"+" "+"内容"); $ v0 b! D* g- s; q+ L0 Q# A3 lfor(int i=0;i<count;i++)//display all content after sorting " |# d7 n7 k. vSystem.out.println(" "+sortkey+" "+sortdata); & f! {+ a0 }+ ESystem.out.print(" % Z$ \6 i; p% ~ u目前共有"+count+"笔数据"); - L- x# T% ~7 eSystem.out.print(" / w0 z: m1 }7 v- a3 I3 O 3 }* z: X3 O* q% c按任意键继续..."); 6 W4 l! U2 h8 R9 r benter=bufin.readLine(); 3 b0 C8 c+ g# N' {
new str14(htable); 2 {) P2 Y3 }0 _2 [2 N) ybreak; . y" n) s3 v; v) e: Wdefault: # i& [) K0 k9 D! o: q8 \$ ?* @
} % ~; A* @+ b5 D* @: T8 w. R
} , U2 Z# Z9 p% a8 l I
# h2 x0 z# I) U) r6 Z5 Spublic int select() throws IOException//method of getting a function selector 2 K! }" V$ ?5 H: V" R! _0 l- B
{ b5 v; D; N/ Q! x. ?9 h1 \
String input; 3 e4 ^$ |9 z2 P6 ^int value=0; * f/ l- u8 L) a( D0 m5 l4 @8 _
input=bufin.readLine();//read a input from keyboard * P& R1 o# M$ h. r' P2 ]
try # X. ]# m- M5 w" Z
{ 0 |* o. u1 v5 }8 W3 ?9 {value=Integer.parseInt(input);//convert a string to a int value t6 F# o) h0 X}catch(NumberFormatException e)//can’t vonverted 9 H. i7 |) O0 y6 n7 j{ ! W4 \ T# f1 M+ I
System.out.print("请输入选项1~6:"); + X* L8 K( ~1 G) J( c6 K5 Evalue=select(); % I0 D9 j$ F4 f' N
} : m5 }$ N: S' J2 H0 H3 A/ T9 mif(value>6||value<1)//if exceed then print a message and reenter 8 ~$ E5 e8 m, Z3 @( a* \0 C! Z
{ ) q! l0 I$ ~8 l( r5 z
System.out.print("请输入选项1~6:"); 5 V+ G" H) J& }value=select(); 6 I4 B9 H6 I* g" U- z* Q
} ! q7 N5 u; G0 L2 K1 |
return value;//return a value , |2 I, x$ a- p2 F8 h' `6 s( m9 p} % ^" i/ B4 Q3 O3 e, y
; E' n( a4 {5 tpublic int getid(int count)throws IOException//a method of return the number of data 9 Y& j" s5 V/ L" V{ s7 \& Y- K1 k) O& P: `; m/ e
String input; 4 p$ V7 y0 ]( k! ]3 k$ A$ f3 @int value=0; % i) b a0 E! x
input=bufin.readLine();//read a user input string from keyboard ; H% z: t) h) [' v1 Vtry : p$ H7 I4 {" ]2 c' v
{ ! j" {# ]. I1 Rvalue=Integer.parseInt(input);//convert the string to a int 8 E6 E [3 G* G( T6 g4 f
}catch(NumberFormatException e)//if can’t convert to a integer then reenter 2 _6 l4 z# y v( X- X. x{ 5 _5 F6 b0 p. q8 _6 S- I( XSystem.out.print("请输入数据编号:"); . \) f$ o2 @9 c7 R# y# B
value=getid(count); 4 ]6 h9 U, u! t7 n7 _- t* D" u- V} ) q6 f: g2 x6 F$ L" u
if(value>count)//the input value is out of bound / u8 R# T8 n+ f- P& R{ / i; k+ G4 K Y) s: d1 e' J0 R
System.out.print("无此编号的数据,请重新输入:"); # y. D& K2 ~* f. v
getid(count); 0 q: [7 j: A9 a4 A h/ l
} 7 v& P6 |- v/ b. V+ I
return value;//return a value , Y2 s6 K* P; @8 y} ) n; L7 \' K7 s9 j. S+ @0 }) ?0 y. J
}