数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:45
标题: [转帖]Hashtable简易数据库程序
import java.io.*;  
3 |7 R' {& Y, _3 Wimport java.util.*;  1 Q" L  Y+ `# J8 u; j
class str14  
" W$ n5 l: h: q5 C{  3 v: z' M8 L3 B$ w" i/ H
InputStreamReader stdin=new InputStreamReader(System.in);  
" E& [- n5 f' t" w: {0 |/ @  ZBufferedReader bufin=new BufferedReader(stdin);  
# i* @3 D: d% g' e+ t% X6 T) b
. P" q$ e  ]  }3 v* c* V. ]public static void main(String args[]) throws IOException  " W9 `  R0 B$ ~; B
{  & h: l2 H3 b* y3 A, E0 y
new str14();//run the application  
- d' X2 y( W. T/ X1 D. ]' c}  ( Z5 z/ g* q7 _) k4 }% B
1 ?( u1 I" Q4 Z& }$ H
public str14() throws IOException//constructor  
( M% [% T, y9 s{  ( ?: C) [! w7 R3 x( U3 x
Hashtable htable=new Hashtable(20,0.75F);  . w  Q! k9 P# t; I( J
dataBase(htable);  3 l1 V( n# @) f: x, P; Y
}  9 B) E  e) z/ b: p2 L3 s- l9 u
7 W; s+ f# B) h) z$ x
public str14(Hashtable htable) throws IOException //override the constructor  
  q6 J4 F+ I( ]" N, N+ Y; H. ~{  
. I: D5 A' I) _' d# VdataBase(htable);  
4 c" O/ g! d1 [. `0 S/ Q0 V6 ~% [}  
0 a9 f5 Q& l- z. [. Z" V6 x! C, S; w: U# ~6 ]8 g: d
public void dataBase(Hashtable htable) throws IOException  % k9 @, Z$ |0 ]1 T( ]
{  
. Z5 y8 n+ k) \! I& kint count=htable.size();//get the ammount of the data in htable  * m% E* l# C) r  A1 ^
int value,id,num;  9 r5 o  b* c4 D2 e
String key,enter,data;  # _# z2 @& a$ F. C! L- G' R% \. t
Enumeration keys=htable.keys();//get all the key in htable  
, P) \- R) h+ s# q5 Q5 iEnumeration elements=htable.elements();//get all the elements in htable  & N3 v1 p! k8 f. y- y- q' ^3 A3 i
System.out.println("' O' e% C, ~; ?5 {9 W( @  _4 d
Hashtable 简易数据库程序. C& P1 o0 V$ t4 k
");  
' T1 T* M0 D  h: m6 D7 nSystem.out.println("(1)输入数据");  
! m( n, @, I6 y6 Y. x# lSystem.out.println("(2)请除所有数据");  - h2 x* i- {  ~) B, x. q& p7 K$ L
System.out.println("(3)显示单笔数据");  
% M4 M7 D% I. I9 _4 p& jSystem.out.println("(4)删除单笔数据");  % ]  b# C7 Y; B3 P$ A9 s
System.out.println("(5)显示所有数据");  ( Q$ Q' Y. a& M. W- N
System.out.println("(6)结束程序");  
7 h( k' A1 D% V& K/ q; p0 i) sSystem.out.print("请输入您的选择:");  
% Q0 I2 q, ?( [% n  H* P0 t( Hvalue=select();//call select(),return the function number  
" o0 R8 |( h2 L" L0 g/ E# U. B% pswitch(value)//the function  
2 C9 J/ O+ F( c+ |2 E) ~{  ( o" k! ]: K8 v7 j
case 1:  - K0 `- I& J) [# a' F3 {3 _7 E& i
System.out.print("
- U$ b4 V- c0 n0 N6 N/ [7 ^3 a请输入一笔数据:");//need data input  
- u' [. c0 R. l. i  m+ v: `3 Ndata=bufin.readLine();  
% i1 v: J4 R* b; a8 K. D5 |" Y. icount++;  
' c9 K! _9 T& m! v) }$ e$ xkey=String.valueOf(count);  / D% h7 }& }3 F
htable.put(key,data);//store it to the htable  
& y4 o% O' Q3 B% fSystem.out.print("0 V8 A9 S7 H  \; \$ @
输入完成,按任意键继续...");  
8 X8 Q" ~; z( R1 Genter=bufin.readLine();  - `% S# R: N& w1 v: \" Q  O
new str14(htable);//reenter  
9 t1 }9 U* i8 i% Wbreak;  
, \* E3 T) v: d' Qcase 2://clear all data from the htable  ( b9 `& _* Q# c- l" l
htable.clear();  
, [+ k4 O" k8 K2 s9 b' aSystem.out.print("
+ f& F1 c0 a: e- D. `: S已删除了所有数据... 按任意键继续...");  
" _5 @! G- t) eenter=bufin.readLine();  
0 P- {) T3 I! `) ~/ ^0 jnew str14(htable);//reenter  / ^3 Y* ]! D/ Z8 A/ h
break;  
: g6 a/ X  N( S6 zcase 3:  
$ M& D' i- \1 Y6 p. E4 OSystem.out.print("4 s: u. E- h& g6 T3 g
请输入要显示的数据编号:");  
# f- F# C8 c& i  q7 xid=getid(count);//call getid()  
$ f! P6 C3 ^" X; `) N( w$ N& Ekey=String.valueOf(id);  ) v9 T# M6 Q9 k- s3 @. |+ g
Object select=htable.get(key);//fetch the data from the htable  
/ N) x) x; B  {. [+ ]0 A7 Fdata=select.toString();  
1 G9 ]( Z6 X! \, U+ l8 O, cSystem.out.print("
$ [3 v6 Y! f2 |编号"+" "+"内容");  . |: v- w% }8 v' o( W' B  v# Q! f
System.out.println("
# O+ p6 ~: M! s* o2 N! X "+key+" "+data);//display the data  
5 S, W5 ?0 q* YSystem.out.print("
& j8 |$ h# Q- v' X按任意键继续...");  
6 k) T7 m% r. ~9 i) uenter=bufin.readLine();  2 B# t8 \7 O: H& b
new str14(htable);//reenter  
4 l5 V. w. `* V( g2 B1 ]* hbreak;  
- t3 u6 L7 w, W. U$ B; [case 4:  - z1 h7 |' R5 H" c
System.out.print("请输入要删除的数据编号:");  2 u1 x+ G) x" Q. V" V/ U8 f8 o# `
id=getid(count);  
; }2 Y) F4 I* [, T: p( t2 F$ Xkey=String.valueOf(id);  
4 o' m( ^7 R" _4 M& T- }htable.remove(key);//remove data  
! Y2 [! R- `, d* f' X5 K! i+ J& Mcount--;  
# V5 `2 \! [! j5 ~num=count;  $ E# Y! E- K- S! I- Z: \  D
System.out.print("已删除了所选择的数据...按任意见继续...");  1 a+ [' u, `  r8 @
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
6 a2 a) u1 x0 N6 Z" `2 i6 O5 _elements=htable.elements();//fetch all the data from htable  . H8 i4 A0 ?- W  q
while(elements.hasMoreElements())    \# w- Z. c& N- m4 O
{  4 O/ i2 `) h, @2 E: \+ Z! B3 b3 {
key=String.valueOf(num);//a new value  
6 A( j& _. {) M7 {% s2 l( Y1 kdata=(String)elements.nextElement();//fetch content of the data  
$ ^, t9 C# c  y7 U1 dhtable1.put(key,data);//store it to htable1  
8 T. o; {% N! J* |num--;  " X* x& K6 M. N) Q! u
}  / U$ R; L. }- T; u
htable.clear();  
% d+ T( W  F3 Xenter=bufin.readLine();  
/ {$ H8 q3 h; H' bnew str14(htable1);  
: [: H$ I4 v& vbreak;  - y5 L' }' ^, Q) t9 C5 I! C
case 5:  4 q) d% z" W# u2 p) d3 u
String[] sortkey=new String[count];//create a new sort array  
" L- G7 R0 b4 q1 {8 O- H" q$ F3 cString[] sortdata=new String[count];  
  w8 j3 N. Q+ z3 S* }1 \num=count;  * T! H. I) W! n# }( I/ m, ?7 }0 z' }
elements=htable.elements();  
; _, e) c) }3 V2 Y2 Y+ Fkeys=htable.keys();  
* @" J( p$ T/ M& C$ h' Y# F" hwhile(elements.hasMoreElements())  . m* E2 A( y" u) B2 s! Q% ^
{  ( t4 F- X6 ^" S/ x  I; }
key=(String)keys.nextElement();//fetch value of key  
% I& c% `% r% v5 v, R3 Z5 Edata=(String)elements.nextElement();//fetch value of data  4 [7 Z5 Y) m  G# b; k1 Y7 a- H5 j
sortkey[num-1]=key;//store the value of key to sortkey array  
: o& ~5 G6 f$ \/ L6 C, e& e5 z9 Usortdata[num-1]=data;//store the value of data to sortdata array  
8 i2 w' z, \5 K# Gnum--;  / }) ?2 {, t, D  p& r
}  
5 [1 C) W6 W+ U& aSystem.out.println("8 g: j' P/ }. d- M
编号"+" "+"内容");  
3 ]6 N; F1 f+ H" mfor(int i=0;i<count;i++)//display all content after sorting  
0 s  `2 T+ I8 ]- u( KSystem.out.println(" "+sortkey+" "+sortdata);  
; c- v! G& m# [: @' k0 qSystem.out.print("% T/ Z) l" x/ u/ v% H. u4 d9 P
目前共有"+count+"笔数据");  
" F; Y$ i- I0 d' e$ H- z3 m: `System.out.print(", D, l9 j3 C; ]$ i

8 ^7 Y7 S5 M: g: ]按任意键继续...");  9 Y, F7 |% x% T
enter=bufin.readLine();  % ^2 J6 S6 z( M
new str14(htable);  
' p# @% e& a$ b; e; P" S9 Jbreak;  6 C( V  e; U" l8 J
default:  " v; U5 z/ U0 R. ~5 @
}  
9 ~1 P, W* o# d' V5 h1 j}  
7 c5 g2 |2 n8 h. U
2 T4 q/ k, C: V4 M' I1 ?6 F# ^public int select() throws IOException//method of getting a function selector  
2 G+ q- }* U8 A* u8 x{  
5 @0 Q' `2 V, R8 g& hString input;  6 t; M' v& C! `1 k5 e) k" u
int value=0;  
* \+ s/ y- _2 ~5 Y7 Binput=bufin.readLine();//read a input from keyboard  5 |! u9 T" Y! S4 [
try  
2 A" K) z+ N/ f1 j' M% I7 r{  " e1 @8 `) o  A5 D% a8 }$ q; \, \
value=Integer.parseInt(input);//convert a string to a int value  
  W& ?1 Y+ O- u}catch(NumberFormatException e)//can’t vonverted  & A4 O0 S9 e" d4 \- k; L
{  . A& h7 m! d* y' N: g
System.out.print("请输入选项1~6:");  ! K1 _. ~" |. ~+ P* D
value=select();  8 L' @+ K# P0 Y/ c0 r/ k
}  
, k; H1 ^# ]: d# d/ iif(value>6||value<1)//if exceed then print a message and reenter  , w' j; w' e5 i/ ^
{  - h8 h$ I$ l1 e% y/ n5 C2 V) r
System.out.print("请输入选项1~6:");  
* W! S6 ?$ i0 Q* Kvalue=select();  
2 M* s1 T/ R8 I( I8 Z}  & m: K% w& {4 C# Y  D
return value;//return a value  4 J6 F9 F" z" L. V3 {4 b
}  
8 s: A7 ~7 p- T) L. s) y% v* h' H7 n% c5 R5 m" u2 v( j
public int getid(int count)throws IOException//a method of return the number of data  
5 D1 H! D( g. a& k3 I& F7 \{  . X' s9 f& e1 i9 s
String input;  
# P5 _$ D! k! I" q3 a, N; p/ R7 }* }int value=0;  
" \/ J2 ^  Z' m+ X4 D  ?input=bufin.readLine();//read a user input string from keyboard  0 a' Z3 i0 V! D9 T" j# P1 b5 T: ]
try  ( |" N7 g0 r+ @! J- t
{  
# i) p3 e9 N# J1 G8 Kvalue=Integer.parseInt(input);//convert the string to a int  
7 T9 N2 p6 h7 R6 J) B0 N; S" C. r4 d}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
9 u( B, X6 ]5 E{  - v# x* T; d8 Y1 u& _9 d% U
System.out.print("请输入数据编号:");  / ]* s4 _3 b1 _
value=getid(count);  5 a# F4 @1 ^5 ^) m' c; [5 e/ }  D
}  
) H1 Y& b/ P6 \if(value>count)//the input value is out of bound  
, B1 R5 `4 e# Z{  
2 H1 N  m9 G' x- ISystem.out.print("无此编号的数据,请重新输入:");  0 Y; A/ d6 p' i. s3 P; I* Q4 z
getid(count);  
5 l% V4 @" z0 l) {2 \& Q4 e}  * Z$ H" H/ _! K$ H# \
return value;//return a value  
5 _! G$ n' l* y: u, [% \* _}  ( L/ d$ H' W% j$ }* @
}




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