- 在线时间
- 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.*; ) H1 @) d+ g- d0 X" i2 x
import java.util.*;
6 Z# K0 I: T6 k0 B/ Z' g. vclass str14 , m d/ p5 y7 b3 O6 q
{ * w& S( Q7 R$ b. m' K4 ]
InputStreamReader stdin=new InputStreamReader(System.in);
* G: W8 a+ S o3 RBufferedReader bufin=new BufferedReader(stdin);
2 h* d( g) Z+ Z7 _
8 P: T: j# n: ?public static void main(String args[]) throws IOException # g0 v7 ^& H$ Z& Q! Q4 o: a
{ 0 g$ b' z9 ?' n9 k
new str14();//run the application
: U& L# V7 ]" B6 I) B} / {' f. Q$ U* `8 j! K! U+ y. o
x! m. D' o0 A Gpublic str14() throws IOException//constructor & ?, M# V- N) C8 b" R2 p9 m
{ 5 f5 `' s w9 I
Hashtable htable=new Hashtable(20,0.75F);
V$ T8 N$ m6 u4 W; W c! N; D* ^dataBase(htable);
. N/ o3 m* K& y* M' t7 N) g g. o}
3 s. V" z/ X0 g
7 I3 H* m5 q- o- F! Rpublic str14(Hashtable htable) throws IOException //override the constructor
+ ?7 X. \2 N/ O0 `$ B( e' }+ U) k{
3 v+ V5 V) @5 [, ` U- R, rdataBase(htable);
& s# [" L+ \. _}
$ x/ a8 s7 r |2 G+ p7 ]
& v$ u& H8 J! Apublic void dataBase(Hashtable htable) throws IOException 4 C& a$ \9 j3 c, d4 ?5 H/ }5 e
{ ! V6 h% H3 @' { Y% E9 |
int count=htable.size();//get the ammount of the data in htable
2 l: c6 W) T! {int value,id,num; / o% E. X+ P- t, J" U: I
String key,enter,data;
' s2 L# w7 L7 K0 X. K, r4 T6 UEnumeration keys=htable.keys();//get all the key in htable
/ P$ }( y9 _1 Q ?* Z# O; fEnumeration elements=htable.elements();//get all the elements in htable
$ M0 ?5 b& H8 i2 c1 R, hSystem.out.println("
+ F% _9 J; q; Z4 i+ q# \Hashtable 简易数据库程序
$ H- b$ I% O3 d& Z; c% s) \5 I");
& x' c1 a/ E9 H# t' ?' T6 @8 KSystem.out.println("(1)输入数据"); $ X# F K3 {$ ~! @
System.out.println("(2)请除所有数据");
% B X: H' s- w2 e) ]6 nSystem.out.println("(3)显示单笔数据"); ( o6 g: }* g' P& o/ d6 r
System.out.println("(4)删除单笔数据");
: m8 o4 t3 I, B Q6 YSystem.out.println("(5)显示所有数据"); 2 T* l" c. w- C9 Z) K( ]$ E
System.out.println("(6)结束程序");
% f4 M- u9 J% _# mSystem.out.print("请输入您的选择:"); % m* I' m3 _ Q0 J
value=select();//call select(),return the function number ! J' A% X0 ?8 q* `4 m3 ~0 U
switch(value)//the function
& o! |+ x" b2 u{
7 ]9 l% T' K3 Y5 ]0 V tcase 1:
$ H. n" q+ K& j( @; l; ~% B+ \System.out.print("
/ _2 F) e' \2 Z% ~' d+ @6 Z请输入一笔数据:");//need data input 6 c3 L6 K( i) H+ C8 W/ |9 ~/ _
data=bufin.readLine(); \2 ~+ n. _( B) k
count++; : j- ~. m, g! G. s9 j! t4 v0 O. L
key=String.valueOf(count); & T$ E8 {, M. `* r. C
htable.put(key,data);//store it to the htable
3 W; ?. K" L: u4 g! _System.out.print("4 D6 y( D; ]! G
输入完成,按任意键继续..."); 9 Q0 R# o5 I4 A- y& h
enter=bufin.readLine();
5 `0 D' [+ ?; Q, c$ knew str14(htable);//reenter $ f# T9 u$ e# c" k- R
break;
; h0 N* @7 L# \! ucase 2://clear all data from the htable ' J6 J/ g! U: n, q& @, U. z$ ^
htable.clear(); * Z, s% t' @& s# b; w* k) G0 ?) n
System.out.print("9 ~/ _9 x# K7 ]/ F6 l
已删除了所有数据... 按任意键继续...");
1 q" K2 M5 j/ Z* V% f2 e i& ~enter=bufin.readLine(); 4 L" }" g8 f8 {$ b* G c+ j' T1 C/ t
new str14(htable);//reenter
5 k3 ?0 ?* z4 C$ q6 g. S3 Obreak;
4 v8 Y! n( a5 ^case 3:
4 ~- r9 F) x$ ?) p5 KSystem.out.print("
0 ~# t0 f: v* @3 T1 a9 O( }请输入要显示的数据编号:"); : \, |# \0 s/ w0 [
id=getid(count);//call getid() ; |1 x' h7 B' |4 m: ?
key=String.valueOf(id); 4 k2 q8 }/ K& |7 O) p2 J: K$ n
Object select=htable.get(key);//fetch the data from the htable 5 u2 [. n- s8 D* `9 j
data=select.toString(); 9 E4 |6 t: Y% J% M% u" v0 I+ B
System.out.print("
/ P( E b3 H6 c; A; v, z编号"+" "+"内容"); , a& e4 ^$ a, E8 m# O3 a
System.out.println("
$ |5 r" R) G- m5 q "+key+" "+data);//display the data
2 V6 w' ^8 M- f% e2 Q- ISystem.out.print("
1 F6 [ L2 \1 g% t; H$ [& l5 {; q7 U按任意键继续..."); : v1 E. V* t, G( T2 U
enter=bufin.readLine();
w8 M1 w f" O- _3 L0 Knew str14(htable);//reenter
7 Y' {# ?9 w: |break;
" b% H) ?, Q) u, i- v1 g4 lcase 4: 0 B; s" D; \- U6 J2 Y6 o
System.out.print("请输入要删除的数据编号:"); + t+ E! s5 I1 Q6 s- T
id=getid(count); + p4 i2 k- c/ ~
key=String.valueOf(id); 2 b Y( y. W @
htable.remove(key);//remove data
1 P+ N- H6 J4 {& ]3 n1 Y% L { H- ycount--;
* X! z. t' H+ Nnum=count; : y Y) d5 E3 \: D
System.out.print("已删除了所选择的数据...按任意见继续..."); 0 ?1 _5 k. i% r# z/ V
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
; R7 S; e+ X% L% v# T8 Melements=htable.elements();//fetch all the data from htable
$ G( V7 b8 ~& L# ~+ }8 W! Dwhile(elements.hasMoreElements())
& B: R5 f, S8 ]( N# h3 T{ 3 N8 @# }* L7 U O1 b% K3 {
key=String.valueOf(num);//a new value
" g$ }; j7 P- ^# \/ @data=(String)elements.nextElement();//fetch content of the data
3 L$ m, P# x U* d) q* C0 l4 }htable1.put(key,data);//store it to htable1
. |! ]" {) ]5 R) ^+ c+ Jnum--; 9 p- \" u% p% A
} 6 ]1 x* `' i- z
htable.clear();
) @) k/ B: x5 w3 U% {- {- Renter=bufin.readLine(); # X) J4 |; \* R% `, d! K Y, p3 e
new str14(htable1);
4 B0 P9 G4 [2 d$ d6 Y8 N+ obreak;
1 [4 n& b, D) G: y' \6 k7 ~5 `case 5: " U' ?; p6 U$ l! f
String[] sortkey=new String[count];//create a new sort array / K0 Z, O! P5 @& _1 g: o) S7 e1 M
String[] sortdata=new String[count]; : l; K0 n# `5 u0 |1 j4 H. M" b( X
num=count;
! n/ b, U& K Q8 j eelements=htable.elements();
! Z. Y$ a: g% ~! Okeys=htable.keys(); ' D* V4 e; a6 Q5 L1 I+ F7 x
while(elements.hasMoreElements()) $ e" i, l. @- A- n
{ 7 ^1 Q/ a& F" o- m4 x0 u
key=(String)keys.nextElement();//fetch value of key ; a& X- M4 h* o0 l' k
data=(String)elements.nextElement();//fetch value of data 4 `, T, m3 Q& o) z; |& `2 v9 i
sortkey[num-1]=key;//store the value of key to sortkey array
. I7 D1 e3 `! G$ _sortdata[num-1]=data;//store the value of data to sortdata array 2 w: K/ s0 f8 v5 @6 _
num--;
3 F A2 l3 k4 M! u+ x} ) n/ r0 B/ P/ n- I: D4 M9 E( G
System.out.println(": F9 W* F* {1 S# \' X7 u$ y8 O
编号"+" "+"内容");
4 z {# h ]8 U4 N" g% W5 Yfor(int i=0;i<count;i++)//display all content after sorting
4 S. n8 c7 R [8 }( mSystem.out.println(" "+sortkey+" "+sortdata);
1 h7 z, w# T, ~4 ?System.out.print("
, L1 s" r* X- r目前共有"+count+"笔数据");
0 A+ \9 ^! A# E3 S( {System.out.print("% o( ~* ~: k6 ], z& a- G9 K& G0 g/ e
5 U" k" D2 U5 ~% J" U4 {按任意键继续..."); : K3 Q6 D7 a. P; U5 h- d
enter=bufin.readLine(); 5 `7 D4 y' D4 k2 f: e! L
new str14(htable);
! V9 x% I) j/ j% H5 [1 cbreak; % M Y) A4 H! t7 D/ ^) P/ _3 U
default:
3 U* j5 X9 g6 e" N. m, i/ S* t7 i} ' A4 K5 y* `# R* I" s
}
: A& G* q S% k5 H
0 W9 v) ?- t0 g5 m/ @. Mpublic int select() throws IOException//method of getting a function selector 9 Y q. Y! k9 U a/ P# ~1 {
{ % z) I+ u, b/ Y# ? v
String input; & N$ i+ H* y3 s
int value=0; ; |9 @3 }& }7 X( g+ ]* V
input=bufin.readLine();//read a input from keyboard " D# v, V' K8 k
try
) O* g/ P5 I. F4 {{ 5 |8 B/ @, A; E, }# r- X s$ G2 r
value=Integer.parseInt(input);//convert a string to a int value
% a5 J$ m2 \/ z5 w" X6 u! _}catch(NumberFormatException e)//can’t vonverted 0 j2 ~1 m8 w, L2 O" @5 c5 r
{ 3 s% q3 q% l( y) h, R$ n
System.out.print("请输入选项1~6:");
, ], }( U, a3 i1 b8 y! h: zvalue=select();
' y, l+ V" g+ n9 Z}
. P* J I4 J! U: w1 ]% ^if(value>6||value<1)//if exceed then print a message and reenter
2 V9 [' E1 J4 O{
* P" ^8 a! A# b& QSystem.out.print("请输入选项1~6:"); 0 w' K8 U" G* x9 M5 ~
value=select(); l8 B L9 L& I2 F
} : `+ l! ]: S q' @% j% X
return value;//return a value & d! T' e# G1 Z* \3 Q4 ~
}
+ G. O" m; W8 N# _3 u; s9 C$ h/ f9 {6 _) h
public int getid(int count)throws IOException//a method of return the number of data
$ X2 T. N; R0 \# y! S" E1 {2 h{ 5 d9 Q" I# O0 q2 T( X; }# D
String input;
' R: Y+ i( \" F1 cint value=0;
3 _* [: N. V- m1 l% X; a1 linput=bufin.readLine();//read a user input string from keyboard + D# S1 c9 L- D9 q
try
( _7 E+ I/ S7 K% d. b{ - C8 Q, g4 ]7 Q( `8 y1 d2 l
value=Integer.parseInt(input);//convert the string to a int 3 {* ?& c. L: W" c
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
+ d( ]# p6 y" M0 t* m2 x{ : ?+ ^0 G: L/ `$ T9 u! b7 f
System.out.print("请输入数据编号:"); ; _6 ^2 a2 o$ s1 B* g% F) _
value=getid(count);
5 l1 p6 D" N% n7 j}
; I8 g0 R- _) ]" C5 Oif(value>count)//the input value is out of bound
! U5 P, @1 Y' h) B# |0 G{
' K2 G3 q4 @. O+ h! P$ S7 ZSystem.out.print("无此编号的数据,请重新输入:"); + D5 u* l) c5 J
getid(count); 9 P) Q1 q& b5 f# P3 b+ x
}
0 ^7 F7 g; ^' i! b; c5 qreturn value;//return a value 8 f! y" Z0 V. N1 m: d" x$ Y) c" d
}
. D1 a! R1 [1 e, {- e; s} |
zan
|