- 在线时间
- 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.*;
, o* _; U2 ~( a( m8 ?* M" f4 Z1 Kimport java.util.*;
+ V) J# ^- F. V4 [7 J9 uclass str14
; `# p& a) p6 I( m8 F+ ^( H{ ) S0 T- ^0 ^" {, \% B
InputStreamReader stdin=new InputStreamReader(System.in); " m. d- w- W D( f
BufferedReader bufin=new BufferedReader(stdin); 9 V. f3 B( m9 l/ s
, r% `* ?! H8 g4 r7 }/ O8 f: d
public static void main(String args[]) throws IOException
6 C' b2 P/ _# Z{ 8 Z" `3 F Z: ]/ T+ a
new str14();//run the application % v( [1 C6 P8 p. I3 F* d- i
} & ] y- u! t+ d! V0 B
t7 I, m( y2 |1 k# l% l
public str14() throws IOException//constructor / e1 r( }* V/ p$ ^
{ - i/ A0 e& d7 O0 F1 g
Hashtable htable=new Hashtable(20,0.75F); 1 Y( w- t" O: k1 x! n& O# V
dataBase(htable); 3 Y# D/ U9 J- w3 Y
}
. {4 y0 o( C( {2 Y* Q
7 L, q# o! g2 K! [& mpublic str14(Hashtable htable) throws IOException //override the constructor 8 @" _8 _ i: h% k* x2 Z7 A( J+ z
{
7 i9 w. v& c* E: n- X4 fdataBase(htable); ) D) K; G, p! w
}
* \+ ?# G" [/ {$ u- N8 C _, L/ J( q( L7 ~5 v
public void dataBase(Hashtable htable) throws IOException : Z2 ]" ~4 T1 D) z$ W
{
# E( L9 o4 z+ E2 K9 @int count=htable.size();//get the ammount of the data in htable
4 S$ B: b: R# Z) Y# H# rint value,id,num;
- S! s! c! e3 P" K( `String key,enter,data;
3 z/ u0 _1 f f+ ^6 \Enumeration keys=htable.keys();//get all the key in htable
9 C1 H% F8 z* C7 y VEnumeration elements=htable.elements();//get all the elements in htable
. N) l+ b+ F' `System.out.println("
- M- h* h0 S0 I2 x/ HHashtable 简易数据库程序5 ]0 b" Z/ H& [- h, ^( R
"); & @( v. Q/ \: @0 \: k
System.out.println("(1)输入数据");
- S0 l+ n8 s6 Y+ P9 aSystem.out.println("(2)请除所有数据");
y; `5 G7 M8 y9 B* J, n+ @+ z; cSystem.out.println("(3)显示单笔数据"); 1 i: V$ K2 F3 ]5 D$ _. c+ W! D, g
System.out.println("(4)删除单笔数据"); " h2 T+ i: }5 c
System.out.println("(5)显示所有数据"); 1 ^" P. G# `( h _' p. |$ @6 t
System.out.println("(6)结束程序"); 8 v- C' E0 f6 t
System.out.print("请输入您的选择:"); 5 h2 K* [. K) k0 J
value=select();//call select(),return the function number 9 I8 a8 f/ A3 _$ {* _5 V
switch(value)//the function . n& S4 |4 \$ m# g' R6 Q$ n
{ : k7 M Q1 e' h* K P! @
case 1:
/ d2 o+ F( W- j; \7 YSystem.out.print("
6 `6 L3 G( @: o4 p请输入一笔数据:");//need data input / z# k9 G! _4 f2 t: k& I6 p/ k
data=bufin.readLine(); 7 } ?/ {) ~" v7 F8 y% h. y
count++;
1 V6 Z. A) P& I: Okey=String.valueOf(count);
, ]! R/ ~' I+ B C8 w; Uhtable.put(key,data);//store it to the htable
: G- t6 f1 o' k% Z: G# u: X7 O, v {System.out.print("5 g/ `3 h! m, |' [+ T/ `& S
输入完成,按任意键继续..."); - z: C3 a9 z9 e9 s o+ p* O
enter=bufin.readLine();
- U1 O6 H- T3 H- k$ g, qnew str14(htable);//reenter
' j/ R0 d) y; i! F& Qbreak; ' A4 X \) M3 _3 G/ I
case 2://clear all data from the htable 0 I: W3 `( V" m
htable.clear();
& c; m# l0 g% O7 k3 G, \System.out.print("
- i" x- ~) {- C; s# P" ^; G' G5 |/ X已删除了所有数据... 按任意键继续...");
3 {, \. l2 ~2 E" f7 N! C6 menter=bufin.readLine();
, `2 @+ Z, Z& I* V' wnew str14(htable);//reenter : f8 U$ I ~# H4 O G- ?, |
break; 5 b! H# r1 v. Y) _+ t c) V3 D
case 3: . ^' y! B; I( y% U7 o1 H
System.out.print("
0 W6 {+ e8 f6 i请输入要显示的数据编号:");
4 ^! s( v* x# \/ J% L& m9 K* Jid=getid(count);//call getid() $ I+ t9 q C+ ^
key=String.valueOf(id); 5 \' ?0 x4 g- G" N$ n' [ x
Object select=htable.get(key);//fetch the data from the htable ) n* |, b. i8 c
data=select.toString();
1 m, a" K m: [. b0 ISystem.out.print("
I! \8 V+ _7 J' ]* [ c4 I编号"+" "+"内容"); , X+ a, @4 k8 L" l$ x( V; U) f
System.out.println("
x" Z- P. j; O$ |) K7 q6 B$ b "+key+" "+data);//display the data - h; R' G5 ^$ q# f
System.out.print(": P7 y: v! }& T$ u! A, e
按任意键继续...");
( |$ R% a$ u2 D! w; H( nenter=bufin.readLine();
9 l% j9 Y" o: J9 J/ k" N8 b) jnew str14(htable);//reenter ' K: X; {- @% S- r3 l+ O4 H
break;
0 A8 e" f3 [1 v3 h; Xcase 4:
+ u9 f m4 W& l# `System.out.print("请输入要删除的数据编号:"); - T$ n# }5 x7 h* v
id=getid(count); + u7 i1 q% B: x- n$ O
key=String.valueOf(id);
3 n! d* _) Y! X5 L, E/ xhtable.remove(key);//remove data 2 N/ a7 p/ K% Z
count--;
' f* @ [8 }7 `1 F. C) B, knum=count; - Y/ i3 B$ g4 ?$ s9 I+ Q
System.out.print("已删除了所选择的数据...按任意见继续...");
, d- U7 \ a7 N t8 N, WHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 % b% I8 ^% D' L! N
elements=htable.elements();//fetch all the data from htable * U* E+ I( r# p$ T5 N- b
while(elements.hasMoreElements()) - S3 ^. m |% r; j: t1 [5 E2 `
{
0 W6 P2 W! Z) Lkey=String.valueOf(num);//a new value \7 `* c' B4 N B5 J e
data=(String)elements.nextElement();//fetch content of the data
4 [3 T5 c) [) R( [0 A1 }8 M7 H3 Bhtable1.put(key,data);//store it to htable1 m& \+ z. M9 @5 r
num--;
$ A( C \5 O2 R! s+ s}
6 g, r! R9 F8 chtable.clear();
# j' N% w* h$ L. penter=bufin.readLine(); . y9 ^. H7 E p8 R4 b$ V
new str14(htable1); $ {; M) s+ Y( t
break; + Z: f$ }( r# E$ w$ S6 t' c
case 5: & E! S" z# H3 P; O
String[] sortkey=new String[count];//create a new sort array 7 s, v) a. N7 w, b1 W
String[] sortdata=new String[count];
' x- [- ]% |5 R# z9 U- Mnum=count;
: E% W ?1 k7 [, [+ @elements=htable.elements(); , D6 C7 T8 D: M# E
keys=htable.keys(); ) D& N( B9 }0 W: M
while(elements.hasMoreElements())
3 }2 y( C6 n) j% Y0 H. b5 \2 S6 _{ e; W4 F. F" E# u3 \
key=(String)keys.nextElement();//fetch value of key / i) A a" f* \( O B
data=(String)elements.nextElement();//fetch value of data ) S" b: h' K/ i, E7 s3 z
sortkey[num-1]=key;//store the value of key to sortkey array 0 h5 M1 e! }* o0 G
sortdata[num-1]=data;//store the value of data to sortdata array : ^! f, [' v, V4 ]
num--;
, P1 \% ~1 M6 o/ |' D8 q+ A}
3 m* V( `) g" Z# W# l- S; F" ASystem.out.println("
# c; Y0 _, I" g5 G$ D' G编号"+" "+"内容"); , H f0 l- D2 v
for(int i=0;i<count;i++)//display all content after sorting
+ @( _+ a( M* d! D3 M. `% RSystem.out.println(" "+sortkey+" "+sortdata); 6 C6 W" I( Z8 Y/ z( l
System.out.print("
! X8 g+ ~* ^# q目前共有"+count+"笔数据"); " u& v; \# N. N+ ?8 a! z
System.out.print("; `7 |* D+ E- I- K; ~0 T; k3 x
" v; R" T2 ]4 `' N& B
按任意键继续...");
* ^4 T8 c% z* I; [% renter=bufin.readLine(); 0 {! ~7 D& k; M. A- z" A: X
new str14(htable); ; |2 [/ H8 O; O0 P) }/ Q" j
break; : G. T k/ F# D* |5 U' t0 _+ W5 ]) q! M
default: 5 o" u8 \3 t+ r% ~1 I& p
} * O( [ u( l2 n1 b
}
. k! A3 Q0 ~7 R! c d1 ~, h3 g$ h8 B9 s; r6 z2 z1 @. z
public int select() throws IOException//method of getting a function selector
; E6 P" _ Q6 [6 I5 E: d6 {{ , y- ~+ I4 }, d \0 t2 m8 g
String input; 3 C: ]! [7 I. `
int value=0;
3 c5 W& _, G4 `' D! u( vinput=bufin.readLine();//read a input from keyboard
% V6 j6 {8 f; p5 C8 htry
5 h# D% u. E; I, K5 X1 b5 H{
1 Z6 c4 @6 b5 L: b& @; H K- Tvalue=Integer.parseInt(input);//convert a string to a int value 6 p, T' e# U6 P9 { |
}catch(NumberFormatException e)//can’t vonverted
8 o, g' V9 {0 J0 L{
/ S1 D3 _# {8 y/ e/ w' |$ ]System.out.print("请输入选项1~6:"); 7 I0 A0 p& r. |. ~, `" G# V
value=select(); 2 M0 E7 S. z. f; v6 A
} ( {3 E7 p% g; {) H
if(value>6||value<1)//if exceed then print a message and reenter : G' {! K+ c3 o1 ~
{ & g/ d& y) r* i. g6 r
System.out.print("请输入选项1~6:"); # \* u( G" C, D" O- O3 F# F/ X
value=select(); & ]& S: X; Y4 s4 @$ S S) |/ [
} * Z7 M% x' ]) O3 @* ?# n$ F
return value;//return a value ( K$ `" S3 I7 a8 D* N
} 2 n, M# @' @! b- ^6 w
! Z" p6 a( K2 p0 s( H% y" w
public int getid(int count)throws IOException//a method of return the number of data
( ]& K# C5 g/ X1 I) n{
5 J J. E& G: PString input; . u, T" m$ D# x+ V' L& C
int value=0;
: a& _- C3 S4 q' ?# f N3 finput=bufin.readLine();//read a user input string from keyboard
2 T, K0 E& }' b# o4 ~8 Q7 Ztry * [- X% `$ w/ ?! o+ D5 _/ _2 ~, s
{ + M f. S9 ?; [$ o# ^ i
value=Integer.parseInt(input);//convert the string to a int 7 C" a9 U- A5 A+ \% O5 C0 d" L2 }# f
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
; L4 D! Z. X$ S; n" K{
: C5 b1 X/ T3 d; e0 Y' d4 A* QSystem.out.print("请输入数据编号:"); $ {( V, b+ [9 F6 A/ g% \
value=getid(count);
9 T0 E4 n* T0 @8 g# S}
) u, N( \" c: ]1 w4 Zif(value>count)//the input value is out of bound
: U; v5 U7 H2 N: o- h" `& ?{ ! c5 z, s. z3 ?% T
System.out.print("无此编号的数据,请重新输入:"); 0 ~% L+ e' w0 M0 z
getid(count);
0 E) p0 M7 U$ X4 b/ s}
7 `' s8 \# e+ c _4 |% Creturn value;//return a value ' e, v9 C! \. J0 B$ V0 P# d
}
1 A+ a; D/ `* S, F9 k} |
zan
|