QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
7 C$ r) V0 f/ E& a. vimport java.util.*;  
0 H) }# r# g. h6 l- k7 [% t8 Zclass str14  
* m3 s9 K) D2 ]/ w/ s{  
, b& P0 }! b: p! @* e5 F3 vInputStreamReader stdin=new InputStreamReader(System.in);  
4 W# |/ c2 ^+ }, U1 W1 d7 ABufferedReader bufin=new BufferedReader(stdin);  ; ^5 O3 a6 S1 [! f2 r, W& A, h
8 E0 H# ^0 ~' l$ N7 ?' s& k' r
public static void main(String args[]) throws IOException  9 T: w8 T% W1 |6 x' m
{  
  V- t- }5 z& k  U; F. v; ], inew str14();//run the application  3 S1 }9 p+ A  ^  l! e
}    D0 g% I) o: V$ n) p* I; F

$ K1 }/ E# S% ipublic str14() throws IOException//constructor  8 X: c5 u+ i0 r
{  
7 @8 P+ P0 T4 b- e! e- I8 NHashtable htable=new Hashtable(20,0.75F);  ; w  ^2 P/ Y4 g2 [, t% G
dataBase(htable);  
; U$ |5 u# k, e- X3 i}  ) n9 s$ ]( m* |2 s4 E8 ]
, ~% Q/ h8 K* }: H; }
public str14(Hashtable htable) throws IOException //override the constructor  5 h4 o% F3 y, O. T
{  
, s# N5 U  h; |! jdataBase(htable);  
& l8 j: v; [  R: l}  . [6 M- o. Q+ ?8 ~1 w
7 O( U) f; \0 @, L7 n
public void dataBase(Hashtable htable) throws IOException  
- u4 E# {: b6 r  T- n4 r{  
+ c) q2 _* f8 z+ [3 Yint count=htable.size();//get the ammount of the data in htable  
% U5 ]& G# w- v) s1 [/ F: Fint value,id,num;  
5 U9 b# e  K4 a" L( X% {; xString key,enter,data;  # N2 U5 l3 e& V- L/ A, Q1 d
Enumeration keys=htable.keys();//get all the key in htable  
, ?% ^. @+ ^$ b. a9 iEnumeration elements=htable.elements();//get all the elements in htable  
; P8 S, Q$ G" b/ o3 fSystem.out.println("
; I% T8 X$ W- g1 k4 e4 @9 IHashtable 简易数据库程序
6 S- j6 I% i, c9 h9 y");  & U8 R) v) X" T& Q$ l& H) r8 M
System.out.println("(1)输入数据");  
, I( p1 T" O! t! f' oSystem.out.println("(2)请除所有数据");  ) D- C) b. f7 B. h
System.out.println("(3)显示单笔数据");  ( ?" t4 \! M2 ]# e
System.out.println("(4)删除单笔数据");  / n# U- `9 i. X
System.out.println("(5)显示所有数据");  5 b0 `( J7 Q( n
System.out.println("(6)结束程序");  5 Z2 V. ]0 K3 c
System.out.print("请输入您的选择:");  
/ r+ m0 H% A0 I8 e; |& \value=select();//call select(),return the function number  4 a0 T3 |) v# F  }% r
switch(value)//the function  & ]) C( L% f8 I( m5 b5 S" ^* e$ |8 p
{  
7 o, B- C- b5 [+ [' u! Z. `case 1:  & j4 Z2 M. r" ?, w3 W  K2 V4 u
System.out.print("
+ J1 X9 e! }1 P5 o0 D2 Z请输入一笔数据:");//need data input  - q; m! P( J8 F. F
data=bufin.readLine();  - w1 V- c7 R3 N2 e
count++;  9 `' @' l! u$ z) n& H) N, n7 I: A
key=String.valueOf(count);  6 f8 B# A, T. E% M2 s
htable.put(key,data);//store it to the htable  + ^2 x7 ^" L( O. F# D0 f5 x1 J
System.out.print("1 T7 `6 y; M& n& u
输入完成,按任意键继续...");  . E. E5 e( L/ U0 [  G
enter=bufin.readLine();  3 f; s( v4 t  d4 w* U0 v
new str14(htable);//reenter  $ ?7 a' X& A. X) T6 e  x
break;  
6 U+ c4 _7 v: R8 Bcase 2://clear all data from the htable  
4 D* ^( z8 _% w8 bhtable.clear();  
+ O; P/ b$ a, w% [- s  n- n2 c/ t- D- YSystem.out.print("8 y7 E2 p+ x6 g, e# f
已删除了所有数据... 按任意键继续...");  4 b# ]2 x$ @' X- e
enter=bufin.readLine();  ! h/ A, D$ f4 |
new str14(htable);//reenter  
* T+ |9 [5 U7 x+ \break;  
  ]3 P9 G8 p& }$ i6 B6 l! N' \case 3:  % G5 K5 C1 ]" r% x# ]
System.out.print("
2 ]% {# Z+ V; C# x7 A8 Q7 ~: M请输入要显示的数据编号:");  
' v; [6 b# i1 G1 t8 A/ h) tid=getid(count);//call getid()  
& w$ j' s& e7 J' Ckey=String.valueOf(id);  . Q3 e% b3 G& J5 V
Object select=htable.get(key);//fetch the data from the htable  
- f; g0 {" c& f  K* E5 Ndata=select.toString();  % V# D$ s; T4 X: Z7 f
System.out.print("5 m' I$ g& k, c8 E( z) @0 X
编号"+" "+"内容");  0 C( k" P8 a, O  a  |
System.out.println("
8 t* D6 U3 o% e& z5 m "+key+" "+data);//display the data  
. m  Q- x* N# }7 ESystem.out.print("
( z# k* {! v# f4 ^* b: [按任意键继续...");  
4 _2 H: k$ p, e" r% z  Henter=bufin.readLine();  
8 u% d4 I' T/ @' Nnew str14(htable);//reenter  8 L: z6 @. _3 Y) m5 C- }
break;  & w# Y  u  M% d5 \/ K
case 4:  
$ V4 ~3 C, i5 a% ^$ f( jSystem.out.print("请输入要删除的数据编号:");  4 N/ J7 b% d; t, {2 b$ b: d! Z+ V( M
id=getid(count);  ( X' K9 j& {, q3 Z: Z
key=String.valueOf(id);  5 H! s6 Y( m8 d3 X" f: A
htable.remove(key);//remove data  
% @. e1 h! D. W' k1 O2 K$ i: m; bcount--;  
, b' h! i& ^" h% N* ^4 }num=count;  
1 j8 D% |2 C6 Q4 H' @System.out.print("已删除了所选择的数据...按任意见继续...");  ; e1 Q: S3 v9 Z3 Y' y1 N) Z3 k( P: G
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
, o6 d1 [% Z! Velements=htable.elements();//fetch all the data from htable  0 [% J0 Y2 V3 T9 s
while(elements.hasMoreElements())  
9 _( W3 v; S: p: c0 K{  
; ^" j" y# b( l/ [key=String.valueOf(num);//a new value  
! T. B6 I) }6 T/ q/ Fdata=(String)elements.nextElement();//fetch content of the data  4 h# T- O" O# ?! o
htable1.put(key,data);//store it to htable1  / g7 A# b, N& @* G, |/ C9 z
num--;  0 i& l" C# J$ M: `- E1 }. h
}  9 Y: h0 ?, G2 u: u
htable.clear();  
, o7 G9 L4 z: ~3 I6 }enter=bufin.readLine();  
+ F6 p. g1 r5 v( u$ nnew str14(htable1);  7 u2 v* J, g2 J* W' h; \$ a
break;  
% j. M  @! _9 A3 |3 N/ g* _case 5:  
  q! o; _  ^$ B4 O" q2 ~+ nString[] sortkey=new String[count];//create a new sort array  7 F. W2 O& s1 W9 q
String[] sortdata=new String[count];  
' j% ~# ?2 Q4 b- F) N2 G5 Unum=count;  
  E# m; E5 H6 k/ c0 t. R  M  Helements=htable.elements();  
1 K+ O* k/ _- b5 a1 s0 @) U  Tkeys=htable.keys();  % d) D  i' N, ]( s1 g( S% m  n2 t
while(elements.hasMoreElements())  # `" |! c) Q' |# s" e$ V  d1 ^
{  % Z' X( I) b/ a2 T* k, J' U; ^
key=(String)keys.nextElement();//fetch value of key  
. ?- W0 b0 [/ Tdata=(String)elements.nextElement();//fetch value of data  
3 r. N) ?5 Y. K0 P# r+ Csortkey[num-1]=key;//store the value of key to sortkey array  + g) ?# c, N4 e
sortdata[num-1]=data;//store the value of data to sortdata array  
; x& f5 a7 n% a$ Y' i7 _num--;  
( f  o- V& O5 H3 l* R}  
9 N1 e6 P' K4 iSystem.out.println(") u# R7 i2 ]* ?7 o7 n: v
编号"+" "+"内容");  " ]% w! ^% }% u3 c4 y" [" P. t
for(int i=0;i<count;i++)//display all content after sorting  
. X! r. e" a5 ^# R+ [. D# G% cSystem.out.println(" "+sortkey+" "+sortdata);  - m6 O7 a% T9 h: E, C
System.out.print("
0 R: D; l- _1 N9 `& N# X3 N) P目前共有"+count+"笔数据");  $ b, v8 i6 b6 `/ a8 `# Z% C
System.out.print("
- O4 _! u  B% D3 A! G" I+ a( A8 H: Z3 q: c
按任意键继续...");  
# G7 n4 r  D; R. d8 {! Z4 p7 {enter=bufin.readLine();  
8 {' ]% h: f- n8 Nnew str14(htable);  
) U% k4 l$ k' v4 [break;  
/ e* v5 u/ v- `4 t  `) H3 Kdefault:  ) O' |6 |+ W. T
}  
5 Z# l, f( o! R9 S4 V* s7 L}  , T2 f- y2 z; O9 F1 Z' @
7 j$ I% T: j  J: \
public int select() throws IOException//method of getting a function selector  
4 S# D$ L5 A% a* a3 Q. P  s0 g{  ' v" N6 O3 r; z7 D) @  y+ @9 s
String input;  
& m7 m+ Q) i0 P/ p" O. p! hint value=0;  3 H+ J: X2 j- F3 v3 q
input=bufin.readLine();//read a input from keyboard    @& O2 x8 m/ |# Z$ p5 B( b1 ^; p. u
try  
( n, D: Z/ |3 I9 D$ Z7 n{  + z: k1 Y8 p, b8 d% j: q
value=Integer.parseInt(input);//convert a string to a int value  / ?$ e* r: |- j% o
}catch(NumberFormatException e)//can’t vonverted  
* H! f1 G1 T7 n" Z% [0 }8 A{  & o& E% e1 U: J
System.out.print("请输入选项1~6:");  
- I; q% B2 }# @/ {5 c6 svalue=select();  
8 c' C: r* ~# d4 w) I( q% O& V}  
! o7 ]) `2 {: E& _if(value>6||value<1)//if exceed then print a message and reenter  
! |- R! r1 T8 R0 Y3 h5 Q5 y" H{  
; l$ f; |1 A& s2 }$ N' Y3 S# kSystem.out.print("请输入选项1~6:");  
( `0 k: E# |9 s7 l0 N  lvalue=select();  
0 g! Z+ s$ K# u}  2 u; I; j2 ^; v- O
return value;//return a value  8 g' G5 i5 z! u4 R/ r- p
}  % r/ R4 i, e- g+ }! |3 F. W
$ \: e2 w0 T5 z7 J0 R* p
public int getid(int count)throws IOException//a method of return the number of data  ( ]" ]& ?6 e* v5 h$ D4 `
{  
1 p- q' ~! r4 y$ s0 e2 gString input;  - P- S9 I* O* \& i9 A) ^
int value=0;  ( v4 ~* v* G( e! C$ y
input=bufin.readLine();//read a user input string from keyboard  ; C9 u# ?3 W+ u$ M3 ^. Q
try  ) k6 b( T. O# P
{  * b4 W. N# d. J4 D% e0 Z. @+ E0 X9 E6 N
value=Integer.parseInt(input);//convert the string to a int  
; j& F" \$ ]" V( ?- v% B7 R}catch(NumberFormatException e)//if can’t convert to a integer then reenter  1 n) Y" B; W# T' ?" Y- z$ S- N
{  3 v; h: j9 F3 ~! C& {
System.out.print("请输入数据编号:");  
# d4 V# i) ^( o' y# A+ X6 n. gvalue=getid(count);  
, M( M! k. |! c) I* @) `7 s7 E}  ! n7 _: T5 c: F# ^$ F& A
if(value>count)//the input value is out of bound  ! w  A: _0 Y& \
{  % y% C7 V, l, G. a5 }1 u6 ~8 `
System.out.print("无此编号的数据,请重新输入:");  
5 K. ?& W( F* W& T& m0 B' B5 }# `4 ~getid(count);  
9 d- u& s2 u1 H& y}    N7 X$ r- O% E' ^4 f8 {5 m
return value;//return a value  ' i( j  O/ t# L$ z( `% h; U7 t
}  1 u% j; x# c+ G, T+ Y
}
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-6-11 22:42 , Processed in 0.524863 second(s), 52 queries .

回顶部