- 在线时间
- 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.*;
+ z( G O! o! k4 S" Limport java.util.*;
' G p0 T' Z; m$ hclass str14 ! k1 z. W" I& r, U7 ^9 O
{
$ ?2 `. L9 K" b: |. k; T( N5 C3 kInputStreamReader stdin=new InputStreamReader(System.in);
; {; @% c0 W, y. `BufferedReader bufin=new BufferedReader(stdin); E. `0 U3 s& h( d
' W# ^- |5 a8 @7 T$ a' o
public static void main(String args[]) throws IOException 3 b# g$ X7 ]) o. K& Q k
{ V6 j z. t4 g l1 o
new str14();//run the application
9 ^3 {8 G& `2 {& f j} 1 P9 o7 J6 O B! H: G# f' m+ X
, r- Z' E! U0 [: i/ j) c$ w T
public str14() throws IOException//constructor
: p( m$ K( t; a6 _ D8 J{
9 o; ?) M5 h; _ ]- a VHashtable htable=new Hashtable(20,0.75F); 6 ?: W+ G; R. A/ k) O, A( C
dataBase(htable);
* V' K5 V- F9 {+ e4 e* M- T6 p3 Q0 I, K}
4 H/ A& l# x% A+ Y9 D( C$ {, r$ F9 D* ]% s% x( o8 X/ M
public str14(Hashtable htable) throws IOException //override the constructor : B- [3 R% D4 S$ [
{
% ^1 M8 J! u, M' OdataBase(htable); , x3 @" S" v3 x8 j+ G: L
} 3 Q! |6 @( [! ?6 W3 `. N' Q$ H
9 Q& u! {* B' c2 M- Npublic void dataBase(Hashtable htable) throws IOException
" k. h; s, D/ s( J{ 2 D$ w: U T( h' O8 F7 f
int count=htable.size();//get the ammount of the data in htable
: k5 o/ X# U# ~* x: `8 Y. y' fint value,id,num; 0 B" @" ~8 L& G0 L+ r! _
String key,enter,data; / ~" y4 O. u! r" `9 n
Enumeration keys=htable.keys();//get all the key in htable / m( t% t+ A! u) D
Enumeration elements=htable.elements();//get all the elements in htable 9 P# Y& p# ~3 s9 b o
System.out.println("
2 T/ o" F, {" ?& K% g$ k/ n- C9 OHashtable 简易数据库程序# z" `( x. P% f# ~
");
G+ [3 |6 `7 D, n9 r# G% o6 @System.out.println("(1)输入数据");
( A/ J# U. G& {1 k' X8 F) p3 KSystem.out.println("(2)请除所有数据"); ( }% M4 m% l3 H' `- [4 V% W
System.out.println("(3)显示单笔数据"); $ \: S+ g7 b- {$ a7 {, Q2 Q1 e; E
System.out.println("(4)删除单笔数据");
# \; A v% |2 H( xSystem.out.println("(5)显示所有数据"); 0 B0 L- c' E% Q% R
System.out.println("(6)结束程序"); ' M* \7 ^, z, d5 b; t: I
System.out.print("请输入您的选择:");
1 E- m1 ?1 h6 I: wvalue=select();//call select(),return the function number
+ W" @' q3 p( }& f8 Vswitch(value)//the function 7 h* m6 f/ ~* l8 d
{
5 G3 [7 R4 z7 gcase 1: 1 e( y1 I5 p: y% F8 b% l
System.out.print("
; ]% `3 [3 {& E( l+ A+ ~请输入一笔数据:");//need data input 4 [ C1 }; O# I' q+ b: C' B' B
data=bufin.readLine(); # Z7 N3 G* w1 h3 M
count++;
5 l& @/ K/ o3 t, K: Ikey=String.valueOf(count);
: v6 j' B) m/ i( phtable.put(key,data);//store it to the htable
: O# T9 O+ E# _, `5 tSystem.out.print(", r7 q1 C# c" C- C; d7 [
输入完成,按任意键继续..."); * v* f' K9 c& Q: P
enter=bufin.readLine(); , q2 y3 u% \! w& Q3 p" c
new str14(htable);//reenter
# G6 C# Z8 `, R3 ~1 Z( z" gbreak;
, H6 i3 a$ z% `7 V; Hcase 2://clear all data from the htable
2 ?" Z0 ^7 E$ y8 I; } x5 uhtable.clear(); 2 o% O8 z p4 L6 n. u( h0 ~
System.out.print("
& j& p1 z$ ^; B% p9 V/ D' J {已删除了所有数据... 按任意键继续..."); ; ?) r2 ~6 |, r7 r- p0 u2 A% u
enter=bufin.readLine();
) h% _" L$ u4 v; i0 jnew str14(htable);//reenter % g U6 ?7 R. g v
break; % Y! }& L# e3 A* `2 F1 K
case 3: $ F3 B* o `& J# h
System.out.print("$ |* A1 w3 d, \# m+ q9 h% V
请输入要显示的数据编号:"); 2 z9 p* a3 L7 Q* q
id=getid(count);//call getid()
) n! l8 _, c0 A' p1 R2 Ikey=String.valueOf(id);
) M! q! z4 H% m: R/ ^Object select=htable.get(key);//fetch the data from the htable
u+ W# O6 ^/ ndata=select.toString();
% S7 v9 z$ e3 S& ?% c1 KSystem.out.print(", {7 a+ [( R' i& E" k
编号"+" "+"内容");
' u& Z5 J0 ^! R7 z: CSystem.out.println("0 N& F2 W F9 n- x
"+key+" "+data);//display the data , e0 b: W7 N5 S. E* S
System.out.print(": d- \9 T7 a3 K: s/ A0 R
按任意键继续...");
% u9 v6 j4 ]" g8 k3 @- s/ @enter=bufin.readLine(); , X. | n: l+ B6 D4 M
new str14(htable);//reenter 7 W: y# B( E+ x- T
break; ( B. `3 ~$ y6 z. Q3 [. o
case 4: ; h e# O: ?+ {1 L }
System.out.print("请输入要删除的数据编号:"); ) X2 N& ~! X- W$ \' p8 p# T
id=getid(count); 2 u- B5 Q2 `! u% K. m/ H* E
key=String.valueOf(id); - J" I1 i; u, |5 V" p$ e
htable.remove(key);//remove data : F9 n. y& F" G2 p* V5 S" ^4 d
count--; : Y6 ~. \ m, b: z
num=count; - [' r2 M' v5 N4 U" o$ `! Y& x$ h
System.out.print("已删除了所选择的数据...按任意见继续..."); ' n6 F3 C1 I+ P
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 & K0 |2 w* E, o/ ~0 X7 A! t
elements=htable.elements();//fetch all the data from htable
# b8 _* B* y/ h+ ~4 Mwhile(elements.hasMoreElements()) . E! Q$ H! }: D' f
{ 9 y& C" F/ t6 i: n( a
key=String.valueOf(num);//a new value
% N4 Z# k' w1 i0 j! C0 J% D" D# K8 Pdata=(String)elements.nextElement();//fetch content of the data ( W/ y0 k+ A% Z$ p
htable1.put(key,data);//store it to htable1 . N3 A1 o6 N. Q8 D
num--;
0 b3 w- A/ S R$ X) H' G} 4 r) T( h7 |: D. J! s
htable.clear();
8 e- r: G1 |( q! g$ f3 Tenter=bufin.readLine(); # I$ [) L* {6 t) w* |: Q' @8 B
new str14(htable1);
4 ^' w j# l; `. J) x, X1 Hbreak;
6 a# u" Y: _$ e3 fcase 5: 2 ` X/ U2 O2 b B ?
String[] sortkey=new String[count];//create a new sort array # x3 {8 y, I. Q4 l
String[] sortdata=new String[count]; 3 e) r! w( t& K) \, e+ Q9 b, F* |1 I
num=count; 5 @8 ?0 ?) e/ V4 U5 L4 G: |: t
elements=htable.elements(); - O: R, f a0 ?+ M( H
keys=htable.keys();
1 Q# I9 l: Y6 ?9 a1 pwhile(elements.hasMoreElements()) 3 S) A+ V! V' U6 J# z# M
{ 0 J7 b, ?; W% x. W0 L7 O" N0 B
key=(String)keys.nextElement();//fetch value of key & F9 y1 l6 K3 l' f& \! `0 Q
data=(String)elements.nextElement();//fetch value of data
6 I0 d. A6 ^3 q" Y& ~sortkey[num-1]=key;//store the value of key to sortkey array
+ Y4 E# M6 p( m! u. ?, jsortdata[num-1]=data;//store the value of data to sortdata array
. h, {3 X6 z7 c( z4 Q- S( ?4 @num--;
( W' z( j( m: b% h}
6 B O& A3 d7 i$ |3 f# d LSystem.out.println("
9 c% U$ A) L+ O4 w, u* ?9 ?4 ^: B编号"+" "+"内容");
# p' y: H, F' R$ Y |1 Tfor(int i=0;i<count;i++)//display all content after sorting
* Q: ~' F1 W: h3 jSystem.out.println(" "+sortkey+" "+sortdata); " r7 i ]9 v$ ^8 \/ i' E- b
System.out.print("7 p# ^/ y- x4 W- M% v2 e
目前共有"+count+"笔数据"); - L6 y( ~$ }5 y7 s
System.out.print("+ R' P. N4 V2 r5 e2 a
$ R. e: J# y; w; H2 T
按任意键继续..."); - F; n' V7 d# t
enter=bufin.readLine(); 6 a$ A9 s: G& t1 D) U7 y
new str14(htable); 9 j3 U+ f2 w5 h4 R0 k( T
break; & N" R; W9 G5 z* S
default:
) Q, q, z( D. I, W}
- F, P, F- g4 Y: D; O} ! ~2 ~- q3 `" a3 x% u- _1 h. Y, Z
7 \5 ? p/ m# epublic int select() throws IOException//method of getting a function selector
& |6 r' D( t$ e9 c4 \8 e' `# h0 G{
$ W0 }: E0 ^2 }' [, a& sString input;
+ M( K' H. v# Q( Eint value=0; " w0 ?1 n2 Z r/ _% C. D
input=bufin.readLine();//read a input from keyboard
3 T6 k# z5 A6 z" q# _- Ntry ; z# C! _1 X/ B9 Y3 d0 h, n6 w" h
{ # N& p/ I+ ^! I. r$ A; g
value=Integer.parseInt(input);//convert a string to a int value
# N- y1 h, F+ M0 U: f}catch(NumberFormatException e)//can’t vonverted
8 _; Z8 j" v& @% a3 S{ . T+ B" E1 {8 [* e5 s
System.out.print("请输入选项1~6:");
" @' z. ?1 U: n9 e0 Yvalue=select();
5 y1 l* v" P" D0 D D* b0 v}
" [) e! K- R3 L& U$ xif(value>6||value<1)//if exceed then print a message and reenter
/ j1 o# Z, y& {+ ~. ^{ 6 R+ {6 U5 M, h' d
System.out.print("请输入选项1~6:");
- m/ p" b5 K, |' d E* Rvalue=select(); 6 \, d& e/ [. X1 ?" t
}
- M: f& Y6 _" S6 D5 p, E. m! vreturn value;//return a value
1 o) J$ {2 i: V# {} ! Q, q* S+ a& m1 e, q, ?
7 [, t, _; a) ^ \, S$ w
public int getid(int count)throws IOException//a method of return the number of data
8 E, W7 t+ i8 s2 m/ y9 w ~{
" x1 k h! F) l ^. H/ KString input; 4 B& g5 i# N9 W. P8 S* E" Y
int value=0;
1 N ~# q9 q& ?! W% Hinput=bufin.readLine();//read a user input string from keyboard
* G" O6 j9 O9 m. }7 otry
" [6 |5 G( o1 q0 q# C{
/ W; P" b8 ~3 r* R _' pvalue=Integer.parseInt(input);//convert the string to a int 0 f) b, ?# B q+ X1 }
}catch(NumberFormatException e)//if can’t convert to a integer then reenter 3 T. l) O1 ] f
{ 6 Y' I! }+ F- V+ h
System.out.print("请输入数据编号:");
+ e% e3 S- A1 p! ivalue=getid(count); $ |6 @5 t/ H2 @2 V: x+ f
}
- l/ K" ~) d$ g0 q. N3 U1 ?if(value>count)//the input value is out of bound
9 ^# p5 e3 X' u. ]1 Y7 M{
1 }: w4 A b* P1 F$ X& J V9 c( nSystem.out.print("无此编号的数据,请重新输入:");
! Q L4 \% T! j8 z& n0 b; Agetid(count);
! b) z) W- q0 I O} 0 y) d, B. d* a4 Y" H
return value;//return a value % S# g" i6 ~+ R: d; t
}
5 y8 `( C: Q! _& E, z' B* h# e} |
zan
|