数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:45
标题: [转帖]Hashtable简易数据库程序
import java.io.*;  ; s6 }4 e  }& g, ?2 N2 H: y
import java.util.*;  # N- i/ U5 @3 A( {2 X8 h) Q; M  D
class str14    N  J; F+ }, y! |/ N
{  2 v( g$ F$ L) `' ^( m1 t+ o
InputStreamReader stdin=new InputStreamReader(System.in);  
4 j6 q3 k9 `5 q7 Z0 H5 MBufferedReader bufin=new BufferedReader(stdin);  
8 d/ Y" R# U5 P6 _% p: s% ^' Q' x# |8 m5 @* Q# [
public static void main(String args[]) throws IOException  7 u! p3 A3 w6 R8 m% i! J/ X3 w) m
{  
/ t; q: K9 G: h7 i! j& v. w2 Onew str14();//run the application  
( n( Z- k" q( {3 k- p; h}  ) g0 Y4 C9 g+ n) n

9 ]$ b1 y8 W+ ypublic str14() throws IOException//constructor  
9 O8 I$ ~# V' U$ b. \& e. e{  
1 C7 f, m: ]5 S4 B8 [$ r( _. IHashtable htable=new Hashtable(20,0.75F);  
+ {4 M; i1 R9 y' o# F2 {* V) b8 VdataBase(htable);  
% A5 [4 B: }: L/ g' d5 C: a}  2 o6 e) R& c* E6 V! e! Q, y* Z
& p* C, q+ ~4 B* q* d$ p
public str14(Hashtable htable) throws IOException //override the constructor  
% w3 q) Y1 Q" _6 {7 b9 F. R0 Q{  " R& g/ r, z, |# K$ @9 U7 [
dataBase(htable);  
' Y0 S, @1 t/ y2 A) n}  
: S$ l" \+ g( H
7 S) `! Q# e) `* Q  @public void dataBase(Hashtable htable) throws IOException  
1 x- n& \1 Z: p$ q) ^0 a{  
. P2 u) ~/ D, F+ _; aint count=htable.size();//get the ammount of the data in htable  
! L. Y5 o/ G. ]$ qint value,id,num;  9 Q& h( G6 L2 j$ X2 n. B
String key,enter,data;  
# m- H' ]1 c7 k3 w" [  G) K! t; y; cEnumeration keys=htable.keys();//get all the key in htable  * q% S; s# F* |& T7 l
Enumeration elements=htable.elements();//get all the elements in htable  1 O1 P0 x4 _. G
System.out.println("
6 p4 k* w" Y! ?" w2 a0 T, {6 ~Hashtable 简易数据库程序& ?$ \" l; L- G) l8 L8 t5 l
");  / Z1 F: c5 W5 h" m: P
System.out.println("(1)输入数据");  $ s! E7 T7 Q2 r
System.out.println("(2)请除所有数据");  
& q& O8 {" p$ g' oSystem.out.println("(3)显示单笔数据");  
9 m& \; @0 u5 b) b1 fSystem.out.println("(4)删除单笔数据");  
6 P1 T  \1 ]2 ]0 y; xSystem.out.println("(5)显示所有数据");    h$ o- b9 a' A8 S
System.out.println("(6)结束程序");  6 z3 s; ?9 ]8 e) t
System.out.print("请输入您的选择:");  & k% r" y; E  ]
value=select();//call select(),return the function number  
3 F* M2 z/ A: cswitch(value)//the function  
9 A! [1 ~1 i  F: H' a8 E{  
- x" t# {1 s: [, F7 s: G3 Gcase 1:  
, F( j6 E; z/ {% `# bSystem.out.print("6 b( W+ @4 o# I' ^2 c
请输入一笔数据:");//need data input  ) {& T: }6 X2 t% D' U6 H& }9 n
data=bufin.readLine();  
: ^# g$ S9 c4 Scount++;  
. t2 u! z8 w" u) r6 n0 Vkey=String.valueOf(count);  2 v/ J; P, V+ R  |# d/ h
htable.put(key,data);//store it to the htable  ) g( P2 {; d1 ]8 i
System.out.print("$ B0 P+ ~! O0 }: Z4 |& c( U, `) f- l
输入完成,按任意键继续...");  
5 n7 K  b- b8 X6 ~/ Denter=bufin.readLine();  
1 \; d4 h# V9 D0 T3 ]/ Cnew str14(htable);//reenter  
; I/ z# y# \/ Z- Q1 ]break;  
! V4 \* \) y. Tcase 2://clear all data from the htable  9 U* }' G+ W" Q: _# [  F
htable.clear();  
! N, H% N; p! NSystem.out.print(": \, M9 [3 o; \% m; q! P" q
已删除了所有数据... 按任意键继续...");  " t/ r" m7 }/ ^, P
enter=bufin.readLine();  
. E6 k/ l* s" D/ Fnew str14(htable);//reenter  ' ~- Z8 n9 A& o+ a7 h* {9 R  @" q3 n; _
break;  
  [3 _7 q' n+ M" ]4 u- N( T5 f+ Pcase 3:  
& n5 r& y7 i* x3 z( ?. j5 mSystem.out.print("2 E3 ^8 ]4 v9 B
请输入要显示的数据编号:");  
+ Z8 u$ H% w6 lid=getid(count);//call getid()  * {( G+ e1 T4 N3 F
key=String.valueOf(id);  
7 E; T1 R5 l8 u3 j' ]Object select=htable.get(key);//fetch the data from the htable  5 h& ?' n+ C; L1 R7 {! B: k" d/ j: p
data=select.toString();  & s2 E' P9 q& C, _. |; ?
System.out.print("& u% @4 _/ P8 I- w, N  z1 y
编号"+" "+"内容");  9 g& l. y7 n  \8 c# e2 E
System.out.println("# A- c5 i, @; f
"+key+" "+data);//display the data  0 U5 z+ H( W& r
System.out.print("/ Y% s- ~8 \. R  ?' Q
按任意键继续...");  
4 V7 V- t% p; Q1 Lenter=bufin.readLine();  
/ r2 C8 q* i8 _( N# \( [$ D6 pnew str14(htable);//reenter  
8 k1 ]; n+ b" E/ c8 {2 dbreak;  
9 ~; I3 \. s; D; L2 G. {  Ncase 4:  4 e; K$ y% K9 C$ ~2 p. ^3 P
System.out.print("请输入要删除的数据编号:");  
; C  b/ M2 k, b4 A% zid=getid(count);  ) [2 [' _; M2 ]* V2 k+ j4 O
key=String.valueOf(id);  ( i7 n: G: `* z: F! k( r
htable.remove(key);//remove data    I! S4 w' E0 ]8 j: U0 }8 ~
count--;  
* r/ ~! P+ _" I* ~; Unum=count;  
) x2 n- F( g2 s  z+ {System.out.print("已删除了所选择的数据...按任意见继续...");  - w8 b7 Z( o% ~, Z
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  0 V* x- P9 @3 Z6 I( B6 w) I
elements=htable.elements();//fetch all the data from htable  
3 _$ N1 K+ {8 Ewhile(elements.hasMoreElements())  
+ C) ]" i# H$ N. j: v{  
( t3 v% T' J% Pkey=String.valueOf(num);//a new value  
) D+ c7 e% @6 @( h2 N( Gdata=(String)elements.nextElement();//fetch content of the data  % M% t  q" F4 @
htable1.put(key,data);//store it to htable1  8 D7 j, j0 s" z+ C. I* l9 ]: Q, b, \
num--;  
9 E9 T+ t; R# ?  u. W, G1 v}  
4 B9 j& p7 o% i8 T1 B9 whtable.clear();  ( p0 T& W# r, v: g# ^+ \9 u6 o
enter=bufin.readLine();  % b8 S3 J2 q* o! x8 L
new str14(htable1);  
8 Z  ^# C5 l' M  A- k. I4 z/ W1 n6 xbreak;  , ]. u( b' V  j: G* d' Z9 d  ^
case 5:  0 q, {, C: A5 |. F) Z
String[] sortkey=new String[count];//create a new sort array  
) E2 m9 P& v" P8 f% H( ~String[] sortdata=new String[count];  
( |! @+ V% F, ?# L/ Wnum=count;  9 K0 @# h" ]7 s+ N1 ]
elements=htable.elements();  : X- m# s9 F# t
keys=htable.keys();  2 q1 Y! ^9 e! _( R! O% x- \+ d
while(elements.hasMoreElements())  
6 o' B% g1 Y/ y( {* i{  , i* a1 ~  q+ G4 Z' S5 ]; o# I
key=(String)keys.nextElement();//fetch value of key  
6 C5 M0 E1 i8 k! ?data=(String)elements.nextElement();//fetch value of data  
3 q5 e/ ?6 x3 N! J# X9 fsortkey[num-1]=key;//store the value of key to sortkey array  - I9 k: y8 W0 O2 v; g
sortdata[num-1]=data;//store the value of data to sortdata array  
/ x- p) Q" H: ?) I' h* d+ Onum--;  
; X  F7 x7 l1 F( t) O" {}  3 w! a3 v7 g  w" p0 s
System.out.println("" @. w; H6 A& S7 }8 \
编号"+" "+"内容");  : V- z; J9 h7 }8 W
for(int i=0;i<count;i++)//display all content after sorting  * y% D$ ^0 B! D
System.out.println(" "+sortkey+" "+sortdata);  
7 k( M! w/ ?, a; v7 z- cSystem.out.print("
; n- n$ F* V4 h" {6 t( U目前共有"+count+"笔数据");  2 J! ]- }; c, V8 J
System.out.print("
' G' h/ [8 i' w% H
3 I8 K8 w+ ]" A* p: f) Z; B按任意键继续...");  % i, g" x9 J" m) u. _9 R
enter=bufin.readLine();  
: I3 L7 i, [& k0 V9 rnew str14(htable);  # n5 `4 `. _& @+ E/ h& w+ g7 n
break;  
- f" }* G. Q+ h- B1 m% K3 ndefault:  
) Z9 L  [% `' b' k  S}  
6 h2 y0 i2 ]! V  Y}  
( _( i( B! a. J! |1 l. }+ W4 }: K9 ^  z. Q/ d
public int select() throws IOException//method of getting a function selector  4 T) N: \1 M% ~# V4 o% D
{  
- Y# e% k4 n; V) ?2 g% WString input;  ; b# a" ^9 `: C0 S
int value=0;  
+ s% e; V" S% V' ginput=bufin.readLine();//read a input from keyboard  
5 K: B/ ]: |3 x) m1 Ftry  
) U! l- z  b: g& w; h  m! |+ ]{  . H% ?, [2 b+ H
value=Integer.parseInt(input);//convert a string to a int value  
+ I: H9 ^% z6 f7 [4 ^/ T}catch(NumberFormatException e)//can’t vonverted  , @5 b9 g, {- q1 J
{  * K  g6 P) n2 j% O: ]
System.out.print("请输入选项1~6:");  ; X! W6 p% q6 `+ C# q! G
value=select();  
9 h& Z% x0 J& Q' X}  
$ S# D6 o. q- @0 O' Kif(value>6||value<1)//if exceed then print a message and reenter  # g9 N' j2 a! d2 |0 `/ h
{  ) R5 w5 l; d! }" Q# c) C
System.out.print("请输入选项1~6:");  
/ [; G  N' R3 N( vvalue=select();  
& X' x* S7 \2 j' c}  . D2 q' p% [6 `  r% S
return value;//return a value  
( i( a. E1 W' ~- O}  * O* g" f# u( S- y

* O& n" d8 a4 z1 N4 Zpublic int getid(int count)throws IOException//a method of return the number of data  ; f3 z% g5 |# M
{  2 H6 p' F+ h& n9 r
String input;  * g: J# C! U" }5 O0 o; [
int value=0;  
& j9 G1 V/ @* }input=bufin.readLine();//read a user input string from keyboard  - Z! X: @0 z3 N- {0 _
try  
! k( V7 _" ~0 O/ `{    u$ d9 s' {0 A/ M
value=Integer.parseInt(input);//convert the string to a int  + |" }& ^: Z3 [! B9 I
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
+ T+ ?* W1 j  J0 v% }5 \+ G9 g8 h{  
  Y8 {! m$ {" q1 q9 C. R! I5 o4 TSystem.out.print("请输入数据编号:");  
+ e; L$ R: n/ d: bvalue=getid(count);  0 `& R. s3 ?/ A- Z
}  
6 A' X+ A) m$ Z; ^2 L+ pif(value>count)//the input value is out of bound  $ v1 S/ v& X3 _" B" d
{  
7 \0 G- ?! a7 y; J5 x0 KSystem.out.print("无此编号的数据,请重新输入:");  
5 C; G  F; i% v  p% Kgetid(count);  ) A! Y# _, l" U
}  
( A: o1 T7 D( o, ?0 ^7 Nreturn value;//return a value  2 }6 G" ]( f1 I/ ~2 R; M
}  
1 s: m  K8 o+ Z9 |}




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