数学建模社区-数学中国

标题: [转帖]Hashtable简易数据库程序 [打印本页]

作者: god    时间: 2005-3-31 01:45
标题: [转帖]Hashtable简易数据库程序
import java.io.*;  
, M$ Y' v2 q1 j0 Ximport java.util.*;  ! S# Z: y( E6 [& \2 x# k! o7 A
class str14  ; J+ b( x+ w4 U7 \$ y
{  
( D, P6 e" ^3 RInputStreamReader stdin=new InputStreamReader(System.in);  
$ G0 i7 k6 E# R7 B7 F) D1 c( @BufferedReader bufin=new BufferedReader(stdin);  
  l" p8 i0 W  Q/ Z% c# F1 t8 o1 p% S4 Y& r$ p: j. v- q, ]/ {2 `9 c
public static void main(String args[]) throws IOException  ) H) W6 w. f/ W) F: Q7 ?8 W; }
{  * F$ N1 o8 {. A- E& h
new str14();//run the application  
: \, D7 T: d  l- Q! W}  
5 R4 `3 y  \- f& X! G$ Y
* N3 F4 A/ i( A' ~; p. fpublic str14() throws IOException//constructor  
- W) C' A& F6 H% s6 H# j{  $ g' S& g, x. [8 I! R, F% \9 _
Hashtable htable=new Hashtable(20,0.75F);  2 C1 s/ [& E3 W+ K- j1 p' m
dataBase(htable);  
$ s  q; {4 M$ q( j) `8 l$ c- H+ Q6 `}  ; p4 y) R: _4 X. s( a) M6 p' [
1 s' k+ X$ s- N: V' _! [2 x
public str14(Hashtable htable) throws IOException //override the constructor  
) R, |: v/ [3 B. P{  ; ^+ O5 d" t, e$ P7 l$ ^/ `
dataBase(htable);  + |  P4 R$ A* ~0 [' I# L
}  
6 c( [6 x: ?/ H# y. }) A
% L4 V4 h4 g/ _4 ?* w: ipublic void dataBase(Hashtable htable) throws IOException  
- k& n% r( @4 G) k{  + l3 e) ]' U$ a; e' }
int count=htable.size();//get the ammount of the data in htable  2 {( R# B, b8 e( \! V7 ?$ g+ ]
int value,id,num;  
3 {+ s6 N2 h0 m" OString key,enter,data;  
: R+ {9 G, N5 S7 fEnumeration keys=htable.keys();//get all the key in htable  
; F! x% T  t1 H/ R$ W2 y# GEnumeration elements=htable.elements();//get all the elements in htable  / W8 {# S3 R) i' g0 [% F
System.out.println("# U. Y4 j+ f" J  }0 q1 Q6 c( A
Hashtable 简易数据库程序' ^8 Y* C9 v  g+ q3 H9 U* o  ^# ~
");  
- o& K7 a( [( P2 ?0 r" ^" gSystem.out.println("(1)输入数据");  
" g6 Q' N0 w) _' ]  lSystem.out.println("(2)请除所有数据");  $ E5 `9 k- m) f) g) [
System.out.println("(3)显示单笔数据");  ) j" E. O. R9 Z0 g6 Y/ ~4 K/ O
System.out.println("(4)删除单笔数据");  
% [! {0 O& m1 y7 ~# \System.out.println("(5)显示所有数据");  # E6 J1 ~7 A  j! r! {, p6 K
System.out.println("(6)结束程序");  ; J2 J& ]) b5 ]! J2 E: l7 y
System.out.print("请输入您的选择:");  
$ u6 y$ a% C/ h( _+ jvalue=select();//call select(),return the function number  8 |: J1 F: v) j. z
switch(value)//the function  8 P, t' X# q( U% i7 K2 f, A
{  , H  l7 W9 K  E: r. k
case 1:  8 R$ |" H3 W4 o+ a
System.out.print("9 m: |( I2 ?9 d; R
请输入一笔数据:");//need data input  2 B$ }& X& i" W8 B! I2 Y& s4 b' l
data=bufin.readLine();  2 y: d8 s/ f3 w$ |
count++;  0 d4 Y/ d7 y. P' R* z: E( p, q
key=String.valueOf(count);  9 @! E5 Z5 z6 k( V
htable.put(key,data);//store it to the htable  2 g0 I. W6 d% J) C4 J. N# P& a' V
System.out.print("
+ H/ Y2 S1 J2 d5 H$ J输入完成,按任意键继续...");  
, C" d. @' {8 h8 |! j; E. b, ^$ @enter=bufin.readLine();  
# T- y4 `% U3 w+ B; `* w( ]new str14(htable);//reenter  # e' X+ R% y! [3 j
break;  9 V; u6 S% x( \/ Q. A
case 2://clear all data from the htable  
& P( \  H  x: @# h5 Y4 ahtable.clear();  3 ^! u: G9 T7 n, t
System.out.print("8 a$ j$ t& i) {* T. D& K) K$ X
已删除了所有数据... 按任意键继续...");  
" Y; x7 J/ H' i+ xenter=bufin.readLine();  3 Q- J% F: F$ Q+ \$ ~( S2 _& W
new str14(htable);//reenter  
8 m, i- B- f  w, l/ P: h- @break;    e& \% N4 S5 A0 I: m% B3 n
case 3:  
& R5 |' O9 q0 C7 Y; s1 r$ b/ ISystem.out.print("0 b' s+ _- B- v6 Z: c+ ~: P
请输入要显示的数据编号:");  5 G& Q8 w# L2 ~5 ]3 ^
id=getid(count);//call getid()  
- x: P0 Q+ G% Q* @; V4 T9 Hkey=String.valueOf(id);  
7 s1 j! m, ~+ N9 dObject select=htable.get(key);//fetch the data from the htable  
; P6 p; e9 Q, \: v. X4 }4 @data=select.toString();  . X, u2 g1 n5 v% s" B$ V
System.out.print("6 G/ l8 l6 b. u, i! n3 O) A( v6 b; j
编号"+" "+"内容");  ! \5 R0 k' o& H. _
System.out.println("0 w) q( H" D: r2 J* D
"+key+" "+data);//display the data  8 D+ ?0 I' O$ a7 P: x+ S2 |
System.out.print("! e# B7 W6 T. Y' l. {
按任意键继续...");  / E# K$ N5 {: q* c6 {6 d6 S& L
enter=bufin.readLine();  % N$ |* \3 i1 k
new str14(htable);//reenter  + D. t2 d/ k% _- I' w. ^0 ~8 I
break;  
5 [( @- m: s3 v9 b4 Ycase 4:  5 C( k& ^( ~& _1 ~( N! U
System.out.print("请输入要删除的数据编号:");  
/ y0 }9 W5 Z7 P% Yid=getid(count);  
5 q% K6 j- c$ m2 G, fkey=String.valueOf(id);  
1 a9 l$ g2 r5 m7 b1 M4 F- ^6 q* _htable.remove(key);//remove data  8 p/ a+ f' B# H% f( w
count--;  2 E4 l8 I; x* w4 E/ i  |$ r
num=count;  % l9 n" L0 e' m* J6 L
System.out.print("已删除了所选择的数据...按任意见继续...");  5 S- H0 m0 q# ~9 D+ p
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  9 o; u6 T2 X6 n9 l9 ~$ a
elements=htable.elements();//fetch all the data from htable  1 i  O1 {5 @2 c0 S3 a5 d
while(elements.hasMoreElements())  6 G( \8 P% R) x! [5 j" }% p
{  
2 Z: s. ^& s) p/ bkey=String.valueOf(num);//a new value  $ G/ ~- W+ c2 a) F! T
data=(String)elements.nextElement();//fetch content of the data  
3 i% T1 t; M; Y7 w0 E! Mhtable1.put(key,data);//store it to htable1  + ~' t( y+ f8 x6 d+ C- h+ C
num--;  
  N6 x1 i7 @' E% H}  6 A; ~) \! T6 B- _
htable.clear();  , y* Y% C' r6 a1 q6 X. [
enter=bufin.readLine();  
# L5 G% \& T+ _3 _- _! \new str14(htable1);  
4 s) @  m# q9 D3 X& r4 Fbreak;  
2 Y% Q$ L1 [9 y; q' g$ ]case 5:  : {' Z1 j- a0 y8 V' B8 `6 r
String[] sortkey=new String[count];//create a new sort array  
$ s5 K/ J* s8 u4 Y2 @' DString[] sortdata=new String[count];  * A8 c0 u+ R- }$ Y3 E7 Z' f
num=count;  + b$ d6 [" `2 R% T, g1 \* n- L. r
elements=htable.elements();  ) u5 C2 L& G8 Q% ~% f
keys=htable.keys();  ; S% K0 g0 v, l6 s
while(elements.hasMoreElements())  
0 t% q* C" e3 K3 D1 \, J{  
* j: a3 l+ l5 }% N1 I7 Ykey=(String)keys.nextElement();//fetch value of key  0 D" A6 I1 U$ z3 q7 l% ?& c+ ?
data=(String)elements.nextElement();//fetch value of data  
7 j4 I6 I$ ^$ l: O6 \+ \: Y! psortkey[num-1]=key;//store the value of key to sortkey array  
9 q# }; V0 [. \7 H3 F1 D8 W. Msortdata[num-1]=data;//store the value of data to sortdata array  
) n4 i9 L4 u, o) Q+ R7 d- j( b  ], Onum--;  & M9 ^3 K$ f# g( C+ ~8 t! U4 N
}  1 w% \6 X1 ~6 j! u5 L
System.out.println("5 m+ f7 g- E" u
编号"+" "+"内容");  
* D8 Y$ l. P2 Y- ~; H7 ~for(int i=0;i<count;i++)//display all content after sorting  7 m- f/ i2 I" ?. }& C  w: A  B
System.out.println(" "+sortkey+" "+sortdata);  
3 K: I/ O& L; X3 H3 h" MSystem.out.print("  O9 x: V) j& ?' H
目前共有"+count+"笔数据");  
  o% A3 f+ I- tSystem.out.print("' j! p8 e$ ?) ^# J( J: [

* c$ ^1 J) u  ^6 R按任意键继续...");  / V/ u0 {0 J; b" j9 o  O
enter=bufin.readLine();  7 R6 s9 ]: j: ?, m( m2 B1 `
new str14(htable);  7 F" i. K5 w% s5 P6 T2 t
break;  
* v& z: M) W2 K+ l- H# kdefault:  
$ r# O, T5 Z3 S}  , E/ \- o# z8 t- J' u
}  : m2 F# Z; _) G" ^! J$ T

7 H8 o' S& v* X" Mpublic int select() throws IOException//method of getting a function selector  
6 s: X1 T# h6 s* o3 y{  
8 T( k2 E7 g# h: Z2 B( J2 n# i' ZString input;  3 `% q& R& C8 y9 _- D8 T- r) F3 |
int value=0;  " d$ Z2 `+ W: Z1 A8 `* f
input=bufin.readLine();//read a input from keyboard  - l  S9 d& f' V. H  E  W( b
try  9 z1 z: b4 u' }4 p0 y$ j
{  
7 L6 z; o/ r" p. f! F3 L* Qvalue=Integer.parseInt(input);//convert a string to a int value  " \  ]% l$ Q% S9 M
}catch(NumberFormatException e)//can’t vonverted  
8 }& j1 {. e  a% H0 P+ E{  " o9 H* C3 ?5 ~# O* a' u  o
System.out.print("请输入选项1~6:");  
) n! \4 E: }+ |! ^! h7 P% N# fvalue=select();  
9 j) [  F' |! D7 X. t) e}  
) o; L- |6 o5 r+ P3 \4 U& X% iif(value>6||value<1)//if exceed then print a message and reenter  
5 h0 u1 D' A0 K% X$ l{  
9 k( z0 y# k7 a( j7 G5 ^System.out.print("请输入选项1~6:");  
( v- I2 |2 m* N% ~; G* ?value=select();  ) b* v# D: O6 y  u' h& e( K
}  / {! Y# j( \1 \# D
return value;//return a value  
- [  D( g+ P) i. B/ M" k}  
6 \: R' K) G, I) ~) S
& L, z; E" U7 h/ y. L! r+ g9 Kpublic int getid(int count)throws IOException//a method of return the number of data  / s# p# O5 k$ V
{  6 S+ w& ^  v4 ]" `
String input;  : E) v5 [. A- \9 G: O/ N1 S
int value=0;  
. P2 d4 l  T! a2 q% r) K! n" minput=bufin.readLine();//read a user input string from keyboard  
3 y  O( \& c/ F) ]try  ' s/ b) u2 m$ C1 `  J& L- P! q
{  
! E, A+ Z) `: u0 Ovalue=Integer.parseInt(input);//convert the string to a int  ! Q7 L3 F* q% a, B7 y. ]
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
( _3 Z1 H) M2 k! E) N1 X{    |/ B* _1 p' g/ N/ ^( v
System.out.print("请输入数据编号:");  9 A1 j: R$ y* B" X0 n( S
value=getid(count);  0 h8 s# e+ q) f9 ]3 u& V
}  
. L3 p+ }1 o% aif(value>count)//the input value is out of bound  
5 Q& G/ ~0 ^! H4 ]3 R5 a{  6 J9 _  S+ U/ V% O* D
System.out.print("无此编号的数据,请重新输入:");  0 C- O/ ^! n6 }1 W  l
getid(count);  
) ^3 A. k& U2 E  _}  
; }* m& u/ w! W# A% ]6 ?2 U# }; Zreturn value;//return a value  2 n# j( T9 ^. N6 p3 o
}  / f; r5 h# p1 o! Y, B, H
}




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5