- 在线时间
- 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.*; 0 }: t) {' @: v1 S! E3 ?8 C
import java.util.*; ! u( o6 H; Z9 F* B1 m) O
class str14 8 f: O& s3 d) U( b; Z& Q
{ ) x5 Q& d" E# q) |2 j6 E* ~/ T5 ^
InputStreamReader stdin=new InputStreamReader(System.in); / u$ \+ X; d! h1 M
BufferedReader bufin=new BufferedReader(stdin);
" q, r7 r7 i9 d: P q' x, C# i7 Q8 C# A6 N# s8 B1 O
public static void main(String args[]) throws IOException
: W, z f" t" ]/ T{
3 K1 L- N, N4 ^; N4 g' Snew str14();//run the application
( Z9 a0 p4 D- q; W' c}
0 u% B3 s# o. J7 f9 f' N9 r
0 I' |$ m5 W- Q Q' y1 Mpublic str14() throws IOException//constructor
* `( q/ y! U. p- I1 ` }{
* z: F$ @ k* xHashtable htable=new Hashtable(20,0.75F); ! d) y# z0 v; }0 K9 M* J# t; p* ^
dataBase(htable);
" l3 l+ M2 k" @* u- n} + ]* e# ]% v2 F$ a" y
+ v& M' r1 C1 Y. b* V6 h! d8 l' Jpublic str14(Hashtable htable) throws IOException //override the constructor 0 O% V8 G# d# ~' ]) z* M- \
{ 1 v! b" e# Y9 Z6 L6 d X% J) h
dataBase(htable); ) q5 a/ F: `6 D8 ?! L7 x
} : o& X8 z o: }* l& X
6 P* I# C8 @! N) t& k7 v: W
public void dataBase(Hashtable htable) throws IOException ! x3 v6 W) U5 s' I. q4 Z
{ # T7 x! s; ^( P2 x
int count=htable.size();//get the ammount of the data in htable ; V/ F/ Q# m# M7 l0 z0 \$ R0 y K
int value,id,num;
" e% t& T, V; s6 R3 `$ |$ r; uString key,enter,data; # ] s1 H2 [3 m4 f3 w$ R
Enumeration keys=htable.keys();//get all the key in htable
3 S) ], f3 ^6 f5 SEnumeration elements=htable.elements();//get all the elements in htable
" I! A5 y& k; Q, QSystem.out.println("' D4 ?7 l" ^; W5 M, n3 L8 X S/ S' W
Hashtable 简易数据库程序: m& d* O- p$ f) S% X4 A4 B# j
");
& O) h% Y& J0 U+ uSystem.out.println("(1)输入数据"); & H: ]; z4 O/ X$ W8 S0 O" Y4 C
System.out.println("(2)请除所有数据");
# o, _+ R- ^, h/ k, q; gSystem.out.println("(3)显示单笔数据");
0 V. }8 ^+ F, |$ u3 M! dSystem.out.println("(4)删除单笔数据");
1 Q& m; U) M p3 p3 kSystem.out.println("(5)显示所有数据"); 0 s _; C: T6 F! U
System.out.println("(6)结束程序"); }% g' X4 \' ~4 t u, y
System.out.print("请输入您的选择:");
/ Y+ F% R% E$ ^# a2 L- F. [value=select();//call select(),return the function number
4 l3 }" h# E/ y$ A& Wswitch(value)//the function % Q' s1 s4 j7 m2 ]7 [
{
5 M4 K5 G. I. X& ~7 C2 q i% scase 1:
9 x: M4 ^& t8 o5 h) p; f- u3 q: aSystem.out.print("
$ E6 l. q8 N& Y, B4 z9 z- Y: ~请输入一笔数据:");//need data input 9 H: d: O. r; J% e& b6 o& n3 f
data=bufin.readLine();
/ r! f" q) D) xcount++;
7 ?8 c% m. l& ^5 |2 X. M) Lkey=String.valueOf(count);
8 L& z( }0 y- I8 }' o2 F% t, chtable.put(key,data);//store it to the htable 5 t; O; v$ J3 L# F5 J2 P
System.out.print("
6 [1 M- c* Y( D1 [# ~, h( ?3 N输入完成,按任意键继续...");
& T. W5 j; |8 n* Tenter=bufin.readLine();
L: O9 a5 p6 B0 `new str14(htable);//reenter # r: z4 x2 X0 c( J% R8 @. e
break;
$ U7 g) O1 I9 |3 \1 o$ S, m3 l' \case 2://clear all data from the htable 1 }* H! m. x8 }, z
htable.clear();
* E9 `; B; r1 ]3 I$ K, P: E0 oSystem.out.print("
. h7 C6 t: X4 o: o4 O已删除了所有数据... 按任意键继续...");
1 @9 s% K' }- U# ^4 ?! {6 wenter=bufin.readLine(); + l4 D. [: T1 g: W/ m$ t% o! H6 M. K" ~
new str14(htable);//reenter
0 z! {5 h! W" |6 N1 obreak; , K, B- ~# Q- A2 _! t( R" c
case 3: 0 X* N$ T' k) H4 ?* l
System.out.print("
; z! I3 E: n$ e. g请输入要显示的数据编号:");
5 g' E4 k+ [9 ]! |id=getid(count);//call getid()
" p, q1 R5 t2 ~key=String.valueOf(id);
: t. M7 \$ [3 s1 a4 NObject select=htable.get(key);//fetch the data from the htable
% O( C) ~1 x: edata=select.toString(); 1 r4 U1 T- J" j* f) b4 \
System.out.print("
7 f2 ?( S# m9 I. Z) V+ \编号"+" "+"内容"); 1 F7 M5 m9 t2 l/ _1 C
System.out.println("
. N. e5 L* i4 l9 j4 H3 T8 `" L "+key+" "+data);//display the data $ H; Q+ n, o" l9 W, s. E
System.out.print("
7 I1 I- r" Q# P' ]按任意键继续...");
% y2 R+ P( o/ g8 ^* venter=bufin.readLine();
( m- c, `7 M3 ?8 Hnew str14(htable);//reenter
5 l- @- F1 j! [8 @. Abreak;
1 T# ]8 m3 D4 m9 Hcase 4:
& O+ R/ j0 {% G5 V: @: zSystem.out.print("请输入要删除的数据编号:");
2 V- n. ?/ H8 oid=getid(count); 5 O: _$ t8 C# ~
key=String.valueOf(id); " ]7 m; M1 _2 `6 D' H# H! n. e: ?
htable.remove(key);//remove data
* m* w# R8 E Qcount--; 9 k; _! \. A, x4 d* V( Y* y
num=count;
9 i9 g/ p" ^: aSystem.out.print("已删除了所选择的数据...按任意见继续..."); 0 V5 `1 x" C3 [: N- ?! m2 c4 l
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 ; E/ y V# ?# q4 \
elements=htable.elements();//fetch all the data from htable 0 b3 p8 c- `" y, S1 j S8 h" A2 a
while(elements.hasMoreElements()) 6 L5 t1 J5 U* `/ c" S
{ 8 [, }) E4 Z& @& v7 y
key=String.valueOf(num);//a new value
0 j8 e( B: N* }! j+ Ndata=(String)elements.nextElement();//fetch content of the data
) f d* A7 L5 _" b2 j6 shtable1.put(key,data);//store it to htable1
d( y" K5 Q& B c. qnum--; * a2 J; v+ l f0 ~$ s
}
' T J' p* F7 l7 w& ~- [, e4 I0 z: L# hhtable.clear(); $ `' q+ q) c* m- X
enter=bufin.readLine(); $ f4 y% z% g5 ~# l& u
new str14(htable1); , [* D# J4 l* p) f& K+ k! H
break; 5 T, ]" P, N- {( m) o; L3 X$ F' f
case 5:
- O6 g# a3 B+ B2 s [: ZString[] sortkey=new String[count];//create a new sort array
$ h1 f1 V8 W$ d: s7 o DString[] sortdata=new String[count]; ' g# V* ]! H$ h9 k2 y: z
num=count;
/ k8 Z9 Y* p# }3 yelements=htable.elements();
' n! W6 d5 X2 s! H; Pkeys=htable.keys(); % I; u8 t7 j0 d
while(elements.hasMoreElements()) ; V' ] |3 v1 k) N9 P7 y6 [: |# ^
{
) C n0 h6 n9 bkey=(String)keys.nextElement();//fetch value of key
8 S% {6 {6 F1 j. N. K: e3 ?data=(String)elements.nextElement();//fetch value of data ) [1 `* j5 w$ z
sortkey[num-1]=key;//store the value of key to sortkey array ; J" K# n3 Y: R/ n7 G
sortdata[num-1]=data;//store the value of data to sortdata array ( P: N4 b- O5 U3 s
num--; ' ^3 \) w* v) F, g) }. K7 c2 D
} 8 B# V: H% T# H# B0 M
System.out.println("/ y7 }& {$ b( U) \. S
编号"+" "+"内容"); * q) W8 C& i) ^9 J1 L
for(int i=0;i<count;i++)//display all content after sorting
# e6 O7 r1 l/ s9 @- r5 S$ k/ PSystem.out.println(" "+sortkey+" "+sortdata);
. o. Z# p7 t1 D# ?% E9 @3 {System.out.print("6 J- o& |9 q8 {3 \/ g
目前共有"+count+"笔数据"); & S8 p) r* n3 M
System.out.print("
( W1 K. R# l4 @ K q4 T1 M+ n! M7 P# f7 ~ \& {* n9 j R
按任意键继续...");
0 j6 S9 E$ ]! G" w, eenter=bufin.readLine(); 7 `3 |& \- w9 l) ~1 Z
new str14(htable); 5 X. n' Q0 i% O0 [8 D! N J
break;
$ q1 E# ]! `- w: H- O" vdefault:
4 p0 J+ p% B$ ^* P' K}
3 J9 x7 K e1 k! I}
3 ?8 Y8 `3 M( Z
- {% M' w! @/ R8 U* r4 V; T& fpublic int select() throws IOException//method of getting a function selector
7 E) S1 t0 A8 V0 T. w% L% H{
$ }0 g5 H! t! a( oString input;
3 R! j% t' }/ b9 kint value=0; + j6 @9 j4 Z7 M7 d
input=bufin.readLine();//read a input from keyboard 0 V( u9 }/ H9 A: V, _( [% n
try
+ r, z n: s- B% Z{ G! k J- s+ P. a! I
value=Integer.parseInt(input);//convert a string to a int value + A! M$ K4 g$ ?+ E4 r
}catch(NumberFormatException e)//can’t vonverted
8 M# t6 ?$ l* A) j* W2 ^* z+ ~0 i{
9 A1 Y$ _% w; E2 V- u# oSystem.out.print("请输入选项1~6:");
& L+ ~2 W" S/ }8 C" rvalue=select(); - s# {( @6 ~) L9 l: M5 T$ g3 `2 A
}
+ r: v% Z! V# L9 s Pif(value>6||value<1)//if exceed then print a message and reenter
$ N1 z5 X$ b. I, p+ T{ % e: E o. B) f# {3 y! f$ o. z
System.out.print("请输入选项1~6:");
4 G& q3 s. X6 M" Q) Wvalue=select(); . F+ |5 S) ~5 d% a! ~% `3 j6 n
}
' P# u+ t5 D, F3 Dreturn value;//return a value
( G! f7 G6 N7 P: ~# s N" y7 ~- }}
( Q+ I% H/ k9 G( z; p
t& A' n! C( Zpublic int getid(int count)throws IOException//a method of return the number of data
+ _6 ?2 h+ n+ s$ i# a; V{ ( }0 t$ `. @# I2 M$ B2 S% y, _
String input; 4 X/ X2 ?2 V1 W( a& q1 B
int value=0; ; S# A- I e1 ?9 N
input=bufin.readLine();//read a user input string from keyboard
! y5 \$ K1 ?" dtry ! `; i5 [4 {' G, O f' B8 r- L
{ ( u0 V2 ^4 X- l5 q# g
value=Integer.parseInt(input);//convert the string to a int
5 f) P/ @# B" |# c' I}catch(NumberFormatException e)//if can’t convert to a integer then reenter
2 g, G6 y+ ]" S4 D" j% L{
; X' l; I1 l, C3 d3 J. iSystem.out.print("请输入数据编号:"); 5 ?1 r7 w, S9 ~$ Y! A" u2 j7 W
value=getid(count);
5 L4 y# }8 }7 _* Y# X: p9 U} 3 G9 B! U" L( t
if(value>count)//the input value is out of bound 8 J- k, y+ }/ O( N9 t# M) @8 d: s
{ . ` u/ s# [) d6 Q7 W, @
System.out.print("无此编号的数据,请重新输入:"); 2 G) ]# |1 Z- X8 \( V0 O1 t$ A
getid(count);
* I9 s' a0 a$ W, t# e} ' j, @1 ]" R. r& t" Z
return value;//return a value - M/ u1 W( i. P1 W# P" L
}
' F" `4 ]2 I. |: R" n} |
zan
|