QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2771|回复: 0
打印 上一主题 下一主题

[转帖]Hashtable简易数据库程序

[复制链接]
字体大小: 正常 放大
god        

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
0 q4 w3 ?# z2 H" iimport java.util.*;  
0 `% z: Q. S% ]class str14  
/ Z+ p5 N& |/ I: q' w{  
/ ]2 k- Q: R* R- t' Z" O- k5 OInputStreamReader stdin=new InputStreamReader(System.in);  * D0 v5 }1 C  x5 ]" B6 V( k
BufferedReader bufin=new BufferedReader(stdin);  
: {; k* c9 e7 u# r
. Z) d, y$ p' q, O3 M0 `public static void main(String args[]) throws IOException  
& h: B# f, f. d. j# x9 p1 K{  ; e+ B  Z* {7 I. S, X$ p& ^
new str14();//run the application  
) F$ a! a0 @2 b- c: [}  
- ~3 f! ]: y3 }' V! O5 c* [3 N1 T: ], ~: u. }( p  V) j
public str14() throws IOException//constructor  
$ s% ^8 b2 [9 Z# y{  
7 I' Y% x7 P7 H* Q8 ^. ~3 |. }Hashtable htable=new Hashtable(20,0.75F);  
* u0 P' R, [2 @, [! mdataBase(htable);  
* v. `: c; Q+ k: R- S}  
5 Q6 {* U. Q( ~2 ]; h) S, A, u: y" i0 P& j% g5 N
public str14(Hashtable htable) throws IOException //override the constructor  
5 I/ J) D5 F2 E, r8 k, W{  9 y6 n0 Z" B5 r7 A1 P
dataBase(htable);  
4 a3 r3 J1 j5 P4 A7 [}  
0 w% o4 \) V" t4 |  F* L3 M6 o  v9 l2 J% l/ B% N! M
public void dataBase(Hashtable htable) throws IOException  
6 h7 y# a3 o8 [{  ) G) g. Y6 K" m# d) m. S. w1 F+ V
int count=htable.size();//get the ammount of the data in htable  # k' \) t( N" g
int value,id,num;  
& W# I6 D" n  V* L; QString key,enter,data;  
$ ?3 P8 @/ J. O6 o0 lEnumeration keys=htable.keys();//get all the key in htable  
6 r# g; Q9 _$ N$ s9 p+ W5 c( t7 ~1 NEnumeration elements=htable.elements();//get all the elements in htable  7 K# Z; N4 M4 @6 R# ?* b
System.out.println("
3 q4 |) c# j, }+ ZHashtable 简易数据库程序: @. ~$ z/ f- O4 d6 g  e
");  
4 r2 ^/ K" X! C2 TSystem.out.println("(1)输入数据");  
2 I. r: w- X: q; j/ R0 [0 S+ DSystem.out.println("(2)请除所有数据");  0 k3 v/ I0 t5 e  P# ?; C
System.out.println("(3)显示单笔数据");  , T8 u  P# q4 `3 l9 p
System.out.println("(4)删除单笔数据");  / d' m# S" A$ y) [4 _2 C. [# W
System.out.println("(5)显示所有数据");  4 W! F' }, g) G% `% g
System.out.println("(6)结束程序");  ; s! y! P( ?% a
System.out.print("请输入您的选择:");  
; A8 e/ @. D/ _& v/ `  `value=select();//call select(),return the function number  , E- }/ S- k* i, O
switch(value)//the function  4 L0 j$ ^. r7 ~9 u$ `) p, f8 h
{  % n$ U9 m, c/ L& ]/ ^
case 1:  
' i3 u7 V: t* X- ^* P2 p7 z3 XSystem.out.print("
, h1 ^9 n/ M+ h% r4 M, N3 U1 ]& W请输入一笔数据:");//need data input  ) J0 c. `4 @8 i) T" k& B' r* k8 L
data=bufin.readLine();  1 s- B5 C( Z1 l5 w" N0 e% ?2 P
count++;  ; @/ e& [" E. ?, ~: q3 n% h& [; Q& p
key=String.valueOf(count);  
) B; i: r9 L  M4 Y9 I( ]9 xhtable.put(key,data);//store it to the htable  " M* Y# L/ z: m
System.out.print("
' h% }. J9 [' _! _2 v) f输入完成,按任意键继续...");  0 X. }" }) v' p( s1 s
enter=bufin.readLine();  
  S7 b8 C+ J% m8 p+ vnew str14(htable);//reenter  1 M# i8 P% V4 r# P6 j  Y/ Z
