- 在线时间
- 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 {; U# B& }
import java.util.*;
5 p3 u. ^) g6 Vclass str14 6 O# g% w; l+ T# x
{
; ?& k i) u6 a2 MInputStreamReader stdin=new InputStreamReader(System.in);
+ C1 G: w( q' I4 D! v) P+ r7 o. oBufferedReader bufin=new BufferedReader(stdin);
+ b8 T$ I: u* t( \5 d8 j8 n" y4 l) X
7 I* ?% a/ s" {' L" s( ~1 f' ]2 v" mpublic static void main(String args[]) throws IOException ! P8 ^% U9 K F4 v
{ 9 i/ h' ?( n' W5 x- n! G6 g& Z
new str14();//run the application
8 g. k- v# s' C& {8 G6 B" A9 ^} * \+ I1 Q+ ^! n+ f6 J7 Z a
" L: u# C9 B' n2 Kpublic str14() throws IOException//constructor 0 I6 r! H" V1 b& r
{ & n* \, P. Y/ e3 Z
Hashtable htable=new Hashtable(20,0.75F); + `1 H! k2 v+ o
dataBase(htable); # Y: P' k& S% ~6 p. o3 f
}
! y8 ^" L8 n/ P9 `8 c+ Z$ U6 X) A) \' C: A, `3 z0 j2 e
public str14(Hashtable htable) throws IOException //override the constructor + @4 W$ c! Y6 `# O+ B0 M6 j5 X$ }- m
{
) w$ ?7 ~# q: ^9 V; T: j. ldataBase(htable);
. M& \% H. x& z+ k1 S} & W8 V+ e$ B( s1 v& H2 y
+ P& r# q n" {, T, ]( c
public void dataBase(Hashtable htable) throws IOException
) R5 n8 Z0 s1 w) _3 f5 p% p7 _/ K, ?{ : W9 [ m* C+ S4 C
int count=htable.size();//get the ammount of the data in htable 8 x( I/ N* S1 ?% {' e
int value,id,num;
# ]! q3 ?% r" E2 V. S: R9 qString key,enter,data;
' [7 ~4 L7 I: ~3 g8 IEnumeration keys=htable.keys();//get all the key in htable . q2 [; k F. m: ^) Z' ^
Enumeration elements=htable.elements();//get all the elements in htable $ N! W8 e! K( L3 M# g! U9 Y% ?
System.out.println("
% C3 |1 ^$ n& a9 O0 `Hashtable 简易数据库程序% {( i% \0 j7 p3 F' S
");
& \$ `, C% n3 d5 V& Q" C; b$ KSystem.out.println("(1)输入数据"); " j8 N5 g9 o+ u) S5 S# P; F
System.out.println("(2)请除所有数据");
2 A' s( ]6 |8 ^3 t7 s: e7 {System.out.println("(3)显示单笔数据");
) {9 T N1 `/ l8 L' B5 T1 {System.out.println("(4)删除单笔数据");
Y4 h B6 U. P6 e9 TSystem.out.println("(5)显示所有数据"); 4 f4 I" B( w$ b% W9 Y+ x9 y
System.out.println("(6)结束程序");
- F+ n8 x# N" }9 ?- y( M- hSystem.out.print("请输入您的选择:");
, h$ l) b7 U* I* f9 Wvalue=select();//call select(),return the function number
' r: \ f% w% J# a- f) c( bswitch(value)//the function 1 H. t$ |1 w7 p& i# ], N5 N
{ - A- s4 v' p1 O' k* K3 ~* |4 [/ y
case 1: 2 [4 X7 T; k2 P0 Y. L! w4 v
System.out.print("
; v- [) ]" @4 ~) D$ C7 j请输入一笔数据:");//need data input
1 x) ?' e1 A! }9 c) c$ P6 Hdata=bufin.readLine();
. ^' j( K3 C1 Y4 ocount++; y; c8 Y' r9 i; p9 {
key=String.valueOf(count); 5 T5 G; {' x# j2 D1 b+ w7 @+ E
htable.put(key,data);//store it to the htable 9 y7 W9 Y2 |0 v) n( E$ U0 t' ]
System.out.print("4 q/ S9 }! T2 [. J
输入完成,按任意键继续...");
6 {) ^$ _, V: H' b3 l" ^enter=bufin.readLine();
* K. N& P$ t0 t6 Q) z+ Ynew str14(htable);//reenter
9 j+ ?3 o8 o" d# Y8 Y: k& \* Pbreak; + ~) E! `6 C" U
case 2://clear all data from the htable
: `" R! H, l2 h# }1 zhtable.clear(); I6 i) e% ]4 j
System.out.print("
( w7 O) ~, y8 C; ]7 J已删除了所有数据... 按任意键继续..."); - i. |% c3 s, P- W
enter=bufin.readLine(); ) M( [6 Q- K4 u6 T
new str14(htable);//reenter 9 r" P' K" f! V/ R+ d8 t
break; & ]: j$ R* e% @/ t) x
case 3:
8 P" P2 @) m% u* E6 D; d: i7 K0 n3 eSystem.out.print("+ M) L7 Y9 J: x, m& B: ^, u8 G
请输入要显示的数据编号:"); x0 H M3 [' K+ U; S+ l
id=getid(count);//call getid()
: Z4 w; _: e( h3 X6 U5 Gkey=String.valueOf(id);
7 U1 a1 g7 C! J( f& CObject select=htable.get(key);//fetch the data from the htable
- i1 m" C% I+ W- a; Ndata=select.toString(); - {5 R6 R5 G5 R! R2 M6 S7 z
System.out.print("
- ~1 C1 V3 t! i4 j编号"+" "+"内容");
/ l) T8 @& d/ j& R# b- U FSystem.out.println("; m7 ?" C. K' M1 G
"+key+" "+data);//display the data
& [" R8 c( Y9 o" Z( FSystem.out.print(" |6 l! E0 u# Z* j0 w1 t' h
按任意键继续...");
5 K0 j1 W* ^9 wenter=bufin.readLine(); % M. ?( H- [" P6 b0 Q
new str14(htable);//reenter
}, J- A3 q' Jbreak;
, s: i: J1 k' |/ ?2 e# A1 Lcase 4:
' o# T4 q' l% e% \( j2 O& ]4 HSystem.out.print("请输入要删除的数据编号:");
; L0 f9 K( B1 ? D" j3 ^id=getid(count); & c3 `5 v5 E+ c; C
key=String.valueOf(id); " p" {# F7 b( v; `
htable.remove(key);//remove data
& _/ L8 g2 }! l+ d. l) Bcount--;
1 U2 b/ C' ?8 p5 w/ D9 Enum=count; 7 D6 Y: Z; G: E7 c$ T( ]
System.out.print("已删除了所选择的数据...按任意见继续..."); ; M8 {! A3 g p2 @' l
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 1 r9 |: F3 C* y0 |8 G! P, G1 E
elements=htable.elements();//fetch all the data from htable 1 k4 k \: M2 N* ]! w) e$ a6 h
while(elements.hasMoreElements()) 1 {5 j4 I. K6 G- y# @( B8 ?
{ 4 [7 |& C: r9 U( i- v& Y0 L
key=String.valueOf(num);//a new value ; h Y, b# e6 j* x
data=(String)elements.nextElement();//fetch content of the data
* P6 ~ M; \0 D W6 P7 u5 khtable1.put(key,data);//store it to htable1
( s; j/ n* ]& J* g; E ~num--; 9 ~+ u; D4 A) \. Z$ d; r6 z
} , b$ l) L5 v! U6 s
htable.clear();
6 Z3 W% Q3 V+ H" K; Center=bufin.readLine(); T7 [& x* \+ b1 j
new str14(htable1);
4 l. F- T0 K) ~break;
( I: O7 X+ R) Jcase 5: ; C6 i1 ]- N+ t- I! y" o8 B' ?
String[] sortkey=new String[count];//create a new sort array
" B3 l$ d0 C0 c4 gString[] sortdata=new String[count]; ; J$ p. f+ |; t% W9 e, \1 O
num=count;
+ {. e2 I: b9 I6 Zelements=htable.elements(); 7 V% o8 [7 q( Z2 q/ q3 h0 C9 E
keys=htable.keys(); I: B' b9 v" V8 }5 T y9 O3 i0 c' e
while(elements.hasMoreElements())
3 O+ a) |7 A+ \ h, [; ?{
0 K- ?# \6 ^' U0 X* Wkey=(String)keys.nextElement();//fetch value of key
1 z7 q! v& p0 L' Gdata=(String)elements.nextElement();//fetch value of data ; \0 [* `4 B7 D0 S
sortkey[num-1]=key;//store the value of key to sortkey array
. _' ?$ _$ B- U; H& D4 ~ B2 W5 isortdata[num-1]=data;//store the value of data to sortdata array
& ~8 W5 N* h4 |+ Inum--;
$ @% E" Z, D9 ~- o$ G4 p}
. m( {7 u0 |6 JSystem.out.println("
3 S$ W: s6 R4 \: Y+ n ^编号"+" "+"内容");
$ x2 d0 Y A' Q$ cfor(int i=0;i<count;i++)//display all content after sorting
" H7 L; r- i2 MSystem.out.println(" "+sortkey+" "+sortdata); / [+ e" ^) N# d& O4 `5 N0 {
System.out.print("/ [2 v9 J, X! V) _9 ^) Y
目前共有"+count+"笔数据");
7 l6 `# D; Q9 D" JSystem.out.print("
8 T# l2 q1 `, e3 e) U4 i9 O5 t3 p8 l; B
按任意键继续..."); / g, y! ~! S0 r" K; I h
enter=bufin.readLine();
4 D, h3 f1 g H, |* D& {# [$ wnew str14(htable); ( h! ?9 ]; y3 \6 q6 G) O; z- u
break;
; n" l0 i4 R3 Rdefault:
$ k" c0 p) W" N/ B# l" G* m} ) Q G- \4 S% n1 m# `
} 2 B! P8 f# u3 i
( F: q O2 ]' U+ ~! [. @public int select() throws IOException//method of getting a function selector
+ W$ A" y; Y( t( ~$ d5 s{
9 V- g4 M% d* X8 a3 d! x5 Z# wString input; 7 y5 G5 B. q+ G# _ @4 V
int value=0;
; n* A2 b% e* d9 K$ L9 m1 A) Y+ ?input=bufin.readLine();//read a input from keyboard
3 j# A- A! n* r. w+ ~. t+ j5 z3 q4 vtry + v2 [" g, H% P- J! T
{
( x# v# u6 Z8 q6 k# c+ c- J; C4 _value=Integer.parseInt(input);//convert a string to a int value
: J/ m4 T5 ?7 I2 L4 e# {7 W4 s}catch(NumberFormatException e)//can’t vonverted
& x$ J" ]: J; b0 @- A* u, O{
g4 [5 f& W" r( E% e" ?System.out.print("请输入选项1~6:"); , l3 `1 D( s# z+ f, _! |2 p! N! v
value=select(); 0 H# }) z: F2 l
} : w' ^0 B- u+ L0 e
if(value>6||value<1)//if exceed then print a message and reenter
9 u3 b5 H; f a9 q{
% c5 l6 A% S, y2 b6 pSystem.out.print("请输入选项1~6:");
( L" D8 V8 Y9 R5 Nvalue=select(); ~- J! o* v0 a/ r6 }
} 2 d0 w2 e5 W. p/ \8 P N( ^
return value;//return a value
6 G9 A4 A+ \6 B' I& O/ ]3 L3 G1 b} 9 w5 P" w j/ ^
# j t) |/ i3 J- D4 j Zpublic int getid(int count)throws IOException//a method of return the number of data - w2 l/ L# C6 X$ l5 g1 d& o4 D& A% h
{
9 u* }1 C* v$ ?0 y! B+ S2 w2 R+ _String input; % e2 M3 a7 t6 z& g
int value=0; C7 G. Y0 Z% {, b! o/ V
input=bufin.readLine();//read a user input string from keyboard
" a1 W( M) I- Ktry & ` @2 ^/ v& E3 @1 P
{
6 I# A7 y: Q; i9 p% h0 w+ E8 X( tvalue=Integer.parseInt(input);//convert the string to a int / J' v9 s* {; T4 x2 g
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
" h0 U- Y+ [% F{
3 { V$ ]0 `8 jSystem.out.print("请输入数据编号:");
. v0 j' e% z, q; h! B5 jvalue=getid(count);
1 R D7 ^; b7 n}
: ]' V) ^) R3 k& Qif(value>count)//the input value is out of bound
% L# |5 k$ A' Z{ ! u0 V% k7 b8 L' L- l/ v
System.out.print("无此编号的数据,请重新输入:");
) W, q6 B, t+ j5 [6 t+ qgetid(count);
2 R+ {. l3 p6 V0 c" g7 H}
; x+ d) r2 Z$ f. N; C$ Mreturn value;//return a value
, |; b6 D& y! G8 I} c2 F- l' `7 x
} |
zan
|