- 在线时间
- 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.*; 9 I. Y9 H& q) l, K
import java.util.*; 8 H0 D- I# W2 l$ ]' f" U
class str14 7 [6 B* X2 F$ n% Q" R: ?& K9 r6 F
{ 7 S4 e/ t! I1 G; m$ ?/ J G
InputStreamReader stdin=new InputStreamReader(System.in); 3 }7 |* H& d# Q# ~0 i- x. i4 B* |
BufferedReader bufin=new BufferedReader(stdin);
) [+ ^; g& h* c+ v0 X2 J+ Q
& P( q! r$ P$ o3 w bpublic static void main(String args[]) throws IOException : H+ ]1 M& y1 j1 g/ ?. p- x- @% q- S
{
1 X: {. X) M7 k; Y& T3 Fnew str14();//run the application
6 o5 h8 D, L0 }0 J, S9 I} . R' m* K* s# z$ l. W
. [8 W3 k& U0 Q* e5 g- E) T3 hpublic str14() throws IOException//constructor
" ^5 b+ }, g2 \6 l{ ; s7 a7 W. P& f
Hashtable htable=new Hashtable(20,0.75F);
) v2 p8 S: E q& n* udataBase(htable);
9 ^4 ?( h1 | T3 Q& }# m. B} # A, d; d9 H4 B( `- l
4 J" S: V2 S6 I& K. G0 A8 Npublic str14(Hashtable htable) throws IOException //override the constructor 0 _2 `2 j" \; f( v' p
{ % \2 O" M Y; b. Q4 {; b
dataBase(htable); + X! _4 N7 D6 K, T0 ~0 j
} ' M4 m2 `) p5 j: g9 _( v
8 l7 Q8 q( @' h" Z, ]
public void dataBase(Hashtable htable) throws IOException 1 t3 P$ P, L& ]8 }% {" @3 `
{ : n) y+ I7 R$ c5 Q" h! b
int count=htable.size();//get the ammount of the data in htable 6 ~( V5 P: f: b. N
int value,id,num; + ]$ }, R0 s* E/ z! s
String key,enter,data;
/ b: c! R8 k% A- d" r0 J, ^) FEnumeration keys=htable.keys();//get all the key in htable : b W8 ^6 P8 K
Enumeration elements=htable.elements();//get all the elements in htable
* `& _8 A% q. l) z* d, A- nSystem.out.println("
! h" O, I3 S/ M) u8 R/ \Hashtable 简易数据库程序( J' L& K1 J. \% `+ O) A* l" E
");
. K4 N# D) i4 k3 U1 QSystem.out.println("(1)输入数据");
% w6 z% l6 V0 I/ {* v. L" p3 xSystem.out.println("(2)请除所有数据"); $ k! z6 ~$ G3 o, o3 W0 `) W
System.out.println("(3)显示单笔数据");
4 ^" `' {% Q' k5 X9 h7 \4 MSystem.out.println("(4)删除单笔数据");
. o: s0 D: B4 G# ]( OSystem.out.println("(5)显示所有数据"); $ q/ z- V6 Q7 Z" k) e `& h) d
System.out.println("(6)结束程序");
; j `% r+ E8 w0 W$ lSystem.out.print("请输入您的选择:");
& g! u0 \, s6 v- Bvalue=select();//call select(),return the function number
& q; W' x& l* b+ Y+ o( {. ]switch(value)//the function 5 l6 ?0 t7 n; T( Q$ P- |+ Y+ O2 n
{
( u4 i& O8 u; m6 K# A5 y$ xcase 1: . O1 A- v5 z% S% O% f! L
System.out.print("
$ L$ k+ P5 Y2 U' y. j/ n6 O请输入一笔数据:");//need data input
# ^0 H S1 n7 y3 {" Jdata=bufin.readLine(); % D% {) C( T1 L8 v& P% B. i
count++;
% |3 y5 ?$ S5 a9 okey=String.valueOf(count); 4 X K2 m/ ~3 A6 C% N T+ y
htable.put(key,data);//store it to the htable
3 R: y. H& V8 OSystem.out.print("* u8 m5 G% G4 M' v5 o
输入完成,按任意键继续...");
3 M7 _/ n; @8 k N7 e/ b( Genter=bufin.readLine(); 2 N/ H `5 o _( o: N3 P \
new str14(htable);//reenter " X* t6 w2 C$ q8 \, L' U; Q
break;
# t: K# Z3 z# u( _4 N" z9 scase 2://clear all data from the htable
' b! C$ d: b4 _# r( I( S, K+ }& p7 N) Rhtable.clear();
/ A: _" c6 Y qSystem.out.print("# J1 L, y3 l) G( ?) ~
已删除了所有数据... 按任意键继续...");
9 w" h! ]2 J9 B7 Venter=bufin.readLine(); l! q: |9 V, \% P# _7 W
new str14(htable);//reenter / q9 K; r% o/ S" {, o9 X5 R# _
break; 6 [+ f) {3 b2 D5 P! X
case 3:
C/ s- v. V- K6 Q' K0 ISystem.out.print("
1 @3 O3 E7 c' R i+ C4 }, A请输入要显示的数据编号:");
0 X# f' H% H+ B# N/ `4 Q& Qid=getid(count);//call getid() ' A" |% W% S$ N$ [/ v
key=String.valueOf(id); 6 \. {' b$ i3 H8 i/ m+ q
Object select=htable.get(key);//fetch the data from the htable ( J$ G3 [6 |9 U3 c7 j1 P
data=select.toString(); 2 Y7 k' \9 U2 c6 c/ u; _+ d
System.out.print("
?# l3 l2 b: T2 I编号"+" "+"内容");
! R; \7 h4 A6 r* o% kSystem.out.println("
+ Y8 J3 c& w& R' [8 p "+key+" "+data);//display the data
0 F$ j7 e9 \, m/ g' Y9 S2 ZSystem.out.print("6 ]2 H3 E/ w# ^; L5 l
按任意键继续...");
, @7 S! ~8 R; Henter=bufin.readLine(); # m5 ~1 q4 i1 J% Y; H2 z
new str14(htable);//reenter
5 G& D* [' }+ K7 qbreak; 4 e# s$ ~" |7 T& g
case 4:
' M; @+ c+ o( ESystem.out.print("请输入要删除的数据编号:"); + [) R, c$ d* V/ n" Q+ ` S
id=getid(count);
! N( M* X5 B' @( e4 _+ gkey=String.valueOf(id); 1 R, \; C' ` s
htable.remove(key);//remove data
S& r T2 l" K; qcount--; ; r8 T3 F. X( T+ K! C. y
num=count;
; Y. X! P: P& S7 pSystem.out.print("已删除了所选择的数据...按任意见继续..."); 7 N% S( F5 q8 I( I# w
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 6 g n" m+ a: u9 F% ^9 m
elements=htable.elements();//fetch all the data from htable
- D* Q" S1 X8 Y% B' B9 pwhile(elements.hasMoreElements())
, u" f& g4 W6 x! h{
9 l [; a* U: ]/ T4 y4 Rkey=String.valueOf(num);//a new value " r v5 j$ N* Q- t
data=(String)elements.nextElement();//fetch content of the data Z, N+ e7 v/ m& ~" _. v7 }
htable1.put(key,data);//store it to htable1 & P' k- c/ f2 B3 v3 C
num--;
2 x8 t: I7 d+ y& r8 \$ S% T) o}
7 v+ M: J4 N" x) y. E3 \htable.clear(); & B3 V& ]' T0 ], B3 `: j- `4 ^
enter=bufin.readLine(); & L* z7 G* |0 d0 O& X, {
new str14(htable1); p- G, H: X/ }4 ~$ R- }
break; $ ?' B/ G% i5 E3 a" v7 i
case 5:
5 O; ~6 P) K3 H A4 |- zString[] sortkey=new String[count];//create a new sort array
% F7 _, o; g! ` N4 IString[] sortdata=new String[count]; # \# \2 k" R( t
num=count; : m0 ~: }6 z' s3 V3 G8 m, w0 A
elements=htable.elements(); : L6 `0 \) D& H$ a' L4 G
keys=htable.keys(); % ?4 S* ]' s( m0 y. V( V
while(elements.hasMoreElements())
& ^! ~+ @" s' S+ x{
) D7 C$ e: F" G( `+ [& Jkey=(String)keys.nextElement();//fetch value of key . ^8 W; i$ l0 B, U
data=(String)elements.nextElement();//fetch value of data
9 J+ u5 C9 M: Usortkey[num-1]=key;//store the value of key to sortkey array
0 G6 C, L: q% S: _sortdata[num-1]=data;//store the value of data to sortdata array
( E2 P6 X$ n$ c7 j1 x; ]6 nnum--;
9 n5 @7 w! f& j4 q2 q5 f} 7 Y( k( `3 B3 D1 ^$ }
System.out.println("8 J6 p5 H( u, Z* m& D; e, @
编号"+" "+"内容"); ) z2 D. |; U. ^+ z0 i, }# @
for(int i=0;i<count;i++)//display all content after sorting : R9 {* \& o5 }' R) U
System.out.println(" "+sortkey+" "+sortdata); 1 k) ?8 n) A& B+ I
System.out.print("/ q, C! d1 a- N& M* r$ m
目前共有"+count+"笔数据"); a& r4 S9 I4 o, T
System.out.print("2 [. h6 h& c7 Z0 K3 c0 }
, x! Z3 q- y) E6 `1 J1 G
按任意键继续...");
# j7 @$ u, B1 ~' j# `enter=bufin.readLine(); ) B* U% O+ _5 _6 p
new str14(htable); 8 ]0 V; n8 t9 Y" ^7 e* c# _
break; , I6 F9 u; l& V7 v- {
default:
! p' V2 {. H/ ~ J) [} : r9 Z' b8 n5 V
} 7 U- {8 ~& H" C0 U0 k6 {& A5 U! J$ o3 h
3 t; S q4 T* k5 N/ B% C5 m" Tpublic int select() throws IOException//method of getting a function selector
9 j( ^9 c9 }( Z, n* L- ]. F& a{ ' a4 L" V& s* }' Q
String input; . N3 V+ l. k- r N+ V+ z
int value=0;
* `) g- }) J* k ^input=bufin.readLine();//read a input from keyboard ! r: Z8 Z7 ^0 f" r& _( M% o
try * t" _6 \- |4 Y0 L. e8 j! c
{ 7 x1 P4 s4 w3 m3 T X. a: ^
value=Integer.parseInt(input);//convert a string to a int value
% W3 V) U# Q1 r/ `" C, c, ~}catch(NumberFormatException e)//can’t vonverted
# F4 O) z, P: S: y a& F{
7 [ ^* Q6 N. LSystem.out.print("请输入选项1~6:"); 1 O9 K, ~5 L, w* L6 ^9 b+ @0 I+ r+ @
value=select();
. P+ o4 a" T2 S' i# S% J3 {) F' g7 I} 8 _1 u" O% O1 V) z3 u7 X
if(value>6||value<1)//if exceed then print a message and reenter 3 Y& ]+ q3 @% R1 N
{ + J7 [9 v* C3 ?; ?
System.out.print("请输入选项1~6:"); ' `( E# [6 {. k& T& `% M" t/ N
value=select(); 4 t- R" d5 ]" h6 f
} $ ?7 Y7 n" g4 ^
return value;//return a value 2 \; p T4 {/ ]0 C& Y7 B
}
; |4 S' c" D) C6 \
7 B( C0 w" k1 N5 ]0 R o0 v' J3 Vpublic int getid(int count)throws IOException//a method of return the number of data
- M6 p8 q" v+ z' q7 x{ & l, k% Z3 U- r$ w: z# l9 j8 t+ O
String input;
* a0 r4 D1 Z/ @int value=0; ' G4 w. \1 |7 w5 G$ O. V/ k
input=bufin.readLine();//read a user input string from keyboard ; j1 ]8 m4 y/ r2 O
try
5 r( j( r3 u" l, `2 j{ * [: R' A/ ~) K
value=Integer.parseInt(input);//convert the string to a int 9 l$ E9 k4 Z3 A3 d5 S# |
}catch(NumberFormatException e)//if can’t convert to a integer then reenter . N- |4 K4 _, Q( i( p2 r8 k) o! t
{
& r1 A' ?; b6 fSystem.out.print("请输入数据编号:"); ( I* w5 f1 ]( v, W% }2 @$ D
value=getid(count);
+ ?9 U$ u* I B" @} , z+ j/ q) i! K$ ~% N- w
if(value>count)//the input value is out of bound - q6 ^1 j! ^) H( y5 h! G8 P8 U
{ 0 q& D/ x/ b, v! N! y
System.out.print("无此编号的数据,请重新输入:");
" I- a6 t! P; w3 }getid(count);
2 a( D, y) `$ E' A, m* f} % E: N, x4 Y, P9 g& v8 K; b
return value;//return a value 4 x& |$ _8 M& E8 q0 I: {
} 0 V5 Z0 h- l1 r; \( ]
} |
zan
|