- 在线时间
- 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.*; 7 u! _; p W$ G9 f
import java.util.*; 4 [% B, X* t" j* L. z( L0 ` A
class str14 $ L6 ]& g( s8 f- R; f
{ ( |' [3 S4 j$ D! @0 U* q# w& i* w
InputStreamReader stdin=new InputStreamReader(System.in);
7 K" D8 [( g2 lBufferedReader bufin=new BufferedReader(stdin); ( n0 W# ?8 y0 ~. @8 ]. i
3 F& @) [$ J, q! K3 Bpublic static void main(String args[]) throws IOException
9 c8 k5 i' S$ C{ 4 C4 q# x7 P* }0 @ f
new str14();//run the application ' v# W1 u* M8 r4 F
} 6 W* d2 D0 M& @: A0 d
6 o9 {+ p" t: A0 M* I# opublic str14() throws IOException//constructor
+ i0 D' E7 c% B; f" Q7 X+ z{ / V S$ B( z( b: v7 P; v
Hashtable htable=new Hashtable(20,0.75F); 3 J: w( i* P* ?3 n$ C
dataBase(htable); , @9 D' c' o$ J* z
} ' N" Q: t5 g" b
]2 w3 S; t6 c& A: z" l; Y
public str14(Hashtable htable) throws IOException //override the constructor
5 F( N# J4 r% g# j K{
9 K. A- y9 [- y9 C& |$ {3 `dataBase(htable); & J, V) F' g u7 q/ w; e
} x& h U; a" I8 e( ~- _
: N) o8 x, X2 _0 o0 Z' _/ L$ wpublic void dataBase(Hashtable htable) throws IOException
2 r6 v% x6 \8 U2 B$ |; @/ |{
+ X+ B+ O' e. y* l7 _8 bint count=htable.size();//get the ammount of the data in htable C4 f6 U& \4 _# H, W h
int value,id,num;
: w1 \" l( k- a1 s8 \String key,enter,data;
& ]6 |# e/ H0 a! e( c4 DEnumeration keys=htable.keys();//get all the key in htable : F# J _% W. q( D
Enumeration elements=htable.elements();//get all the elements in htable - b) K+ t, ~- T: m
System.out.println("& H$ T* D' H9 Z- @1 T1 n
Hashtable 简易数据库程序9 t7 {" r0 O& T9 L) Y5 l
");
! q: f, h! ^2 S% y7 h3 q! ISystem.out.println("(1)输入数据"); . |+ B! F4 n* U% R8 A& Y
System.out.println("(2)请除所有数据");
: J3 Q9 q& z1 O+ S$ r/ ^0 qSystem.out.println("(3)显示单笔数据"); ! T# m% B* t! r& c
System.out.println("(4)删除单笔数据"); , {$ Q$ I% n2 M1 u+ @5 j
System.out.println("(5)显示所有数据"); 6 |& r4 r. @5 [( w' p5 ]
System.out.println("(6)结束程序"); & X6 m, j3 u( u0 l. M$ r, P N9 F2 i
System.out.print("请输入您的选择:"); 6 S: s& h [& F0 k
value=select();//call select(),return the function number
- q) K# K) ~7 l5 J/ L' I9 w3 @switch(value)//the function
, P$ f! E# _8 r6 n4 x+ ?% \/ H{ 6 z! h( y' p. b
case 1:
1 a* R O2 y' U' w' QSystem.out.print("
, E7 B6 x- I7 V f9 B0 F请输入一笔数据:");//need data input
) W. ?# i, ^) Q% E0 n4 |* A, _data=bufin.readLine();
0 T3 ^5 g i3 i# Xcount++; , V. }" x7 k0 N. ^
key=String.valueOf(count); / f C& N5 L$ K+ o
htable.put(key,data);//store it to the htable / n- r4 ~0 E1 e5 s
System.out.print("6 u- U3 D* d+ P
输入完成,按任意键继续...");
, {5 q* F7 u, a6 f+ v: Ienter=bufin.readLine(); : r* @' u, S* O1 x( y/ }
new str14(htable);//reenter - a$ e4 d+ ~% `2 p' V& b
break; , T# \7 W9 @# c
case 2://clear all data from the htable
9 n! |5 ]% A3 Q8 D- ]' {htable.clear();
0 A+ n* O' ~3 ?: ~System.out.print("
0 D6 @4 l5 ~/ V* E8 s已删除了所有数据... 按任意键继续..."); 0 y; U8 E/ c, C/ k% U
enter=bufin.readLine(); , ~6 C; j- q8 [5 U
new str14(htable);//reenter
2 W, E% K! q. Obreak; 3 ^$ p7 D: M4 z" \/ `* e, Y
case 3:
& D& M% C! h! _2 d0 dSystem.out.print("
1 [% m# ~$ Q; F9 M& @4 D请输入要显示的数据编号:");
$ W! \- D% c( L0 f. Vid=getid(count);//call getid() 2 }" ^, X: U! z( W/ d
key=String.valueOf(id);
3 h) d& r/ ~% U2 }0 yObject select=htable.get(key);//fetch the data from the htable ( j) O( U( s: ?4 I1 n( t1 r
data=select.toString(); & x1 n4 U! Y3 o! g
System.out.print("
3 O# Y- z: f: y' `% f编号"+" "+"内容");
; o$ z: n( a% e7 M' zSystem.out.println("
7 y5 f5 i+ K4 ?; f6 p "+key+" "+data);//display the data 5 }# z5 d& _. _
System.out.print("
m/ S/ T1 L6 C, K按任意键继续..."); , \5 N" ] T! \* Y1 c% f$ J
enter=bufin.readLine();
8 D& P) O+ v# F* Enew str14(htable);//reenter j& N S* H" y D" G
break; 7 E3 E. X+ z. E# H- V7 f/ V b
case 4:
( \" C' V, `4 J% {# |: u' w% kSystem.out.print("请输入要删除的数据编号:");
& @0 _4 u. u- q; p+ |& u# s8 uid=getid(count); " H- |. M1 n1 _4 G- V" z
key=String.valueOf(id);
% `) t" V B4 j/ ^, ~, I, khtable.remove(key);//remove data
# [' D$ S; |+ u! A. W1 h: gcount--;
2 C- F3 X& p) B$ N7 hnum=count;
" s+ y' |8 w9 T8 x' u: lSystem.out.print("已删除了所选择的数据...按任意见继续..."); & K C5 c* g4 ?( f& k# J3 I
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
, Q1 @# g, s( Z" @9 X, i _ m* Selements=htable.elements();//fetch all the data from htable * U) Y' u/ L/ K# y+ j: c; J
while(elements.hasMoreElements()) Y' W+ c: f6 [2 I. O
{ ! T! h4 s$ s9 c
key=String.valueOf(num);//a new value 0 [2 X: j/ Q A1 C# F* ^
data=(String)elements.nextElement();//fetch content of the data
% {# o. v# n& a( K6 w. ^htable1.put(key,data);//store it to htable1
" C4 H5 Y: u' O; ?! Unum--;
. x/ J) w$ U& n3 t0 O. }3 e}
K$ O4 x% k3 T5 P* Q" C, Fhtable.clear();
, g" q4 @7 i4 ]" P( p; Xenter=bufin.readLine(); $ l* [7 r0 y$ Y3 z# G
new str14(htable1); 9 b% z% A2 s$ y" X
break;
* I7 x+ W; h t9 _) s' m- y; o; z9 wcase 5: 1 z' B$ I" [$ [7 F- {1 T
String[] sortkey=new String[count];//create a new sort array
5 |; `/ C8 P4 C% S- D0 j: K/ w; E# R6 UString[] sortdata=new String[count];
! S0 `. e5 F4 k3 Tnum=count;
: e" K$ x; N" G, _5 E; Qelements=htable.elements();
" |& g1 v7 Y% K( z0 d- Hkeys=htable.keys(); " k. D7 G. u! v8 U" @% M
while(elements.hasMoreElements())
& b% x: S5 h. C% }2 Z' ~( v{ ' U2 `; {" C- T: |
key=(String)keys.nextElement();//fetch value of key 3 m0 G* H+ J; U; ^& a
data=(String)elements.nextElement();//fetch value of data # H! Y6 h( C8 Y M. D( W
sortkey[num-1]=key;//store the value of key to sortkey array
& {, e5 ?9 f: ~5 O+ d' fsortdata[num-1]=data;//store the value of data to sortdata array
: w" J a. h1 W" a, `" b: {4 Qnum--; + V3 I% ^1 ]4 l3 v# F, ~9 B
}
% D, W2 [7 Q$ U3 jSystem.out.println("
0 }' |% D; q: w9 x. b- p8 Z编号"+" "+"内容");
; S6 V& p' W" m( dfor(int i=0;i<count;i++)//display all content after sorting
F9 }" Z/ H; t! ZSystem.out.println(" "+sortkey+" "+sortdata); : S G' j' x: \) R, C. `
System.out.print("
; T% N! F( f/ V7 I目前共有"+count+"笔数据");
. X4 E& ]% R& f5 D; c$ pSystem.out.print("# I; A5 i4 \. c$ m) h# e3 U
/ b; c, Z7 ?, Z5 P, b2 V9 D0 s! B按任意键继续...");
# P6 s' X' y+ T ]enter=bufin.readLine();
4 U% N( I" o* U* snew str14(htable); ( e3 h, z m, X/ C x
break; $ t. v) m' V0 `. _6 q
default:
" b5 U: D- F) ^4 a} : o# u# v5 n6 c m$ P; w
}
& ]* s( _) W+ m& ^2 _. B) E5 I# Q& f0 V
public int select() throws IOException//method of getting a function selector
! t5 P; D+ R" ~! {1 A' ?{
0 a, [+ @& \: O/ V4 iString input;
, }7 F5 ^# |2 s" s+ \int value=0;
2 k% q {+ d8 ]9 B2 a8 U# Iinput=bufin.readLine();//read a input from keyboard 0 ^0 W- e4 T7 s; B" I: L
try * o; S- e" R% F: }8 H
{ + j( x0 ^! P8 ]$ p* S
value=Integer.parseInt(input);//convert a string to a int value
% T4 \6 _, L# b}catch(NumberFormatException e)//can’t vonverted
: U/ Q& l9 H1 V/ x( |{
2 N& g1 M8 ~5 C: r" w& y( }$ mSystem.out.print("请输入选项1~6:");
3 e' Y7 w8 Z% f! W7 O0 A3 q0 v/ ?$ O, evalue=select(); ; q1 E; h; }5 J3 s/ ^, }9 W. G
}
# x! H0 h) c& t1 E. Qif(value>6||value<1)//if exceed then print a message and reenter
0 G( V/ w& T9 H3 s{ 5 m; @2 \% G! o0 m
System.out.print("请输入选项1~6:");
7 u9 ^7 c; o* J# d- I2 Gvalue=select(); 5 d) A) }% U f- K+ F" p$ ~
}
8 e" k5 r8 y" B- z- k5 greturn value;//return a value
. t$ ~+ _/ t9 n7 F0 Y2 [}
! }/ a, Q# r; Z7 y$ C- ]. P' {% K: E' o
public int getid(int count)throws IOException//a method of return the number of data & t7 j! E% W4 }! {5 J
{
# t, N+ s* a1 S! `String input; ) G2 r+ B( g3 w/ Z- C% b7 V
int value=0; 4 p9 }) t3 M. k0 U- {7 N
input=bufin.readLine();//read a user input string from keyboard
- ?/ I3 _3 p/ n' M w6 N1 Ttry ( D, g0 G9 p; X6 w2 s2 C3 P3 O4 A
{
# D# z7 j: c8 W4 ^7 q; bvalue=Integer.parseInt(input);//convert the string to a int 5 M, J6 E4 [' t* p3 W8 Q* v
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
' M [4 a# V6 F4 }{ " w/ Z4 Z2 b2 r4 E& T
System.out.print("请输入数据编号:"); 8 @+ D& z. f1 P/ l& J3 J
value=getid(count); ! r. p% W% D7 c
} ' g" A! Y; o; l% n9 I
if(value>count)//the input value is out of bound
$ y* {( [9 L* u% c" y4 b9 H+ h% Y3 q{
7 B! K2 y5 u6 B& y6 r; sSystem.out.print("无此编号的数据,请重新输入:");
8 Y# ]$ W/ C* O1 k: jgetid(count);
" K! z3 w5 s+ ~8 d6 t} % W/ e" I V6 N% o6 O. Y4 M
return value;//return a value 4 I" E9 m1 [1 W1 m: t
} " ]5 R. c+ X9 y
} |
zan
|