- 在线时间
- 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.*; & K% ?: G4 M7 `
import java.util.*; $ l( p! {2 F2 Q7 O0 q5 o
class str14
( `" `0 {3 ]/ D/ C! H3 Q' m- t2 r{
$ a) @9 H4 f! }' K: YInputStreamReader stdin=new InputStreamReader(System.in);
4 U$ T8 d5 |8 a# C" a4 i: L8 [3 wBufferedReader bufin=new BufferedReader(stdin);
" o$ K8 L" I; X5 t
, H$ `; j$ d( f7 |) ?- Wpublic static void main(String args[]) throws IOException
. ]2 M) D; X$ Y{
% V! y) T. @* ] N, e/ cnew str14();//run the application
1 W0 x8 m1 [ L+ P. f} 4 u2 Y. I3 p7 A, S/ m/ q
+ ^" x0 l! m e
public str14() throws IOException//constructor
* q! L9 d! t' F2 a6 a0 Y8 ]{ 4 s% f9 N! b- N, C7 D1 I
Hashtable htable=new Hashtable(20,0.75F); * \) q7 W: y3 s
dataBase(htable);
# ?6 _/ a8 t$ b: u0 Z# _}
; s2 T9 f o; n* P
6 @4 ?& q7 e, S0 H: ]8 E4 }7 zpublic str14(Hashtable htable) throws IOException //override the constructor 2 A) R4 T, Y6 s' e, D
{
: E- M8 H( p0 ]; k% p# M3 `" e5 IdataBase(htable);
& l8 Q# B% b5 U; I1 N# D% r}
! b( e8 W0 J7 a- b$ ~! O& b/ j, a2 P, a: H0 x) `1 Q3 g
public void dataBase(Hashtable htable) throws IOException * U. y# S+ ~2 ^2 p1 G
{
1 }/ G+ e; F( `* L, Vint count=htable.size();//get the ammount of the data in htable 7 I+ i% j r- R+ t2 [% h1 W" t
int value,id,num;
9 b D" Q- O! t) W* d9 O/ W3 zString key,enter,data; ; V3 ~- Q6 u8 w' `/ V& |1 E- C' }7 J
Enumeration keys=htable.keys();//get all the key in htable
3 U H$ J% }% b ]: e/ IEnumeration elements=htable.elements();//get all the elements in htable
& q n1 P- j# q x! `. h% dSystem.out.println("1 j& M0 F j) S$ C
Hashtable 简易数据库程序0 C) i+ H5 V/ _ e$ V+ C9 s
"); 3 U/ Y4 N* d, u, k$ {6 |) {5 p
System.out.println("(1)输入数据"); & |4 [- \+ N( w, U( |( E; p
System.out.println("(2)请除所有数据");
: B3 P# _1 I/ J/ HSystem.out.println("(3)显示单笔数据");
$ u, {& i; e. @% }: v$ R: v! dSystem.out.println("(4)删除单笔数据"); 7 }6 k2 ~8 b; u) e
System.out.println("(5)显示所有数据");
& f% k4 i3 m; a' x: i( PSystem.out.println("(6)结束程序"); 1 l0 ^, X) Z) n G5 |
System.out.print("请输入您的选择:");
) L1 j, t! j* L- [value=select();//call select(),return the function number , {! i$ L" s$ {' i1 a, m) p3 y
switch(value)//the function
9 f- w" Y2 v9 b! K, S1 S{
2 X' k* x' f* R d/ x- W% g6 j+ scase 1: - t: d# }) c G. W, R! [
System.out.print("
2 Z- v) M* w' h) M" B' n) [请输入一笔数据:");//need data input
- N: ]: @: t& L' e9 y W( `9 E7 \8 Ydata=bufin.readLine();
* h1 k: _- Z- H! ^- u, t: kcount++; 7 `8 b U: n& Y. J3 P) ]0 P: H' z
key=String.valueOf(count);
" N2 p0 S6 x( P' }6 Hhtable.put(key,data);//store it to the htable - {# k; s( t, ~/ o3 j3 K
System.out.print("
. M @2 H$ {& L( ^) u输入完成,按任意键继续...");
2 H6 b) R: O5 }- k* U6 [+ ]" benter=bufin.readLine();
P& W4 y& s+ G6 Hnew str14(htable);//reenter 1 V) U1 ?8 v+ w$ J, c$ O
break;
* k5 ^+ J7 k) N( B& j9 Lcase 2://clear all data from the htable
/ f$ z$ N5 g4 L7 fhtable.clear();
; ?9 @' ]% f; Z! L( I% I/ ZSystem.out.print(" Y' f# j% X; q$ ^2 t2 }6 @8 v
已删除了所有数据... 按任意键继续...");
" \5 b3 w# L5 m8 v T0 wenter=bufin.readLine(); 5 }& G: J7 b8 A$ M9 _
new str14(htable);//reenter
# ~ E/ ], Z3 Lbreak; 5 y K4 F( J$ R) I- e5 E( _' V
case 3: 2 ^2 P: ]; b$ K- b
System.out.print("* v: u: e7 `) G7 l% A4 c/ q
请输入要显示的数据编号:"); 7 v- l1 ?: H: Z. q& h
id=getid(count);//call getid()
; h; g8 @0 V1 E& D- F+ o6 K5 Mkey=String.valueOf(id); 5 t' m0 P; j$ w x
Object select=htable.get(key);//fetch the data from the htable
5 z: }( Q# T( K/ tdata=select.toString(); & G1 u+ N: j2 t8 d, a
System.out.print("
( x: B0 D0 ]! \8 U, }3 {编号"+" "+"内容");
7 z) ?7 b7 X6 J Y1 OSystem.out.println("
8 T4 k' Q4 l7 d) I Z& I "+key+" "+data);//display the data
" j6 \: _) h( lSystem.out.print("
8 L- t0 E E7 C- u& Y3 F1 S按任意键继续...");
1 Y) W. X, ?0 O0 c; ]# w/ B/ N: tenter=bufin.readLine(); & Z5 U# r% z C" C
new str14(htable);//reenter + t+ J* G& B0 c" M( R- O" k
break;
' ^7 X2 f) u3 d+ d0 rcase 4: 2 x# W7 ~3 M0 X0 Z
System.out.print("请输入要删除的数据编号:"); 9 C* D5 h8 u6 h) R- Y! g; h
id=getid(count);
/ P E2 N, j' Q/ ?4 i4 O5 nkey=String.valueOf(id);
3 d5 E$ i" D( }" r7 r0 Ihtable.remove(key);//remove data ' W! x! w! _: h0 m; R
count--;
6 f. }1 O* `) ^2 R- E7 `7 I" v @num=count; - I! U$ {) S0 x4 w, ~
System.out.print("已删除了所选择的数据...按任意见继续...");
e# L' Q4 C2 V. Z& iHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
* o9 x) d4 B1 n/ T4 p- ]elements=htable.elements();//fetch all the data from htable
3 K# i3 |" W0 T1 Q+ H# rwhile(elements.hasMoreElements())
9 |, q! Y V# ?' o( |0 x! Q{ # W+ C A: S& P
key=String.valueOf(num);//a new value
$ o# z6 v& l% h% u, {1 l8 Edata=(String)elements.nextElement();//fetch content of the data / `/ w9 E2 ~! n9 `7 V( ]/ H/ c
htable1.put(key,data);//store it to htable1 + {6 _; A! e0 N: e, p
num--; / X* E1 o6 O+ w) e5 ~0 X' [& ~- I
} ; C, V, c2 v i4 }( H9 x$ [
htable.clear(); / X- d; x- \5 {7 H8 e0 \
enter=bufin.readLine();
; P: C3 c1 M( ?- z' ~+ qnew str14(htable1);
% I$ w2 c! H$ jbreak;
" P3 w1 k( t6 m$ y# D4 y6 Lcase 5:
$ ~' G) o5 I1 @+ B/ hString[] sortkey=new String[count];//create a new sort array ' Z2 w1 ~6 M1 B+ H' L: E* W W, q) J% _7 S
String[] sortdata=new String[count];
" m! T$ o, T, d9 W' Y7 o' {num=count;
& U2 X! I( \, E5 u$ r% V$ nelements=htable.elements();
* j; [- a7 [- C h# M9 a/ X/ skeys=htable.keys(); 3 z. ^& [# u4 r0 S6 b0 }! f& Y: n
while(elements.hasMoreElements())
0 Q: l+ L- i3 }6 N8 s8 B% `6 K: {{ " A+ M, U# Y7 O g! R1 E
key=(String)keys.nextElement();//fetch value of key
0 q8 O1 Z1 _" L8 Gdata=(String)elements.nextElement();//fetch value of data
6 M; ]( l( Q% Msortkey[num-1]=key;//store the value of key to sortkey array , z6 I5 x( L7 N
sortdata[num-1]=data;//store the value of data to sortdata array 3 h( h9 n- J& z2 p' F
num--; 8 s2 ^7 O- ~' x* Z
}
# g# O) _/ F! S# M6 ^* x! H. N, \1 lSystem.out.println("
4 k! K8 Z9 @% q( o( [ v8 q& d编号"+" "+"内容");
2 _6 k- f# F( R# y! P0 H/ mfor(int i=0;i<count;i++)//display all content after sorting / i3 F9 a4 S* \/ c/ Y. C
System.out.println(" "+sortkey+" "+sortdata);
9 N; {# f9 ?6 i z0 ~, NSystem.out.print("
: W: O' U9 A. h: A目前共有"+count+"笔数据"); 9 l2 e. V7 z% i: ~2 ^
System.out.print("
[% p* n6 F8 a7 S4 {) n. k8 v8 y# b0 a* M( n
按任意键继续...");
g: t1 O7 Y3 B& t. Oenter=bufin.readLine(); d' b5 c# I+ K+ C( a+ C
new str14(htable); ' P$ K( y4 {6 q8 i
break;
1 g5 K, ]+ D, D* v; xdefault: 0 Y0 a( S7 g) [( X$ M3 M6 U
} ; `% e( l1 T. ~8 a
}
5 e: u9 x: R, _/ [1 w6 z" u) `5 @+ a5 _, g+ v4 V. }
public int select() throws IOException//method of getting a function selector
# t7 {5 a: V6 I: t1 T$ P{ p( y. |6 z b5 h0 ^4 ^
String input;
5 U2 }/ f1 l T* `1 V# ?, Nint value=0; 3 \5 p0 q! T" j: b/ ~9 p# K
input=bufin.readLine();//read a input from keyboard
3 n: y$ d& \: f% ttry
9 s6 n4 V8 ?5 g7 m{ + Y# L: O N. g3 |8 S
value=Integer.parseInt(input);//convert a string to a int value
1 T/ r2 a1 m0 D}catch(NumberFormatException e)//can’t vonverted
8 {, W; x# N9 B# D{ ) b) M' O% k+ u5 B. V# e, V
System.out.print("请输入选项1~6:"); ! G4 a$ C& u$ n4 h) l' K n/ m
value=select(); ! K# _" p. u* [, Z6 f
}
# i" @, v7 I5 [% @if(value>6||value<1)//if exceed then print a message and reenter ; @' b7 U$ e/ _0 v
{ 0 C3 B: F' D. X7 Y2 X: d! r
System.out.print("请输入选项1~6:");
7 q& e* h9 @* a- s0 y3 y2 g7 ^! hvalue=select(); . s& [2 W. [: ~* `* |1 Q: q2 F
} ( Q5 D6 J! R3 M) s8 ~, g; q
return value;//return a value , [. P2 ~3 _- o
} ! o! k# Z0 [( f. [/ [
3 Z+ B8 l+ ~& b7 f6 g; {- C
public int getid(int count)throws IOException//a method of return the number of data 2 s) a& o6 f. q$ }7 ~" s( W! M
{ - I: l0 t/ B" p1 n, w% `- o! o% J
String input;
' t& @- Y' x( `- o3 i6 r* v6 j' aint value=0;
7 C: P0 b+ [ einput=bufin.readLine();//read a user input string from keyboard
# D" m$ i( g9 |: O# y7 [try
! G% E7 q; n; J" _{
" e: N( w2 V) ?0 F0 Ovalue=Integer.parseInt(input);//convert the string to a int
@, W$ j+ B8 x1 @. V}catch(NumberFormatException e)//if can’t convert to a integer then reenter
( s/ G" ?- z* J" @{
# z2 c) g2 r) QSystem.out.print("请输入数据编号:");
. E" \% u% B3 S2 x! Svalue=getid(count);
% l @5 L) u- y" w, g2 S}
5 t# F2 l# ~3 u! M C+ lif(value>count)//the input value is out of bound / O$ O2 ?% N6 `
{
3 W8 m# Y3 X2 | J- FSystem.out.print("无此编号的数据,请重新输入:"); % W7 G( s+ f7 E. W
getid(count); # N0 d- x1 B' d9 f$ c: }4 }7 m% g
}
6 U- \ q$ A) J breturn value;//return a value
, t. ~( S4 R, d% Y0 `} # x0 m6 K7 J' c# g6 z
} |
zan
|