QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
+ R7 S/ Y% T2 H' bimport java.util.*;  
- W$ H( d' ]. Fclass str14  
0 u. ?+ A5 J) s- g- Z& q{  , t2 M. T5 O" q8 }) b! k
InputStreamReader stdin=new InputStreamReader(System.in);  8 C( j" F6 `+ p$ W: I
BufferedReader bufin=new BufferedReader(stdin);  / Z1 M$ Z5 r2 D6 o* E

4 u) {/ h7 Y& M3 v6 apublic static void main(String args[]) throws IOException  
3 v0 M* }4 d* H+ y6 J{  * \9 s. D# }! f6 h3 b! [% W/ n
new str14();//run the application  
: Z8 B! u6 L$ g3 [3 P5 J1 b}  
2 g! d2 q& ?5 d5 W+ ~% u  N$ o3 w( ]3 @
public str14() throws IOException//constructor  & W  \) s# W; H2 _9 d
{  
. E8 h2 o. w, |+ bHashtable htable=new Hashtable(20,0.75F);  
' H- z& d# p9 L/ T. {  k' F2 wdataBase(htable);  - `: m: D' S) X# W0 y' l
}  
, D# u! I6 c8 S) }5 G" y0 n& i( G- G7 O# D  ]0 t/ c( X0 i! t  ~
public str14(Hashtable htable) throws IOException //override the constructor  
3 L, {2 Y. n% ?% g% r9 I9 ^{  
& G7 l" ]  ~( B& @8 [7 `2 Q% S8 w# wdataBase(htable);  / v+ O& g1 N& i& h9 g  P
}  % W  H( ~( {8 ?; P3 G" J

# M+ E7 p( W* ]public void dataBase(Hashtable htable) throws IOException  5 P- T" S- A& q' r7 w8 ?
{  ' |8 N  h- a  I) R9 B$ y6 n1 R
int count=htable.size();//get the ammount of the data in htable  - a' U0 c& Y6 q5 w/ o
int value,id,num;  
9 q9 Q0 j, C/ i# |( `, T6 F" ?) PString key,enter,data;  
6 H# w) F1 {2 v% }1 V  A2 ?( |Enumeration keys=htable.keys();//get all the key in htable  
  ]6 _. m- ^# m& _0 |Enumeration elements=htable.elements();//get all the elements in htable  
9 |+ I& D% Y3 p5 {3 p) [  K: ESystem.out.println("6 X! D& @/ P" x* T
Hashtable 简易数据库程序5 A# B2 N8 M9 C$ G
");  ! b: w+ r8 e$ w! o; d; R( G0 l
System.out.println("(1)输入数据");  $ M; ]) C) l' p9 x6 F- E
System.out.println("(2)请除所有数据");  2 X$ h& |7 B5 \+ d6 E
System.out.println("(3)显示单笔数据");  / L9 c# z6 p: B; ~" s* G* b
System.out.println("(4)删除单笔数据");  
, V+ ^9 T& ]% Q  s8 m" ^9 v% lSystem.out.println("(5)显示所有数据");  ' Z: Y7 \6 N! [% C0 m
System.out.println("(6)结束程序");  
) c  l0 J% S. _1 K0 `; B# S+ BSystem.out.print("请输入您的选择:");  
8 Q) H3 ?5 S) f/ Dvalue=select();//call select(),return the function number  
  f* d; |+ K1 v& P# y- h3 sswitch(value)//the function  % ~3 f. t% K6 M
{  
" L9 _) C; {) j7 Z+ \% @# R/ vcase 1:  % d( L% a5 ]$ {/ F$ f
System.out.print("! w* Q2 b6 M/ f3 F- Q
请输入一笔数据:");//need data input  + L" l, z7 q2 M0 Y+ ~. s
data=bufin.readLine();  
' w4 j6 j, m1 [count++;  * a9 C" }5 D7 [7 Q# Z" B
key=String.valueOf(count);  9 T$ ]! i6 v1 I, F/ r! [  H6 h
htable.put(key,data);//store it to the htable  3 O: a6 z3 E+ |
System.out.print("0 p8 T/ ?4 Q: H6 ^* ^; M' n
输入完成,按任意键继续...");    P5 n9 ?6 m! Y& g7 O. K8 N
enter=bufin.readLine();  
  O2 v7 K6 ~# qnew str14(htable);//reenter  
+ {% {! E& I# F2 Cbreak;  
) }+ ^* i' i( Fcase 2://clear all data from the htable  & O1 |9 ^$ r/ j4 ~! X
htable.clear();  0 y# Z5 I2 y3 d3 K8 R5 I9 s3 l
System.out.print("( V# {6 Y9 G& v
已删除了所有数据... 按任意键继续...");  9 h' Q3 W( F, l, H  ]0 k% [  O$ x
enter=bufin.readLine();  
& s0 ]: y! m; U' i5 ^new str14(htable);//reenter  9 J  i, K. W* E* d( s2 u
break;    v6 e7 j' b% x6 C3 g) n/ ?
case 3:  / L  Q% w( ^. y: F
System.out.print("$ g0 A8 a% h) |1 K8 d
请输入要显示的数据编号:");  9 R2 r  n, \+ D' |2 j- @4 \
id=getid(count);//call getid()  ( P  I' o6 X0 ^, Z3 u. W4 J
key=String.valueOf(id);  ) F! E+ g  F: g% b2 e
Object select=htable.get(key);//fetch the data from the htable  $ t: p& z! J3 _' D7 K
data=select.toString();  
( k6 r- }* S: g" ~* ~0 o3 d( ESystem.out.print("( o& i, g: o2 S8 Y" @5 f& v- M# d4 E
编号"+" "+"内容");  : r' x6 \% Z2 u$ L4 q4 O- h- h: b
System.out.println(") j% Q+ `7 E( d# r0 E+ C, e% z
"+key+" "+data);//display the data  . c( X3 l4 B! X6 @
System.out.print(", Z7 J6 Q6 ~" j* t' C: D* g" f/ I
按任意键继续...");  
" ]& |" z  d0 }( u: d: j5 e2 nenter=bufin.readLine();  ! p& ~: J$ _/ V% ~- t2 `% m
new str14(htable);//reenter  
9 ~2 ]# ^! \( r. ]: I  M" T8 @- obreak;  ( T1 y$ l: y# b/ I3 r
case 4:  
  U. H; H+ a4 z: a8 ZSystem.out.print("请输入要删除的数据编号:");  
- a4 G9 Z+ @, |7 [; W" pid=getid(count);  ; Y$ B+ M5 w: H" H& Z- f
key=String.valueOf(id);  * X4 o, d/ T/ N- v1 m$ d& O% I/ f
htable.remove(key);//remove data  - g! ]0 g5 ]+ \& \$ E
count--;  0 M, V0 C' L. q) Y/ V) r9 R+ k
num=count;  3 o& M7 J4 ]3 T* f, d! K
System.out.print("已删除了所选择的数据...按任意见继续...");  
) s/ S: g( ?9 V5 H: n- [% F  `Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
  F  W* v% ~) O7 r, [elements=htable.elements();//fetch all the data from htable  4 C( O- G4 `3 O1 X
while(elements.hasMoreElements())  8 b9 X4 F0 g3 E2 \. K  s+ b2 B) {) k: `
{  : f! }( V% n; y, o6 D0 n; [! A
key=String.valueOf(num);//a new value  
1 L5 M# U! h; Z5 w4 Gdata=(String)elements.nextElement();//fetch content of the data  
) Z" J0 \, y7 h3 ~. y4 [5 Dhtable1.put(key,data);//store it to htable1  
& d) I( F3 m% H& {, Enum--;  ; b  ]7 n+ P% _" h5 \! _1 v& p* U
}  
7 b# U& d6 x' h8 i8 I2 e7 Y6 Xhtable.clear();  
, D; \" B0 `2 D& nenter=bufin.readLine();  
( D, S& {. l9 e. Nnew str14(htable1);  9 B- y; g. {5 }
break;  & t+ E7 `$ B, F; H& t2 R1 ?/ h
case 5:  ; C8 p+ e/ A9 q
String[] sortkey=new String[count];//create a new sort array  
. g, [0 X2 U2 L! b) M0 f0 LString[] sortdata=new String[count];  
* q$ u) n6 m7 {- {' |: F5 \num=count;  
1 w# s! }& f  _9 d% G1 xelements=htable.elements();  
7 p% y6 w* B% L3 K8 K/ Xkeys=htable.keys();  # @7 k/ b8 Y& a" d* |* I" A; ]
while(elements.hasMoreElements())  
/ \1 r7 I9 T8 Z# j' ]! ~{  
, P6 {1 ~3 p0 W1 A3 vkey=(String)keys.nextElement();//fetch value of key  6 [* Q6 F* |4 I6 V- v
data=(String)elements.nextElement();//fetch value of data  
; O) Y9 D% i5 t/ Fsortkey[num-1]=key;//store the value of key to sortkey array  
$ z+ G! T7 Z, l% b" Usortdata[num-1]=data;//store the value of data to sortdata array  
  b3 N/ {0 W9 i0 s: Enum--;  
2 z& |# d# e9 R* }8 Z}  6 B' @2 I, s  ~3 d1 w/ ^* {
System.out.println("
: g9 L  \6 H& c3 N$ Y编号"+" "+"内容");  # P" Q; ], v( j1 @( G
for(int i=0;i<count;i++)//display all content after sorting  - \) U/ X# G. I5 n
System.out.println(" "+sortkey+" "+sortdata);  
6 R8 j6 M! C# u) K6 b: w% K% {System.out.print("! V' E- H& b  C9 [6 f# O% m
目前共有"+count+"笔数据");  
: n$ M; B/ u! |) @! }" KSystem.out.print("
" E7 e$ L  S2 j5 E/ k6 W  T2 [$ @3 l9 ^) Q
按任意键继续...");  
& C7 Q; ?4 _  U& G5 k9 N3 N4 J# ienter=bufin.readLine();  
1 A) ]0 _+ {3 k$ Y8 e- [new str14(htable);  
7 u8 s* k% @+ Y' p# P$ d: Zbreak;  ) n6 C/ R( ?5 A) r. ], z: E, u
default:  
& ]3 C! }' @) ~( v& R: m9 ^}  
4 M7 k5 J0 I/ I3 e}  
8 [  [  V0 A* \
. u3 B, w2 h; R. B- z) v$ h4 epublic int select() throws IOException//method of getting a function selector    U/ C/ ?$ s* {. j
{  " @* h6 r- _( Z# D7 [9 |: y
String input;  4 w) s) j; k$ H6 |3 y( l! L
int value=0;  
3 p* i, B' B* K4 cinput=bufin.readLine();//read a input from keyboard  
& w0 g  ]4 K' k  v7 rtry  0 W) C; @# E# g
{  
. ~+ @  d/ w5 xvalue=Integer.parseInt(input);//convert a string to a int value  
3 R+ c$ w' ~! U7 L4 M}catch(NumberFormatException e)//can’t vonverted  . |2 B2 s* |! Y0 F" X9 `. m/ r
{  % w0 k4 S! c' {3 m
System.out.print("请输入选项1~6:");  
' w3 N- o6 z$ F" y$ j) O7 svalue=select();  
3 a6 x4 o* f7 u  F}  
# y3 a) T7 S- r; M! o) C' d% H6 Jif(value>6||value<1)//if exceed then print a message and reenter  
9 b9 Q4 D# o: [$ G7 O; ]{  # I' Q. p6 `/ P8 R6 s
System.out.print("请输入选项1~6:");  
2 x& K9 V* k0 p* I  ?value=select();  
: m( {/ d9 [" V# d8 J7 k! H  [}  * B! m) \+ r/ J  t0 A0 ^/ j
return value;//return a value  0 z2 k# ~% v" A6 C
}  2 _" x! {7 Q0 a  J! D

) P! Q4 Q% w5 ]public int getid(int count)throws IOException//a method of return the number of data  3 i: n. d% _3 z& E
{  
& ?$ Z. }3 m2 P" `. F9 Y8 B% r0 pString input;  
5 u6 N$ I' s* P: [+ c) Q0 eint value=0;  - T' t- L% @) I
input=bufin.readLine();//read a user input string from keyboard  
* D% U% I* `& b4 \$ S. e0 Qtry  
+ t4 {2 X6 I) g{  
# y+ c' l2 J% P: Tvalue=Integer.parseInt(input);//convert the string to a int  
" o: D, e* U9 f7 j}catch(NumberFormatException e)//if can’t convert to a integer then reenter  8 U3 [- Q, U9 @9 U: B
{  : ^; O. [$ J6 H9 A8 t
System.out.print("请输入数据编号:");  
! E. O% q: D$ I' U/ O; q1 P8 L* Pvalue=getid(count);  3 T0 q( E+ b) u" }9 @0 M
}  
  q2 S( M/ w- t( a' H' y5 n1 kif(value>count)//the input value is out of bound  
; S8 t* S( W6 _5 I: c/ ~- l{  
4 \8 V: Z! B( O, P$ w" s! MSystem.out.print("无此编号的数据,请重新输入:");    i/ l0 S; M. l, C* v& j3 R5 O& i
getid(count);  ; c  P7 a6 r  E5 y9 A! u# s
}  
3 }! S' G& d" r- ureturn value;//return a value  
$ c' i& X; u" ~/ l0 a# @}  
" i' E; r! [  P( {# o( u}
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 20:46 , Processed in 0.531527 second(s), 52 queries .

回顶部