QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
8 Y* M' K1 Y  ]% C8 {3 Himport java.util.*;  
' P# W) H+ G% U2 `' Z7 l3 Cclass str14  6 t# y: g9 B6 w! t/ M' h7 z
{  
- ^* q6 m9 |5 h, ?  r1 DInputStreamReader stdin=new InputStreamReader(System.in);  0 q& v/ G: ~1 M+ z
BufferedReader bufin=new BufferedReader(stdin);  
+ r# b! E. f9 m1 q) `# `: [. W5 k6 z. M( n9 a
public static void main(String args[]) throws IOException  
' O! i8 b  W* [8 O! t{  5 j. D3 K% y, \4 F9 ?& v
new str14();//run the application  
% S  \1 M$ h7 l* a}  " `! b+ V, T! x/ K. r4 F0 O* N

2 m5 K$ ^) @3 d1 a2 Y. t: ?3 Rpublic str14() throws IOException//constructor  
6 r: H/ Z4 h' Z7 b, B{  
0 a; B/ V. ]8 c# {+ O: y" gHashtable htable=new Hashtable(20,0.75F);  
$ Q" T- X! F/ |  S' q8 z+ Y( [& h2 ndataBase(htable);  0 ]- Q$ B% x+ W9 ]! \
}  
; ?! n" j9 c+ c
% y# a7 X+ _; K+ ipublic str14(Hashtable htable) throws IOException //override the constructor  + j$ R% y8 y" i7 E4 ?
{  7 M4 f9 k/ B* W, j  ]
dataBase(htable);  
( K& j9 H5 z2 ?3 c# O8 c}  3 t, J, z7 H# f2 ~; u

3 `+ B9 h* ^5 @  L* _* f+ c& ^+ @public void dataBase(Hashtable htable) throws IOException  
) I* e" M& U7 q! C% F, E8 w' A{  
5 U! O3 P' P6 S, dint count=htable.size();//get the ammount of the data in htable  
8 s- b' Z5 Y3 [int value,id,num;  - P  d$ v. L% k& w
String key,enter,data;  
0 D; C$ Y; z2 \8 L; b. \3 k" M) xEnumeration keys=htable.keys();//get all the key in htable  $ B( N$ N6 G" K9 _# t; E# e
Enumeration elements=htable.elements();//get all the elements in htable  
$ Q# @# G4 C1 M2 Y! n& u' fSystem.out.println("
+ `. }6 v5 p/ G6 p, R& ~Hashtable 简易数据库程序
% W8 Q- E! F  {) V9 f");  
7 K, I% @' N  h! w! S0 QSystem.out.println("(1)输入数据");  ) R$ S2 u$ o1 z' Y- h( d4 a/ x
System.out.println("(2)请除所有数据");  
' g% B- @7 n7 T5 QSystem.out.println("(3)显示单笔数据");  6 j; L7 O1 U1 j$ |5 @
System.out.println("(4)删除单笔数据");  " K5 \  W/ R( I7 W# C
System.out.println("(5)显示所有数据");  
* D# _3 y8 ~# }% XSystem.out.println("(6)结束程序");  
, R# `) w) C3 ~% G) T- c) fSystem.out.print("请输入您的选择:");  ' u7 v$ h5 u7 g
value=select();//call select(),return the function number  # W% O  U. D& t7 t3 V4 d* L
switch(value)//the function  + j- b8 C# y( }/ u2 S5 C7 u" S  f
{  
( {" |  J' Q1 Q1 g2 ?. s1 l$ Vcase 1:  7 r, ~  A6 e7 E3 T
System.out.print(". S! E& h7 l0 `' L: l% {
请输入一笔数据:");//need data input  
7 [- r# p  K: K' Odata=bufin.readLine();  : i4 }  i8 N- K
count++;  " W7 U" {9 L% r# e
key=String.valueOf(count);  " `: b% w% R$ t# v3 s# ^/ s5 R( k
htable.put(key,data);//store it to the htable  ! o1 c3 j4 R! @" I  }' [, E
System.out.print("
! |7 w2 v5 j1 O" {+ n, }3 d输入完成,按任意键继续...");  / s; r: S- s$ c  Y, I: x5 W! h
enter=bufin.readLine();  ) L8 l$ ?' f/ ?& u7 k" u
new str14(htable);//reenter  9 r# r, k$ D- ]3 x+ _- Q  A
break;  . c% }+ p8 I0 G1 I% n2 q) H4 q$ D: b
case 2://clear all data from the htable  $ |& h; Q! B, P; ?
htable.clear();  
7 l8 _2 L$ J- N1 q: D& E8 R1 uSystem.out.print("
2 w; S3 K: J: X' J已删除了所有数据... 按任意键继续...");  
+ B: W, u3 j( Center=bufin.readLine();  
3 a! \' d0 i8 b  X! i" r+ K" \new str14(htable);//reenter  ) t3 P1 u8 V4 P; `- C5 K9 {
break;  
. s! C4 L5 F: Hcase 3:  4 e" c/ Y/ W8 R8 T
System.out.print("
1 w/ N  q  [/ E请输入要显示的数据编号:");  8 R) Q$ b2 H: i& D+ v
id=getid(count);//call getid()  
  O* U5 R+ e' G! `# y# ukey=String.valueOf(id);  3 e" S, C6 m7 I# w4 U% E; L* d/ J; S
Object select=htable.get(key);//fetch the data from the htable  / t9 W5 p% v- t; q6 \$ s  E1 }; {' `
data=select.toString();  
" |6 Y$ o  A) \: MSystem.out.print("0 c4 j0 f2 u; r/ M1 V: y# k
编号"+" "+"内容");  
/ X) d# \5 P- E2 h1 MSystem.out.println("
" a! r4 \6 ?4 D$ R) X "+key+" "+data);//display the data  - H" [/ g% k% I1 Y% l: t
System.out.print("- H4 X6 z# B7 z  ^( B
按任意键继续...");  : i+ h( R# A; b7 ~/ h1 P
enter=bufin.readLine();  
) ^9 ?7 k2 Z5 t' k. _new str14(htable);//reenter  
" p& B8 q5 @. g. N, r1 B7 ?  Rbreak;  
: M1 F( \# y: Dcase 4:  
& K) @5 T6 {% b: nSystem.out.print("请输入要删除的数据编号:");  + f4 d: P( `' p0 l( @- r+ C
id=getid(count);  
* H( K, [: v5 X& _key=String.valueOf(id);  : g* ?5 B1 `) g4 ~/ D# H) n- P7 l
htable.remove(key);//remove data  
5 q6 q" Q+ ~. F7 l! y" ]6 n! Ccount--;  
; R; s0 U2 g, {# b6 c2 @5 k3 o  [num=count;  
  i# ~: N/ R' J! I2 {  u: j& i& \System.out.print("已删除了所选择的数据...按任意见继续...");  # Q/ Y# W$ J  j; m% f& k5 F% E
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  ; e6 |! h. O8 a2 b5 X
elements=htable.elements();//fetch all the data from htable  
3 Q3 {6 M& }+ I1 C( K$ `while(elements.hasMoreElements())  * m+ c, D0 E+ h* A0 I
{  
! b6 v' D& f9 s7 {key=String.valueOf(num);//a new value  
- O5 I% B3 A# J7 w( U# jdata=(String)elements.nextElement();//fetch content of the data    Z8 n- K; x' e0 P
htable1.put(key,data);//store it to htable1  & q8 t( x  A, G$ A1 ?5 y# g* K
num--;  ! [. a) k0 F1 w4 D" `5 |8 t
}  ' @, E6 g1 e2 H4 q1 P
htable.clear();  
& I; F1 ~0 v  a+ W5 {% Oenter=bufin.readLine();  * L* _( y1 o$ b( z) t+ j
new str14(htable1);  
+ h3 }& Q) p1 g! M$ \) o% S: Vbreak;  # H0 |( I! j4 {
case 5:  
5 [0 H! l) Q' B" p, S+ BString[] sortkey=new String[count];//create a new sort array  , `: z9 [- Y5 K; [) g4 I
String[] sortdata=new String[count];  
* A3 ~8 y0 C8 ^9 c7 n0 cnum=count;  
, X# K( F- X, M) n/ m: K0 J% [7 Gelements=htable.elements();  
$ W" d" S3 o; J4 _* @* l/ ^keys=htable.keys();  
: F9 J/ ]* X1 Lwhile(elements.hasMoreElements())  5 X/ K+ ]% G+ b/ s, ]
{  ' {3 i( v5 I5 B" [
key=(String)keys.nextElement();//fetch value of key  / @% B" }" H6 @, ^/ Q- ^% e8 [
data=(String)elements.nextElement();//fetch value of data  
9 H) b" j: [7 C: G; F( h  k* @sortkey[num-1]=key;//store the value of key to sortkey array  
$ o8 f  \% F! fsortdata[num-1]=data;//store the value of data to sortdata array  
9 ^) _1 s; u8 j0 \- x( Inum--;  , t9 B+ H8 V/ k* N
}  ; }3 B2 l2 J) o: f/ O
System.out.println("
- E( o' c" |( I( v- w/ _编号"+" "+"内容");  
5 j" N( t- p$ W9 j0 [3 q$ z9 Pfor(int i=0;i<count;i++)//display all content after sorting  & m$ {, ~0 c/ X. \) d
System.out.println(" "+sortkey+" "+sortdata);  
; I! z$ @* B. X6 t8 E4 G; [# WSystem.out.print("7 Q1 _! Y3 @7 p0 |' u# A6 ^
目前共有"+count+"笔数据");  ) b/ _1 G2 B: g) ~1 F$ O: Z
System.out.print("# }) c% r3 Y  S) n

* ~9 Q- E( |+ Q9 k3 k( |6 r: l8 K按任意键继续...");  
* w- r" ?+ b* _6 ]; Y/ K' henter=bufin.readLine();  8 t6 X" v( f. y2 ?# q! }
new str14(htable);  $ K' b: D, [. n- t, Q2 W7 D% o
break;  0 ~7 m# |3 Z- j" e7 W# T7 s  }
default:  
3 A, X2 B  c. u& P8 g* s}  
  O  m0 {5 L# L" }1 d2 I" b}  ! [" i: ^5 n# i8 c* ~  [% ~

% I* z; A( t! Mpublic int select() throws IOException//method of getting a function selector  , G# U6 D, E5 U
{  
+ ~0 T5 h  w$ z9 v" Y+ r- VString input;  + l  S5 H9 Y, y3 F, i
int value=0;  : f/ ?' ?# N; k+ |4 C/ J
input=bufin.readLine();//read a input from keyboard  
. p$ ~1 t) A) Ztry  
/ }3 ?3 K- J% \2 N{  
7 v: I& v% a" C9 y& p9 G9 rvalue=Integer.parseInt(input);//convert a string to a int value  
3 p" H0 m# u4 \0 Q# g}catch(NumberFormatException e)//can’t vonverted  ; Y0 B% t# A* Z& M7 b! Q
{  
" a1 _9 Z) _0 L+ P0 }System.out.print("请输入选项1~6:");  3 i2 o+ j- j$ @, V
value=select();  : ?# G. W: C* l! l
}  ( _& b/ t4 ?' A5 P
if(value>6||value<1)//if exceed then print a message and reenter  
/ ^3 |0 Q7 A/ w{  * r, V9 V! Z: r/ q# e
System.out.print("请输入选项1~6:");  6 M( m( V8 P( p& Y+ D( _  l
value=select();  9 M# d: h6 F3 C5 r
}  
6 G6 @8 a  p* W( w2 ~) sreturn value;//return a value  
6 e( x; p( X: M$ `" l}  
' H4 U0 e& J0 c# p* f' R( Y1 N2 c0 a& j& U% R' G2 }
public int getid(int count)throws IOException//a method of return the number of data  0 R" T( |# j  y8 l7 U: l
{  5 ^& u2 \1 k8 |. W/ {+ H
String input;  / P. P0 ^2 b' f# c) ?
int value=0;  ' F& s8 P, Z7 Y' Z1 d5 I; S
input=bufin.readLine();//read a user input string from keyboard  & S9 f- }! m9 l! b
try  
4 ?0 f9 R1 s# \1 s; ]% T7 O{  
5 d; }2 E+ ?6 Hvalue=Integer.parseInt(input);//convert the string to a int  ) b4 y: d+ v, n/ t0 @0 t" F
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  4 V- K5 G0 }; K  j
{  
* j* J" L- J+ ~1 ^3 c* QSystem.out.print("请输入数据编号:");  # s# O6 T, A/ p. G9 N6 H3 N! E# j
value=getid(count);  
4 }1 x# z7 S: |}  ) A' d0 {4 [! `) T" |
if(value>count)//the input value is out of bound  
) j- p/ i) M& G, T& J4 N* t{  
: M" I+ ~. K3 n* @* WSystem.out.print("无此编号的数据,请重新输入:");  
3 a. B/ n- ?1 {0 E# sgetid(count);  
/ U5 y. Q% N1 ?. i}  8 j; T5 A6 d8 r' X
return value;//return a value  
0 d- q. m+ N3 G% U7 y& o}  $ ?4 ?% u( ~2 \8 g
}
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 08:54 , Processed in 0.286101 second(s), 52 queries .

回顶部