标题: [转帖]Hashtable简易数据库程序 [打印本页] 作者: god 时间: 2005-3-31 01:45 标题: [转帖]Hashtable简易数据库程序 import java.io.*; 2 J6 T3 \2 U. Q% \& Fimport java.util.*; 7 F- J4 A7 t7 ~8 ~0 D! F% k" g! Uclass str14 : s* B8 }# }1 X- I' p( \
{ ( t# T( w. Y: BInputStreamReader stdin=new InputStreamReader(System.in); # L1 L' h& d; q+ d0 l: N0 MBufferedReader bufin=new BufferedReader(stdin); * z3 t' R5 L' k0 s3 N; |' B
- N- [- ~- J* c2 _+ M @* q
public static void main(String args[]) throws IOException 2 h8 }0 C; C" n# c# W3 B
{ [ V% I+ X, u' {7 Anew str14();//run the application 6 R" K8 y, {1 A' I0 {} 7 R8 a& p0 b0 s* ^9 {& e9 ]
& A/ ]5 |1 c" v. s6 ], C" Z
public str14() throws IOException//constructor + \! X: r3 G: t1 h& D{ " J0 U0 q3 l# a* B) C
Hashtable htable=new Hashtable(20,0.75F); ! Y0 p" m/ g: ?& LdataBase(htable); 3 L2 e/ V6 s& S7 l7 ^} 1 O1 y0 O* k- c4 y; R8 P8 x
9 K. S5 P( _# m6 A
public str14(Hashtable htable) throws IOException //override the constructor % \$ E9 N3 |0 y
{ 7 G+ v2 m5 r n+ k% j, ]2 p" C
dataBase(htable); 6 H. ^/ E7 ?) x
} ) V2 A9 t- O7 x# M5 N: \
5 q( m7 J9 P# H: Ppublic void dataBase(Hashtable htable) throws IOException ; L( @6 c3 ?" I! h8 a1 i3 f7 V- o5 M{ ) u7 V" X+ r+ \) S& C ^# ~8 W
int count=htable.size();//get the ammount of the data in htable - ?% T2 \6 N4 I
int value,id,num; / N/ C+ J, H8 e S
String key,enter,data; 6 H% l( l( }# ]1 Y; @- a
Enumeration keys=htable.keys();//get all the key in htable & i# o" u7 a0 c) ~6 L4 z
Enumeration elements=htable.elements();//get all the elements in htable 5 y, {* C9 w4 R# c
System.out.println(" 0 w3 J/ s& r* F- [9 _% F- xHashtable 简易数据库程序! ^' n, g7 v- V' W6 M
"); + a, c- U: ]5 m9 h1 HSystem.out.println("(1)输入数据"); 8 u) d' E& y6 m0 B
System.out.println("(2)请除所有数据"); , l5 [- K$ s: ]( M/ `6 }( xSystem.out.println("(3)显示单笔数据"); * z1 h6 m% i$ U! G* a j; Z6 RSystem.out.println("(4)删除单笔数据"); F. P M- q2 ~0 v' i1 sSystem.out.println("(5)显示所有数据"); 0 {# j7 {' j. n7 N
System.out.println("(6)结束程序"); 8 ]( q' E/ g- e0 W! y0 r T' C5 QSystem.out.print("请输入您的选择:"); , S/ K% b0 d z1 J9 O: t3 R
value=select();//call select(),return the function number $ V7 E; [* C% u0 {3 n- X, oswitch(value)//the function $ C, |" A! X" R( i7 z: G{ 8 I) P G+ M% T: ^" hcase 1: 9 `1 n* ]3 U& }; O1 C4 y2 pSystem.out.print(" / t7 H8 V7 p! p# n请输入一笔数据:");//need data input - P# K& I, |0 U# D5 q0 ~" x& x$ n% k6 Vdata=bufin.readLine(); 5 }) b9 [3 ^! f; Y! jcount++; 8 ]+ `2 w$ T2 K1 }! R
key=String.valueOf(count); ' _; e. C) T& t7 yhtable.put(key,data);//store it to the htable 3 \; O% @% P* ]' ^5 f
System.out.print("4 K) I" y5 q1 p9 m. P
输入完成,按任意键继续..."); ( h l# m; }" j* E! C+ ~( penter=bufin.readLine(); / O! v* |* e, ^0 z1 W }0 F8 ^
new str14(htable);//reenter 2 S: N" }) p' f5 p) }' ^1 K) ebreak; ! @* r3 Y, i. u" q+ C
case 2://clear all data from the htable ) e0 N% r. b# v3 N/ ^) g3 u. Ehtable.clear(); 9 I" A4 d+ G% _; ^' pSystem.out.print("* L9 O9 _( u" x% s1 J: \3 X
已删除了所有数据... 按任意键继续..."); - {+ x# [$ k5 j. k
enter=bufin.readLine(); & i! ?; f1 j. H3 knew str14(htable);//reenter ! T' u' o2 H& H' H0 J3 b. }
break; " v) y+ j$ E g6 e: Z! \case 3: 1 D2 \' E' R4 f5 {2 \5 e# gSystem.out.print(" ; ]9 h6 F% H0 x, {请输入要显示的数据编号:"); ' ]2 N- r' s2 p: L# Pid=getid(count);//call getid() , }/ D: ]# D; {6 \5 Z5 J' b# Qkey=String.valueOf(id); 2 J/ q' u3 f% {Object select=htable.get(key);//fetch the data from the htable $ ~2 r3 S4 G0 h y0 x; e/ w) ndata=select.toString(); # |: {8 S" r R Q- ASystem.out.print("3 i' L2 M( K* \7 E" q: i0 s
编号"+" "+"内容"); + ]/ W1 I; b0 N4 }3 u z& J
System.out.println(" l( b9 m+ p! b* x "+key+" "+data);//display the data ' P3 d: h, `2 i6 i1 G
System.out.print(" " n/ |0 A' U, W% U3 y9 i按任意键继续..."); ' u4 o8 f/ S' ^" W4 Y( I: o0 oenter=bufin.readLine(); 2 o4 K3 m. f1 Q7 {
new str14(htable);//reenter . [6 `. C1 [; b5 n7 Vbreak; + V( e" p: B' Q! y; @* A
case 4: % c( F7 L Q+ F# U8 i% {5 \6 c
System.out.print("请输入要删除的数据编号:"); 2 Z/ G, j& N2 l- ^id=getid(count); 4 Y$ q6 B$ B4 M! nkey=String.valueOf(id); ' d7 m, J; ~. t) k6 X* N4 h
htable.remove(key);//remove data 6 d# a c% o: c, B# w) u+ Ccount--; . J6 C' z4 o" N$ Q" d& x( q
num=count; - E: i* {1 {, G% N5 G3 B
System.out.print("已删除了所选择的数据...按任意见继续..."); $ p( S* M. C, o& h" _
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 ( m# G- w3 G9 e* s- I0 q T
elements=htable.elements();//fetch all the data from htable / J$ Q/ R( n! f9 T/ X
while(elements.hasMoreElements()) . r2 S; N* h5 l1 m{ ! a9 _, V: u1 d' Mkey=String.valueOf(num);//a new value ( Z" u1 f( V: l, W! Vdata=(String)elements.nextElement();//fetch content of the data % a0 e; x, H* `htable1.put(key,data);//store it to htable1 9 O7 P. X5 L' f: X& ?
num--; ) L2 e1 s* i4 @1 N& B) P} 1 H- J T! P; O6 _+ z' Z E
htable.clear(); 3 O: y9 K% @2 l
enter=bufin.readLine(); ) t- K$ d' `* c5 S
new str14(htable1); 6 t: o* J, w8 r- ]3 t% D5 Lbreak; ; `( a* {* r- d$ y3 p8 w
case 5: - |7 X6 u8 w8 L! i( J8 E0 X- Z3 tString[] sortkey=new String[count];//create a new sort array |! T+ x% P$ r6 O2 }( G$ pString[] sortdata=new String[count]; - ?: C2 v: z# m6 l6 b+ c2 ~/ `num=count; 5 ]1 ~9 x \9 Q8 ?. ]. C. y* V; b. i
elements=htable.elements(); ) e( v+ [- n+ t. p
keys=htable.keys(); ! Q, x, a" p s* `
while(elements.hasMoreElements()) ) O9 ]* p, h, u{ 8 h) s# U% Q4 L3 y' t4 ?% ykey=(String)keys.nextElement();//fetch value of key 4 O, _ ^4 ~# \8 H* C0 Wdata=(String)elements.nextElement();//fetch value of data R, | }( F; ?1 L+ d, _
sortkey[num-1]=key;//store the value of key to sortkey array ( m" K, o8 }7 x2 \
sortdata[num-1]=data;//store the value of data to sortdata array / B# c0 H6 F9 C3 i
num--; - N7 M2 b, n% G f} ; h* x, D7 {( f6 O2 `
System.out.println(" 2 N: }6 I) o4 v$ I: c编号"+" "+"内容"); * ~. R3 d: c: A/ g! R% B \0 E
for(int i=0;i<count;i++)//display all content after sorting 7 c4 ~4 p9 S1 U* U3 `System.out.println(" "+sortkey+" "+sortdata); & \1 G2 M" } p+ n8 R: a0 t3 ySystem.out.print(" 7 I2 e( ^1 k" Z$ v3 P) \目前共有"+count+"笔数据"); 9 j# F6 ^5 ~# r+ G- M: V7 J: W; b gSystem.out.print(" " N$ A: ]: @0 z3 O* j& r" B D H. A9 U* {
按任意键继续..."); p% e$ L' A. @3 _1 S( f8 a0 zenter=bufin.readLine(); 2 v9 U* Q6 A6 G8 }. ?+ S9 H
new str14(htable); ) m0 r+ `6 T# A# F, P8 k$ [break; ' G5 S, C/ {6 b: q8 d$ Vdefault: . K, ?0 t' Y; z6 E/ p} , M" @( R; z) d, v5 T# v: S
} 9 L2 V$ W9 ^- G, J! P4 p % D* @, ]+ Z" u( G! f$ S8 `public int select() throws IOException//method of getting a function selector n! }, c' w; B& C" {; ~) l: q
{ / n8 r% |9 k3 f! d% C
String input; 8 X+ j: P, ^5 l' o R, {( D yint value=0; & S% V- i. ]3 E8 ^2 H U, Vinput=bufin.readLine();//read a input from keyboard 4 v9 W! f" q4 j( b8 q& `- jtry $ c& u$ H# X6 I* i- k+ Y0 Y
{ 4 g, P& Y! L' o U" _( g0 \5 k! Qvalue=Integer.parseInt(input);//convert a string to a int value . G" g( B3 O$ n4 U6 F}catch(NumberFormatException e)//can’t vonverted 4 B: ^/ e$ e9 ^ u3 H
{ . p# U8 Q$ [/ C
System.out.print("请输入选项1~6:"); $ |6 u/ J% I7 C X( Wvalue=select(); + q- h* g# ]1 `' P% c- k9 g+ ^
} $ F) `# b! u$ O7 O, L
if(value>6||value<1)//if exceed then print a message and reenter ( g n2 {, g- M Y5 T6 U, F{ ( h1 M- f( w H2 `- d: Z
System.out.print("请输入选项1~6:"); ( J2 ~2 ^" T2 ?0 `0 kvalue=select(); 2 r' \# S+ k2 k- V) u
} 9 W# _% ~. U9 N( P, ], W4 N
return value;//return a value : |0 @' w y, I; U7 q& b} 8 ^8 D% U- k/ Z8 h
4 F$ O/ t3 r- @% Qpublic int getid(int count)throws IOException//a method of return the number of data * P, Z. j+ {; c0 S4 l
{ " I) T7 o) u" T4 k# i: }7 Z
String input; / e; [4 N0 |6 ?0 @* ~4 w$ Q' E
int value=0; - {3 n$ s8 T8 y4 m% z; v
input=bufin.readLine();//read a user input string from keyboard - L0 ]3 U. L ]3 {# M
try 5 M9 `7 Y0 N$ b2 G8 c; z{ ) L, Z" h8 o. e$ G4 w
value=Integer.parseInt(input);//convert the string to a int - O0 R$ O. [; A6 z}catch(NumberFormatException e)//if can’t convert to a integer then reenter 9 r# w8 F3 j- P7 x6 @& S( L{ 0 _* N: `7 |9 S+ a; }3 S
System.out.print("请输入数据编号:"); ; V$ f U. ^9 u" U9 Zvalue=getid(count); ! s. p3 N5 k& U! ]
} ' k8 W' y* H! ^6 P9 [- f# O( hif(value>count)//the input value is out of bound + D' ]- l _ w2 T0 K. `4 q
{ 1 |& l- C q0 e! v7 J4 N
System.out.print("无此编号的数据,请重新输入:"); " w" e9 V$ U6 S9 f: K
getid(count); & ?. W% v/ W% M/ X. h! u} + ]& X) v3 Y0 u( Ireturn value;//return a value 8 W1 b1 b/ z" V6 p! l} . J, x8 D' ?/ c3 G% M/ l
}