- 在线时间
- 0 小时
- 最后登录
- 2007-11-12
- 注册时间
- 2004-12-24
- 听众数
- 2
- 收听数
- 0
- 能力
- 0 分
- 体力
- 2467 点
- 威望
- 0 点
- 阅读权限
- 50
- 积分
- 882
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 205
- 主题
- 206
- 精华
- 2
- 分享
- 0
- 好友
- 0
升级   70.5% 该用户从未签到
 |
import java.io.*;
5 D: b+ d3 |% b0 oimport java.util.*; 1 `" v2 ], @$ }' ~, ~
class str14 ' m( N+ E8 M N. L! K
{ 3 Y7 n: a7 W n1 G* u, i8 j) A
InputStreamReader stdin=new InputStreamReader(System.in);
: X* p' y! n: Q3 wBufferedReader bufin=new BufferedReader(stdin);
: R9 X% h- }3 d
! z) P) b& J/ y& X' Wpublic static void main(String args[]) throws IOException / o) f8 l+ a7 x7 p/ z
{
7 y; n% _- L0 Y: e0 B8 n$ j, d# knew str14();//run the application
8 Y9 ]1 J2 t% J* t} * z% J+ B/ }+ d5 T. l0 h% P
: H) t: \5 a/ R2 D( cpublic str14() throws IOException//constructor
/ r9 ^% g. q2 |{ : u( y4 {7 {! k! X8 t
Hashtable htable=new Hashtable(20,0.75F);
" r) i6 _. O( X6 QdataBase(htable);
3 X) \9 e5 ]5 ]% L$ u% P5 h) w* [}
1 `# Q, Q3 M, M5 m) v. [
' R N) z- W; S: e* X; d1 g( gpublic str14(Hashtable htable) throws IOException //override the constructor + U0 a5 [. h8 j
{
; {4 z% [, e/ p JdataBase(htable); # M4 T8 d7 _/ C. j* B; v, h) }- O
} `, w& a2 b( V8 m8 ]" y# D( I3 Q0 M
/ ?8 d# Q' I2 c- O0 r; F9 B
public void dataBase(Hashtable htable) throws IOException
( N5 |2 X& I$ `8 m{ + C- A* h7 {, Z( ^, P; P- [6 S
int count=htable.size();//get the ammount of the data in htable
" S( S' P6 I) t7 w eint value,id,num;
7 @' I3 W# Q) M) SString key,enter,data;
5 v2 X( B1 x! `) `/ W8 G! z& C' {Enumeration keys=htable.keys();//get all the key in htable # o2 m8 a7 h5 ]! z
Enumeration elements=htable.elements();//get all the elements in htable ; X2 K. }! j9 L T
System.out.println("0 W6 a" C7 `" M# H
Hashtable 简易数据库程序" W9 W: V& a$ v; @
"); : ?5 j% N1 g( l+ y: w' r
System.out.println("(1)输入数据");
. W9 B0 q' `7 O( P% g$ p8 a$ LSystem.out.println("(2)请除所有数据");
0 n- |4 {$ E1 W. s# XSystem.out.println("(3)显示单笔数据");
7 L& U. K, ^# j+ g$ J- |$ ^System.out.println("(4)删除单笔数据"); 3 l; {) S9 d x) w
System.out.println("(5)显示所有数据");
; d( k$ Q. L4 v/ g, HSystem.out.println("(6)结束程序"); - \& H, J0 M% R) d9 I% c
System.out.print("请输入您的选择:"); & r5 V9 L5 ~( @$ A/ E3 r* v
value=select();//call select(),return the function number ' Z" B! G! c4 c) u" p, O- H- U
switch(value)//the function 6 Q3 E3 v# D9 G5 k
{ % |+ R+ G- N( `$ }
case 1:
( z. ~6 V7 M! `2 _ U( f- u) PSystem.out.print("
' V. W$ Q; t, C9 z f请输入一笔数据:");//need data input # Y/ t% M4 u5 T( y
data=bufin.readLine();
* S! `( G5 r0 u$ u% A. V+ W0 ncount++;
7 @: p& j% x1 V- O1 d0 ?key=String.valueOf(count);
" B, a4 k- s& m* y. m8 \" K* Y |( Shtable.put(key,data);//store it to the htable
. [' i) L$ j, `# Z( K, _5 R) pSystem.out.print(", v4 ]0 c1 l2 C& N) N+ P0 d- X
输入完成,按任意键继续...");
( N6 n# n/ |. K+ b/ N* denter=bufin.readLine(); . E& j! M4 D/ F5 N% G( Q8 l! y6 c
new str14(htable);//reenter
0 r* u D; s9 \# W5 `: Z: fbreak; H# m! G, B3 v' y$ s
case 2://clear all data from the htable ( l7 ~2 ? _" L, V4 {3 s
htable.clear(); 9 g L! J; f9 c; R8 a9 |) a
System.out.print("
# i8 E# p' x3 o9 R6 N已删除了所有数据... 按任意键继续...");
! c0 i- m7 H4 a- H1 ~enter=bufin.readLine();
0 ^: f8 _+ a/ l: R onew str14(htable);//reenter
7 F- Z3 r6 m1 Q0 _% t/ |0 q0 Fbreak; 9 ~% [' s, [1 v& Z p9 n/ ^
case 3: 6 f1 z" M+ G( D; ?
System.out.print("
2 T w' ]# v. I请输入要显示的数据编号:");
% O6 X4 V9 E3 y* J& zid=getid(count);//call getid() - f3 y/ c) P/ S2 n3 Y
key=String.valueOf(id);
+ `9 Y- P; ?+ O( b$ G) sObject select=htable.get(key);//fetch the data from the htable
6 a/ z2 r0 ]- s! g. Zdata=select.toString(); 8 w/ K* Q6 r5 S. j
System.out.print("
V; S! E8 e* V0 x# d/ x* p- t+ G编号"+" "+"内容");
: {) R( q6 E9 _System.out.println("9 Y( z. M$ N: m1 E9 b
"+key+" "+data);//display the data
, P4 j+ v% F# T7 s' DSystem.out.print("# ?: ]7 s- h! }) Y$ {1 t
按任意键继续..."); 8 x% a) I6 c: s3 s" l
enter=bufin.readLine(); S4 h0 K- j+ G8 ~7 ^& C
new str14(htable);//reenter
9 W0 G1 @$ w' D0 ]" s8 Kbreak; ' }" W/ K( |2 h- B- ]! f/ Z) f. N
case 4: # @) Q6 P* a8 s8 Y5 F! j9 b Q' d
System.out.print("请输入要删除的数据编号:");
d. \) F9 b2 X8 p' I, lid=getid(count);
) k, ?+ P$ G. s2 H# B J# L9 tkey=String.valueOf(id); 6 R2 `8 U4 ?, `$ ~+ q5 G( F
htable.remove(key);//remove data 5 U4 R; h# C# w. c- T
count--;
: l& e8 u, s7 @ Cnum=count;
) h# ^: s# v: Q8 bSystem.out.print("已删除了所选择的数据...按任意见继续..."); 8 e4 b l- m: q" ]* J% f: I3 [; ^
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
$ U- D" p) t- I f! l4 uelements=htable.elements();//fetch all the data from htable
) r+ `2 O9 k- I. m$ N" nwhile(elements.hasMoreElements())
6 A) l: ]& B' V1 f D5 c{
3 A! _# y8 r3 s% O* }key=String.valueOf(num);//a new value
- ^8 v b+ R$ r, @3 Odata=(String)elements.nextElement();//fetch content of the data , J9 v8 J6 @/ I8 {5 m$ a5 ?
htable1.put(key,data);//store it to htable1 2 R& X' T' V$ P7 G1 x1 x# U9 C* @5 J
num--; 2 o+ N8 d/ z7 K( v$ m
}
3 `9 M0 { ]3 n) Uhtable.clear();
# p. I4 n: w$ Q' tenter=bufin.readLine();
& x l. E& S9 K! I) Knew str14(htable1); 3 L5 ?* X& M: L/ f' P
break;
5 e$ D2 _7 b+ K. s! tcase 5:
: M9 h0 m- H5 L" Q L+ R6 MString[] sortkey=new String[count];//create a new sort array ! b6 E3 e& R4 t# L9 Y1 y5 K4 b
String[] sortdata=new String[count];
3 U( W, U# n* V; E, M' Gnum=count; 3 Y4 t5 \& z4 \ r/ M2 K
elements=htable.elements();
( z8 I, L O8 ikeys=htable.keys();
; e9 x7 t) d/ U( Y- x4 nwhile(elements.hasMoreElements())
8 ^3 D4 y) s% }; G{
1 C( B( C. z& N3 A# M) J4 `( n$ ukey=(String)keys.nextElement();//fetch value of key
8 G/ N* o9 @( s6 L" o6 f: gdata=(String)elements.nextElement();//fetch value of data ; q! n1 `5 [3 q9 z
sortkey[num-1]=key;//store the value of key to sortkey array
+ }( \/ m- C ?sortdata[num-1]=data;//store the value of data to sortdata array ( Z$ M$ ?8 j2 g
num--; ( \3 Q; O7 g8 I0 F! K; E
}
; ]- e P* @: y$ P' F& D% P' USystem.out.println("
, Z/ ^$ r6 j4 ^编号"+" "+"内容"); ! j# R' F2 K+ H, W5 E8 U( {
for(int i=0;i<count;i++)//display all content after sorting
; C9 u3 u3 ]2 S' cSystem.out.println(" "+sortkey+" "+sortdata); : M! p" H- T0 L, U$ U
System.out.print("
% S, v2 }4 P% J% ~5 L& Z目前共有"+count+"笔数据"); $ O: Z# d) O% _$ ` ~$ c
System.out.print("
7 {0 f5 L2 [2 o& @ d+ h: o. q* E$ t' D
按任意键继续...");
3 r) h: t* P4 L H/ S$ c! Y' eenter=bufin.readLine();
: g: u: L$ ?9 u0 qnew str14(htable);
( S. s* l8 g. t3 Y: Y n& Gbreak;
( [! n% l- G6 Wdefault: 5 I. G- o; j" I, A/ k' T3 ]
} ' G# C/ k3 o5 D$ w! C' a
} s( y% x0 Y+ a6 U+ z( a+ X% R
+ z1 q# z" u, S4 ?! F6 q8 C; mpublic int select() throws IOException//method of getting a function selector
6 D: z& ~* S: H+ L% N' p8 L{
, c; j8 ^8 v1 t$ y* `' ZString input; 9 R/ i! y' `6 x
int value=0;
$ L ]. ?5 E |& S) @: D4 ginput=bufin.readLine();//read a input from keyboard " `; Y3 P9 h- w) ^3 `2 K# M
try 9 ?) f# d4 d' O: \2 ]5 J# X
{ . L* C* s# m% O
value=Integer.parseInt(input);//convert a string to a int value . v% f: }4 I2 S0 ~
}catch(NumberFormatException e)//can’t vonverted
- O0 _: V/ P3 W' ^6 E) A{ ! c- x) J+ \% A3 X0 Z5 O2 n
System.out.print("请输入选项1~6:"); % h, Q. U& m; ]0 p0 G5 c
value=select();
( _- Q0 J6 ]" Q& ? `9 \}
5 U5 Y2 p1 ^; x/ }if(value>6||value<1)//if exceed then print a message and reenter
. @3 e" W2 g( {# u5 l" ?* F{ 5 f) b) O) u& C k$ U' X1 @# a6 _. \5 m
System.out.print("请输入选项1~6:");
% B% g2 C6 T8 f9 ^" H* {1 Avalue=select();
7 U( r- E, J. j4 ] I} + H1 |0 D8 S' [" G
return value;//return a value & M9 P8 R2 c- H2 v3 N# I1 w
} 0 ?' T/ v" p T7 i% u% w
; O7 S! x" [5 G6 {6 {! q# D6 L
public int getid(int count)throws IOException//a method of return the number of data
7 |; H. X7 F7 E5 p7 G{ - \( r" L% H) R" s3 G
String input; - T5 u% n8 R2 s+ A- z1 Q
int value=0;
- m2 }2 X; F* m" I Sinput=bufin.readLine();//read a user input string from keyboard
- `3 V) d0 n0 l1 Ktry
7 J5 Q3 Z( U! h+ N6 l# {3 ?{ . F4 G* [) Q4 C% [6 i
value=Integer.parseInt(input);//convert the string to a int
1 i" f" c$ z4 B}catch(NumberFormatException e)//if can’t convert to a integer then reenter
) k B! A' ~9 R5 ~+ T4 V2 g7 H+ R5 L{ V, N' n" R+ \( R' O9 y1 C* [" Y
System.out.print("请输入数据编号:");
9 V8 L- d3 R E- f3 k% i e! @- Rvalue=getid(count); * b) ~, o; [) _1 h9 A; [+ @% `$ k
} " g5 U) J7 N: g: l+ v+ V
if(value>count)//the input value is out of bound : @ X1 }, L& n" F
{
% m* M3 I0 i" KSystem.out.print("无此编号的数据,请重新输入:");
" i9 `, Y8 v' Bgetid(count);
! S% b9 [# E V' [$ W}
7 T' ^# D! w" `) n' w7 Qreturn value;//return a value 6 ~ g5 z) t# r P
}
& C3 c5 K( G8 j' g6 v} |
zan
|