QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  " [0 E# k1 {! F; R% r9 B1 L( U/ q  z
import java.util.*;  
* f! K9 r4 X3 Pclass str14  
2 j) ^$ R  M1 d# e# R) a$ T{  
+ i9 c) H/ t% v' t, gInputStreamReader stdin=new InputStreamReader(System.in);  * t* O4 l5 U: u& s" z7 h" I7 Z
BufferedReader bufin=new BufferedReader(stdin);  
& Z  j1 T  V0 Z1 D5 I! ?; }/ N7 k4 y/ m3 c
public static void main(String args[]) throws IOException  
; s* ^' b/ ~" V# U{  
' ?5 s' M! l$ B! {) d8 Onew str14();//run the application  3 q& `( L& H# j) j) b
}  
4 R; m  ]$ @; g* N1 n% |3 c; |8 f/ }4 f3 S6 {9 Z/ d# O: _
public str14() throws IOException//constructor  
3 H( D" T4 x  S  w" I$ n7 L: C{  
- n5 e- x5 L/ cHashtable htable=new Hashtable(20,0.75F);  2 z8 E6 E8 @# [; F9 G: |
dataBase(htable);  $ [' m( N& ?5 T6 ]
}  
& `. _: L2 i7 z# G3 C5 }: i/ ^& M6 L$ Q
public str14(Hashtable htable) throws IOException //override the constructor  
! [1 K' Z* l$ Q9 _) U3 q* V{  
) n1 ^- f/ Q1 F% wdataBase(htable);  7 M* A& _7 ]  H
}  
1 E- {" W7 {; U. f  r
  E. l/ U" ]1 g6 q% [public void dataBase(Hashtable htable) throws IOException  
: |, @1 L$ h% u* V{  
9 }5 X+ g, x2 i# n3 z+ `int count=htable.size();//get the ammount of the data in htable  % r6 |& E/ ^: R; T
int value,id,num;  
; p( ~! |" y3 f4 j/ NString key,enter,data;  
& @# O  v8 I2 {& E9 ~& _4 tEnumeration keys=htable.keys();//get all the key in htable  5 N# Z- V* n: V* ?& X4 g
Enumeration elements=htable.elements();//get all the elements in htable  
; m6 i6 I5 g! E! M7 u. J. YSystem.out.println("5 a8 N, l  T& f2 B& j6 t/ ~4 h/ a
Hashtable 简易数据库程序, T; |: ^* a; E% q7 V7 U% U
");  
0 A( T  h9 E5 P: |' G1 R2 B5 vSystem.out.println("(1)输入数据");  
4 R! C# l1 p4 p' C+ F, H: V* {System.out.println("(2)请除所有数据");  - K4 Z$ w2 J$ J( I9 }0 ]
System.out.println("(3)显示单笔数据");  
# u! ]! m: j# l2 d' w0 ~System.out.println("(4)删除单笔数据");  9 ?4 g& K; W( W8 X
System.out.println("(5)显示所有数据");  ! p. V' X! y7 p/ Z4 S8 W2 U- w
System.out.println("(6)结束程序");    ]" X5 ]% N1 T8 s5 P; N: d5 _
System.out.print("请输入您的选择:");  : Y2 Z  C4 x) d6 `; d* e( v
value=select();//call select(),return the function number  
9 l* I* f0 L6 \3 X3 ]' H, [: @switch(value)//the function  ' R3 H- I' @/ _( n' r1 H& _9 D& S
{  * k/ x0 K8 `# }5 M+ Z
case 1:  
8 a8 L0 J, k, D& F  k) I4 C9 _System.out.print("7 \* h3 C- g/ w# I2 n8 I
请输入一笔数据:");//need data input  ; c+ ~: C# S8 K) W% k# S! c" _
data=bufin.readLine();  . y7 k, l) W% L: O5 f/ p) _4 B
count++;  / E( W1 s; \+ H8 t
key=String.valueOf(count);  : I: b' S6 ]" q8 P) D: w
htable.put(key,data);//store it to the htable  , ], i3 T# I* o! ?
System.out.print("& t1 Y3 f  w+ r: Q
输入完成,按任意键继续...");  
4 Z! a9 O7 T! n% \4 q5 F' venter=bufin.readLine();  , D" n( F5 y: ^4 |
new str14(htable);//reenter  
9 f& ~$ ~: b% }) H. `7 xbreak;  2 P2 g8 j4 E2 l2 \$ G
case 2://clear all data from the htable  
/ b, O5 p" D1 Zhtable.clear();  . r% N6 t$ h0 e; A7 O
System.out.print("
, Y( ^' C& Y4 F已删除了所有数据... 按任意键继续...");  $ O7 y- _! L1 U$ g
enter=bufin.readLine();  
# ?7 D+ Q2 _/ A; a* m7 C5 A) e* `new str14(htable);//reenter  
! B0 @: {  [9 I) f. L7 J. z" `break;  
# W) {( `; B) K: N% J4 i2 m/ \9 ?case 3:  
# e6 A# k+ z! P: ASystem.out.print("
3 e# X! e0 \) g1 O请输入要显示的数据编号:");  6 |- }( Q+ _+ M5 T8 d- y* n
id=getid(count);//call getid()  
) ?& g4 Y  Z* X$ k) {' `key=String.valueOf(id);  $ Y, \6 i7 C- `2 r4 E: h
Object select=htable.get(key);//fetch the data from the htable  , L; D& m  Y1 y/ Z% T( V
data=select.toString();  # W0 W4 l0 F2 p
System.out.print("- R: Q6 d  K/ `  u3 H
编号"+" "+"内容");  5 d/ i& Y) ~2 R" b$ D+ o
System.out.println("
" u8 A6 x9 z: `' _ "+key+" "+data);//display the data  
( [& W  Z& u4 [) K' Q7 wSystem.out.print(": S, s4 q" O& t1 T, N
按任意键继续...");  ; b* F' y9 G" Y
enter=bufin.readLine();  " d/ `" v; E/ b6 g2 ~! R0 e
new str14(htable);//reenter  . N9 L  k/ I4 s! C
break;  , N/ ~! o) n% T/ Z
case 4:  
3 I, r0 ?1 x. f" k, d6 F( jSystem.out.print("请输入要删除的数据编号:");  2 n+ D- Q) a; m5 ~. V/ R/ t' E
id=getid(count);  4 C5 X4 ?- P1 l5 {5 `
key=String.valueOf(id);  , E8 o, a9 G5 B* I
htable.remove(key);//remove data  
( [' R: U; A7 d# Y5 `1 K. h: A0 Ecount--;  ' s1 z! x$ m1 e' [4 |( k
num=count;  8 k9 j' O0 y" x; ?- q1 h2 n; {: i- N
System.out.print("已删除了所选择的数据...按任意见继续...");  
4 \% }- [  e$ p. _8 i3 i$ eHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  7 {1 C& W% z3 r( G( E
elements=htable.elements();//fetch all the data from htable  & j5 j/ h' I0 e+ Y7 z
while(elements.hasMoreElements())  
0 U# t$ P" [: `- C$ y{  7 @' o0 l# z2 T& b1 k
key=String.valueOf(num);//a new value  
, _! ^( t+ t' N  h4 ?0 ?5 ~& Ndata=(String)elements.nextElement();//fetch content of the data  % h* w6 x8 q9 a' C) s: a
htable1.put(key,data);//store it to htable1  
/ h& p' S/ p/ B" O3 Rnum--;  0 u$ B. H$ n, \
}  4 g% W* J* ?' d" L3 |, A1 `
htable.clear();  5 J5 N# H7 n2 J  Q: A& s
enter=bufin.readLine();  
0 T1 `9 q$ C6 @, [) @7 x- K; Vnew str14(htable1);  
0 |/ F6 l6 L# j+ s7 Y0 h, `* Mbreak;  
/ b* Z/ s. _- y) A6 G1 Jcase 5:  # |8 E7 |8 o1 g
String[] sortkey=new String[count];//create a new sort array  
. v4 l4 Q6 l: u: B" \+ RString[] sortdata=new String[count];  4 \7 k" W1 `) g
num=count;  0 P# D/ {( v( b5 O3 @4 m
elements=htable.elements();  
& R3 u; C- K& v" g% [( e# h% ?keys=htable.keys();  
& h5 S! [4 t. u0 }while(elements.hasMoreElements())  
" Z. x9 J" L; R0 u! h9 K{  
, l& u6 m8 D, n' R+ w* B9 o! ?2 jkey=(String)keys.nextElement();//fetch value of key  
0 g5 i1 Q6 L4 r8 b7 Y0 X5 Y" `data=(String)elements.nextElement();//fetch value of data  % g2 g0 W+ Y& E& Z$ J
sortkey[num-1]=key;//store the value of key to sortkey array  
& I3 Z0 ?: `4 v* W# ksortdata[num-1]=data;//store the value of data to sortdata array  
# b. R& V, s( ?5 _num--;    R1 O* J* T3 n; S/ X: c
}  
5 u4 W) S2 \/ @- g0 YSystem.out.println("0 {3 ~7 w+ `. X' W- {# a
编号"+" "+"内容");  : D7 I! P7 n+ f" j
for(int i=0;i<count;i++)//display all content after sorting    F* P4 ?% @: d' _2 r6 s
System.out.println(" "+sortkey+" "+sortdata);  
: ~4 ^0 S+ ^3 y( V8 }$ a4 A. o, q; aSystem.out.print("
/ B4 z6 r4 P; k/ ~0 n$ f. Q! |" a目前共有"+count+"笔数据");  " C3 }2 i4 C( H- A+ j: h# G6 N% W
System.out.print(": d$ Z4 a  c- ^& Y  l& F6 \9 b2 [

6 O! y. Z3 U! z7 q, |8 f, I按任意键继续...");  
8 e* N/ C& T  R! \3 R5 x% kenter=bufin.readLine();  ) c) h/ g/ h/ h$ T; W% X! \
new str14(htable);  % o  |* ]1 m# n  [/ A5 j0 `
break;  6 E# e* S& L4 q1 u$ J, ~% G
default:  / j0 i( K( F9 l) Y6 H3 ^. x
}  
8 v" s$ ~0 r' g, |7 M}  2 ]2 T' N9 d! k

4 H. a2 W+ A: C. h7 [! jpublic int select() throws IOException//method of getting a function selector  
9 L6 J, K5 X% N% z# O{  
& Y% o, u' @  M1 L" m5 _String input;  
3 V% `% @" o( n3 Mint value=0;  
5 z- C! g1 M+ H! `input=bufin.readLine();//read a input from keyboard  1 _; g- v8 R! P
try  
5 C. C: ?- x* P, W+ f$ |* z{  
+ ^9 h( F; m) J- Svalue=Integer.parseInt(input);//convert a string to a int value  ! N8 s4 I/ z1 }! D5 ]( A& k
}catch(NumberFormatException e)//can’t vonverted  2 q6 i. ~* E) t  q  d
{  
% J# F  O2 i3 F" y' E0 ISystem.out.print("请输入选项1~6:");  ) p! d) [% B- H7 D
value=select();  & J! }- z( j: z, C2 K5 _) d
}  
. j+ \" [( Z: t/ H4 D7 W7 Z' Uif(value>6||value<1)//if exceed then print a message and reenter  
7 n* o9 }6 U$ m{  ( z: |2 y; @. S  O( t. o: @# l: E
System.out.print("请输入选项1~6:");  2 {6 V2 L- j3 L% r9 D
value=select();  
" s1 R; \+ I+ E/ }( ?}  , X, J4 h+ ~- q- F; m, c: W
return value;//return a value  9 U  E# `  b6 G. j9 V
}  
; [+ v% U+ u6 T. }! I, Z1 i9 I0 ^0 y* S" n( d
public int getid(int count)throws IOException//a method of return the number of data  
( u; j) @; _- ~5 X# n! {{  
0 ]- y2 |, ^/ X. xString input;  1 P0 H% G7 \& [1 Y1 Q
int value=0;  % c9 l  O9 @- V: p; u- h
input=bufin.readLine();//read a user input string from keyboard  
5 e% C: x, T& R4 p! stry  ; g1 P( e7 d, a5 L% w# k7 |4 g  r
{  
( u- H8 A0 U1 @3 K* Hvalue=Integer.parseInt(input);//convert the string to a int  
3 l! v& m0 e3 c" j}catch(NumberFormatException e)//if can’t convert to a integer then reenter  ( }- ]+ @# G% D9 o
{  
0 q. q8 q5 d6 `. p2 h7 rSystem.out.print("请输入数据编号:");  
) w' \5 Z7 ]! J, v7 Tvalue=getid(count);    M6 i5 z+ U% }$ w4 Y! K
}  
8 v! N# Q+ }- J9 iif(value>count)//the input value is out of bound  
. ]; O. C6 P! _, r& c/ W; c{  
# d1 r$ K8 d& P% ^+ LSystem.out.print("无此编号的数据,请重新输入:");  7 D1 w6 S. q$ a$ L6 S4 w/ Z6 I
getid(count);  
& A) D, v) o$ h6 ^( g8 X2 R}  
0 ~  T0 }9 U# z" K1 creturn value;//return a value  $ T0 n8 d0 D# t1 V# D/ G1 l
}  ) p2 P- t$ _: }& @7 ?$ 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-10-9 06:42 , Processed in 0.415910 second(s), 52 queries .

回顶部