数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:45
标题: [转帖]Hashtable简易数据库程序
import java.io.*;  
: t' v% r( W, F( Rimport java.util.*;  ! `- P+ B% N6 h* G0 S9 ?
class str14  # m1 ^' ?% ^% v" U1 S
{  
. t& q1 O* ~" _/ x3 }) aInputStreamReader stdin=new InputStreamReader(System.in);  
; x! l8 ?( M5 ~, A! yBufferedReader bufin=new BufferedReader(stdin);  
. q4 c( f& B. p& A, ^# u' ~0 z2 p4 w5 H0 u
public static void main(String args[]) throws IOException  
5 Z, n! t9 O. g; p, n+ w{  
, v, m8 {5 l3 @new str14();//run the application  ; j4 b, o7 R$ e% n- ^
}  
. s7 E2 e9 }( t8 }% h1 k6 i/ q& i0 x
$ F. ]5 D6 v; z& C% hpublic str14() throws IOException//constructor  6 T2 ~, i1 w: f$ P2 a
{  & C5 g  b2 \, U& h4 M
Hashtable htable=new Hashtable(20,0.75F);  
7 C; t3 n! v) N( l1 Z& FdataBase(htable);    {& W" Y$ l: T( x
}  ' z/ p/ [) D6 y' k" {5 O

+ w; d: W+ I$ Z/ V* O( D1 k# Cpublic str14(Hashtable htable) throws IOException //override the constructor  5 d# w$ O7 H, e' F
{  , ~  A5 Q$ b1 M- I
dataBase(htable);  4 N/ ?9 B. b: Q6 v
}  5 d; Z- F7 P" g; t7 K
" c( C# J3 n9 N- W
public void dataBase(Hashtable htable) throws IOException  ) p# j8 E! Z/ H: x! Y
{  
5 ?& [! p. T9 `. p6 m  oint count=htable.size();//get the ammount of the data in htable  % c2 L* @3 m+ D+ A2 n
int value,id,num;  ( h5 }0 N' A* |+ b( H! A
String key,enter,data;  & [& B* L9 j0 s+ F- C: V
Enumeration keys=htable.keys();//get all the key in htable  
( n: K( }8 ~4 [- p, CEnumeration elements=htable.elements();//get all the elements in htable  4 w7 h0 L) T6 F# z
System.out.println("
# N$ W$ t/ P2 ^# g! T! qHashtable 简易数据库程序
9 l" a; [( q7 w# }+ p! u$ k");  ) u3 s0 s6 s) H7 y
System.out.println("(1)输入数据");  
0 Z9 m; f) a3 ~. Y' R* nSystem.out.println("(2)请除所有数据");  
( b( J. K% L$ Q% U7 TSystem.out.println("(3)显示单笔数据");  / Y, _$ ^. b  c& o7 R( K
System.out.println("(4)删除单笔数据");  
( T& ~# j' q, D8 rSystem.out.println("(5)显示所有数据");  6 U% \" f9 q% R9 a# f% g$ P
System.out.println("(6)结束程序");  
# f4 G: Z- t* K2 {System.out.print("请输入您的选择:");  . G$ s3 d* C) \4 I# h$ d$ g- H
value=select();//call select(),return the function number  % ~( G7 }% I6 j; p4 r; g
switch(value)//the function  3 N% h! q. A" ^1 V( y' c
{  
' T1 n0 E" F# S  k; s& [! g! X" h1 ocase 1:  , W9 z4 y, _3 Z
System.out.print("
% ~5 `* j" |- {% [请输入一笔数据:");//need data input  2 v- Q! l% t* C1 v8 t
data=bufin.readLine();  " ^& t) ], U5 [+ i* R, A
count++;  * n9 v* |' p$ N$ M1 \
key=String.valueOf(count);  
2 c$ W1 {  M: z2 h, shtable.put(key,data);//store it to the htable  ' z/ `2 h. K/ q# J. s/ m
System.out.print(") ?9 e. r4 m/ |+ O% ?$ U. z% t
输入完成,按任意键继续...");  
& W6 r3 A' x' xenter=bufin.readLine();    M% G* p- n& @# e1 Q
new str14(htable);//reenter  ( o- R# ]- t7 o2 O1 f
break;  
; D' n7 E% P1 ]case 2://clear all data from the htable  
  s0 n  ?0 {/ Mhtable.clear();  
% f8 j  _% |: {! Y, ISystem.out.print("  P; J! t: o7 |
已删除了所有数据... 按任意键继续...");  ! L. B) L; o9 `+ q; X: I: T
enter=bufin.readLine();  % p2 M- a" i$ A; X& ^5 t2 S: q
new str14(htable);//reenter  1 i; n8 g% s! K) }% g2 N/ V  a
break;  
- ]& @1 r- v# l; q9 Ccase 3:  
0 x1 ?/ ^4 i3 q9 B( y8 WSystem.out.print("
. x$ F( F* F, r, ?( C" F请输入要显示的数据编号:");  
4 b+ `  i; F+ _6 a! N# S" G' Sid=getid(count);//call getid()  
0 h+ g3 r  Y. H3 a. }% ~key=String.valueOf(id);  0 }' t9 ?1 ~/ S: y/ n9 ]
Object select=htable.get(key);//fetch the data from the htable  ; O8 q9 X4 e- t- L5 @
data=select.toString();  
; B- b8 C8 ~# ~+ {System.out.print(", y) _: C! X* F7 W/ F
编号"+" "+"内容");  - e" ^. e" f2 N: U/ z, @
System.out.println(". C7 ?- N9 |1 A+ Q
"+key+" "+data);//display the data  " u  f, U$ ^/ `- j+ F
System.out.print("3 X" J! v) A7 J
按任意键继续...");  ( o/ Q* i; Y  G, v+ r
enter=bufin.readLine();  
6 ?' G8 D# A2 g1 `/ ~& Snew str14(htable);//reenter  / q' }7 _4 }. P0 }
break;  
% j1 V  u/ u# ^+ p, p7 I  C9 O! B5 ccase 4:  
* Q. n  D. R) V' cSystem.out.print("请输入要删除的数据编号:");  
. t# G( b- ]& [3 bid=getid(count);  * a5 V0 F/ B+ P* q0 {
key=String.valueOf(id);  7 I$ s; y8 c; h6 g
htable.remove(key);//remove data  
5 G$ `7 o, R$ Ycount--;  
; |4 S) l* n* X  Inum=count;  - l4 b4 w4 i* x. ~( h  T
System.out.print("已删除了所选择的数据...按任意见继续...");  
: H: v" H! C3 y* W, rHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  5 q3 D+ M& J2 t# d
elements=htable.elements();//fetch all the data from htable  
- [+ f( W- N2 d5 v8 D& H; Fwhile(elements.hasMoreElements())  
( z) [0 Z+ c6 k( k) K* ^2 b{  
. y- z* T- n# b9 vkey=String.valueOf(num);//a new value  
+ T% d  V9 W& D6 O. _9 Bdata=(String)elements.nextElement();//fetch content of the data  
9 Q+ `4 Y7 E% t0 w' Vhtable1.put(key,data);//store it to htable1  
& u+ f0 h, X9 Vnum--;  2 o; a% i# T; f& B5 g2 X/ V! A
}  7 p1 M  p) b0 f
htable.clear();  
2 V* b! \  H5 e9 jenter=bufin.readLine();  
( o2 A. O- z4 Q( W# }new str14(htable1);  ' B- g+ L; y/ @8 p/ E
break;  4 m8 }4 \" j4 [) w9 L! u7 Q/ j: ?: C
case 5:  
# w0 y2 w- a# ~( wString[] sortkey=new String[count];//create a new sort array  
- ^0 W+ ~8 D4 t. I! Z3 KString[] sortdata=new String[count];  4 x8 c; ]1 d. Z& g- [
num=count;  6 g6 e- `3 t" |* t
elements=htable.elements();  
( z6 Z, G- c2 \keys=htable.keys();  
1 v$ g# A4 N0 u" {while(elements.hasMoreElements())  
. p3 O8 |. g% g& F# m0 E% }{  5 A/ F( e- E6 }" @! Y2 i. q
key=(String)keys.nextElement();//fetch value of key  
# ~6 r: b" z/ h/ Idata=(String)elements.nextElement();//fetch value of data  
: |# T# v3 S4 e2 {  n% t* B* csortkey[num-1]=key;//store the value of key to sortkey array  
1 k% R0 W( ~& T5 K; Isortdata[num-1]=data;//store the value of data to sortdata array  - s+ g" k  ]- C8 B+ W
num--;  4 U) j% a! x/ D. c* R; f
}  ' o; b+ K; g; V. `: X* F
System.out.println("
$ R  _4 h/ y$ g8 ]" i( e" }* C6 U: ^- `1 O编号"+" "+"内容");  
( H9 b& V5 q2 ^# x8 ]for(int i=0;i<count;i++)//display all content after sorting  
) L! F: J1 `; aSystem.out.println(" "+sortkey+" "+sortdata);  
% y5 |3 m8 |' ^" r+ r% _System.out.print("
8 ?. X3 _6 l9 O8 g! _; R" K目前共有"+count+"笔数据");  
& }, a: I& c' p5 g4 u1 _System.out.print("
2 ~+ G% [( f$ \' n, q. m) e
3 J* o) t, k3 P4 P, F按任意键继续...");  & c: y* g* ?$ T, Y6 |# ?) w
enter=bufin.readLine();  * n) j& \; H* K
new str14(htable);  
6 D4 O- |; |6 x; |6 O- Ybreak;  
  _9 l( C2 u# ~* xdefault:  2 y1 @( Q% R, L# m* i% A" |
}  
6 ^: X; I. s  f}  1 `2 d' M5 [: F' ?" i: ~+ S) \+ }
( k) m4 P% a, n. s9 W
public int select() throws IOException//method of getting a function selector  
& J2 d# P+ [8 t6 e; |4 p{  
& a2 H8 m3 T, h, BString input;  6 n8 O( V* }+ z2 ~  p+ D
int value=0;    L( p  f6 }4 G5 C
input=bufin.readLine();//read a input from keyboard  
& e  W4 P" k6 D$ M9 W2 {try  ; X% i: t3 a8 l/ x( L9 `  K" W
{  $ u6 d2 w1 i, i! @* e; c1 O, f
value=Integer.parseInt(input);//convert a string to a int value  " A/ e" |# ^( U: W
}catch(NumberFormatException e)//can’t vonverted  
$ O$ l1 p; W0 A( \5 g- }{  
! J+ n2 w% u: b0 wSystem.out.print("请输入选项1~6:");  
% ]  q7 i  e7 i! r( m- L$ L2 Svalue=select();  
+ o+ {( f, r- L! Z}  
& z* j4 W' c! f/ w2 x; @) r4 B6 f7 qif(value>6||value<1)//if exceed then print a message and reenter  ) w/ N. A0 M! C
{  
0 L. f# ]+ p( ySystem.out.print("请输入选项1~6:");  
" }7 j6 r$ g% u6 U2 C- m0 @) _9 g0 P0 ]2 rvalue=select();  3 J2 R& x2 k  ^
}  
6 Y1 `) G6 Q4 R  Preturn value;//return a value    |# _2 r3 v: p$ F6 ]7 z# Y7 O% E" Q
}  9 G  X* ]4 s3 g( \% Q* d

7 y- l4 L3 `& a" E% _public int getid(int count)throws IOException//a method of return the number of data  
/ z2 Q- h  k; _8 P3 z0 L5 i{  
# M  e7 ~2 p& Z) D1 c7 DString input;  
& ~* R8 w$ b! v/ e; q8 T: D& L/ Zint value=0;  ; }2 E9 I4 q1 v) I7 I, @6 C8 M6 ?
input=bufin.readLine();//read a user input string from keyboard  
2 y% {, A: g% A5 C1 y% T1 ~try  . R2 h8 M& T& |( X2 p0 t
{  ( v5 A; N- b, w( e# y/ U! j% n
value=Integer.parseInt(input);//convert the string to a int  
4 G( ]9 R( Y- e$ N# F}catch(NumberFormatException e)//if can’t convert to a integer then reenter    F! ?/ s! F7 ~% s+ T- L9 }/ y
{  
& e! D" b( \8 t4 G) A5 R, lSystem.out.print("请输入数据编号:");  5 I1 ~  f- V% g- f
value=getid(count);  
* U. J% o* W/ q' t. n6 V}  * ~6 y& b: m1 u2 F" I* b
if(value>count)//the input value is out of bound  / l* y, T  I8 u& n/ B
{  3 X$ Q3 m* C% f  m
System.out.print("无此编号的数据,请重新输入:");  ' ]8 a4 V. I6 O% Z0 ?
getid(count);  0 r* P) e* y% ?  ?) N  ~% z
}  5 H  [- x- _% {- A- \4 a4 R" U3 s2 P
return value;//return a value  ! c. D  f+ {  d1 r7 F: N
}  5 O; ?9 o/ ~8 N
}




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