break;  
2 c- p* m- Q+ p# |7 f4 |% i( {% Ecase 2://clear all data from the htable  4 X4 V+ O/ U- U: K! P8 P- h4 J
htable.clear();  
' y( m, A# u$ H, ]7 [( JSystem.out.print("
1 b( k) |. m- m  @$ E已删除了所有数据... 按任意键继续...");  $ n$ `- d- d' q2 R% B
enter=bufin.readLine();  
8 q7 N- Y5 a! r3 T$ \  anew str14(htable);//reenter  
# K& j9 X2 P, B5 \/ d( z5 A" Ebreak;  
- w# |; Y6 v: O0 E9 F2 P0 G! @3 C3 pcase 3:    Y9 R% g0 Q$ g4 [6 y. _( y/ V
System.out.print("
4 Z9 K5 \4 S: Y- D) J5 |# B& p请输入要显示的数据编号:");  
3 i, ?, a+ |" ~$ s) kid=getid(count);//call getid()  8 `) s! ]7 A* c3 k# u9 a% H4 f
key=String.valueOf(id);  
" m1 o" `% H% X$ X% [5 GObject select=htable.get(key);//fetch the data from the htable  , }) q5 ?$ F+ {) c
data=select.toString();  
0 n9 _7 i* x. a9 F/ mSystem.out.print("
$ {: H$ {6 ^5 Q0 A2 s编号"+" "+"内容");  
$ A) w5 N4 u5 d& L/ MSystem.out.println("
0 A# F$ w/ o+ O6 V1 ^& O# S- R "+key+" "+data);//display the data  * m" V8 a, O6 j0 Z+ F4 Z7 u, U% ^+ ^; T
System.out.print("
9 Y# I6 Z7 a+ Z按任意键继续...");  
  q# P2 _6 z3 N. A4 Z0 c3 l) b8 Tenter=bufin.readLine();  5 g2 L8 z9 Q' Q/ C. {- G
new str14(htable);//reenter  
5 k5 U- P1 {, Y3 Nbreak;  
0 A* l* L. h* ?' n  B8 jcase 4:  
$ W! w3 A( {/ F# d2 Q7 s9 Q" y. X0 b1 E: LSystem.out.print("请输入要删除的数据编号:");  
# H. b1 t+ `3 b3 Y( U: {id=getid(count);  
5 S" {8 |# F+ j! B/ q! }$ Gkey=String.valueOf(id);  
$ `' T+ [0 p1 \$ I* v' Fhtable.remove(key);//remove data  
! A# m7 z8 L: A1 x5 B& z- ~3 Bcount--;  
4 s6 @  R2 t7 y6 ^num=count;  
! v' k2 }$ b1 N2 JSystem.out.print("已删除了所选择的数据...按任意见继续...");  ' ^) _% O9 `9 h0 i  I5 K% C: D5 C
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
- U" O8 X( U7 q. pelements=htable.elements();//fetch all the data from htable  + j9 p0 V0 q4 p
while(elements.hasMoreElements())  / E, P- ]+ g2 z$ w' m) i% f; L
{  - F% n0 U& }% q- d: q1 u- q1 R. A3 {8 I
key=String.valueOf(num);//a new value  9 O1 |/ W4 G# B2 F
data=(String)elements.nextElement();//fetch content of the data  
% a! Z" J& @) W5 w, @4 w7 }htable1.put(key,data);//store it to htable1  4 ~9 n9 a' s# O9 D, ]2 M' u! O
num--;  
; ~$ P( R, U# a& i6 \. X}  # H1 _- j' ~9 }
htable.clear();  
1 G6 k3 F+ Z7 x' Aenter=bufin.readLine();  + ~" F4 x% ?  `' B7 t+ F4 w# W& [: \
new str14(htable1);  ( P. e- j, i  p" o5 b
break;    b1 _5 ], W) o- _3 `
case 5:  ( X7 w" i" h8 Y+ e1 l% ~1 L
String[] sortkey=new String[count];//create a new sort array  
$ I  O0 Z+ m; U4 `/ g( h. R. P( KString[] sortdata=new String[count];  8 A& r$ u4 a2 `/ H$ A/ U9 F1 o
num=count;  
1 |6 G! X6 e7 t. C$ d4 Aelements=htable.elements();  
# P9 y2 _# j/ G+ m( r/ u3 bkeys=htable.keys();  ( l2 H4 z2 _6 T
while(elements.hasMoreElements())  ! y' C: x! Q8 A( C( l3 |
{  * f  E7 H0 S6 C$ `4 G5 c1 [( V
key=(String)keys.nextElement();//fetch value of key  
: ?1 h$ _6 j- L, S( ndata=(String)elements.nextElement();//fetch value of data  ' A7 @( r3 |% I4 b
sortkey[num-1]=key;//store the value of key to sortkey array  3 r( z* D8 }3 r8 z1 w1 E4 Y; a, v" |% c
sortdata[num-1]=data;//store the value of data to sortdata array  9 ]9 t7 f$ \, U: `4 ~& u( I7 X0 w8 N+ f, S
num--;  9 o  @# m% F. V: S7 f  V
}  
6 Q+ N  W; R3 |1 o' e  hSystem.out.println("2 L2 Y9 i8 V8 V' l4 a
编号"+" "+"内容");  
+ F) F3 g3 F$ I2 i" ~4 s8 e3 `for(int i=0;i<count;i++)//display all content after sorting  
: g* J' {' u- E: jSystem.out.println(" "+sortkey+" "+sortdata);  % i& O& O9 c, Z6 _& P3 J
System.out.print("
/ V- |- ~3 m% N+ J/ q* f! \  x目前共有"+count+"笔数据");  
, j- o% y1 d7 q6 XSystem.out.print("
) {1 w  w# b; H/ U6 D4 p( w1 ]$ @0 `3 u, _+ ^  |! N' N
按任意键继续...");  $ {  {; f0 D" ^3 P  R/ B, I
enter=bufin.readLine();  2 |. K+ o* F% p  ?. _; V% K
new str14(htable);  
) V: s2 O4 l. k) ^break;  ; d% E. j9 E+ {
default:  
6 r, A! }1 ?7 {5 |* A}  , I. w) x6 ~0 ^! F! M
}  
/ q) t+ [! r0 `  a5 w) L# p4 F9 J# B- d: y1 b
public int select() throws IOException//method of getting a function selector  
0 n. c0 E: y3 R0 i' i; Q1 Q{  
+ _: _( A2 K3 C6 IString input;  
% Y( X5 X6 J+ {; l2 i* {int value=0;  - Z& `$ \% }0 p' G
input=bufin.readLine();//read a input from keyboard  
7 B! O& J  G* Ktry  
* K% w/ a8 _3 m& X- S; \' }% B7 w{  
9 t1 d# y' b; |# x  G, Avalue=Integer.parseInt(input);//convert a string to a int value  
1 p! o( a, ^. E! t( c5 b}catch(NumberFormatException e)//can’t vonverted  
' H" x0 l- S5 [. {( b{  ( d& n2 `6 v; f. R+ x
System.out.print("请输入选项1~6:");  , t( k  I, l8 T# l3 B
value=select();  
/ V! g9 f8 B' a6 C/ V% f& r0 B}  
) [; e5 l) ?4 p! ]. v, v5 iif(value>6||value<1)//if exceed then print a message and reenter  2 _' X; M- v9 I* j: `
{  7 D& M' @4 U) r, s% {: E  h1 W
System.out.print("请输入选项1~6:");  
2 S  S- Y* K$ s% {value=select();  
* X6 G# b+ h1 u4 e1 r4 I  x1 S}  
  p4 K5 i9 v0 h" f( K1 s! ^return value;//return a value  , r% B' A. q  i- l6 |& l1 b
}  ; H0 t# b2 }& ]% _. h1 P' a. Q

9 u. T: G# r1 f2 Upublic int getid(int count)throws IOException//a method of return the number of data  5 I8 D, E. Y4 S( t3 V4 \
{  8 s7 f2 a, F( Z0 C. A% j) X, x
String input;  7 I# c% u% _- j/ G- R
int value=0;  
. {" l0 {: ~, y1 ]input=bufin.readLine();//read a user input string from keyboard  # v8 S$ n$ E4 ], b
try  - Q$ b; }9 @- |: g& T
{  
, c; U" F# X% A- B: Nvalue=Integer.parseInt(input);//convert the string to a int  4 D" f6 B! e+ Y5 w( N
}catch(NumberFormatException e)//if can’t convert to a integer then reenter    W: E4 @( Z3 p8 ^9 S
{  0 a, y$ L' u3 c6 g; p0 B/ \  m
System.out.print("请输入数据编号:");  # i* M# d0 t2 \& [0 q$ Y& ]
value=getid(count);  
0 X$ f2 q; N- i7 O- F9 X% Z% `}  
2 @5 v5 p9 E7 e6 eif(value>count)//the input value is out of bound  # j% q' |7 d7 U2 q7 a% ?: x, G, @
{  
8 y2 i9 k1 {3 ~' {; Z' f8 \' o7 `+ CSystem.out.print("无此编号的数据,请重新输入:");  . G! F$ V8 n4 }
getid(count);  1 f9 ~' D3 K. F$ L
}  
; T) g1 G" @! q0 sreturn value;//return a value  
* r& i; ~" W. m: Q}  
& E; P( i* h2 `& C}
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-8-8 21:59 , Processed in 0.473638 second(s), 51 queries .

回顶部