- 在线时间
- 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.*;
: t" ?1 Z* a$ nimport java.util.*; & M9 |( \9 l' @' _3 Q
class str14 , B; ], J- q1 Y3 w) l; B# U
{ " x. k8 S; y& K0 v$ h
InputStreamReader stdin=new InputStreamReader(System.in); " ~0 \# I7 Y8 X1 F5 I
BufferedReader bufin=new BufferedReader(stdin); 0 Z" L8 ^" X3 Z/ X; F' _4 d
3 p/ q! N M# I; z- ^
public static void main(String args[]) throws IOException
: d' O' G7 t3 z+ r3 W0 W4 N( I+ }{
7 l2 E6 a+ l" f7 M$ ]: nnew str14();//run the application
/ y E c( F2 G4 w$ c+ k}
) M5 E# X% L4 {* Q3 [
- `. B. j8 a7 G2 E( \public str14() throws IOException//constructor + A* c- O1 P6 \+ O4 |( \+ v2 B
{ % a' F: F$ |/ _+ M
Hashtable htable=new Hashtable(20,0.75F);
8 ^& w2 U1 Z6 k( QdataBase(htable); " q- q8 u$ C, \( R
}
& l& D) F Z/ b* p0 E7 g& a, a
7 y3 T( g, o6 U' X4 Lpublic str14(Hashtable htable) throws IOException //override the constructor / T0 a$ U1 X' k* C: |' h4 s
{ 7 c9 ~8 ^7 E" t5 ]
dataBase(htable); - z0 C `9 v# e7 B; |
} 7 f9 A0 ]6 K# n! w
6 V, Z8 y- K) N% r% x# s
public void dataBase(Hashtable htable) throws IOException
( M* T% s7 y6 `{
/ U9 n2 D: P1 G) R: Gint count=htable.size();//get the ammount of the data in htable / ]2 p: |& y1 d4 F3 z; x" w
int value,id,num;
# i# c5 `, C$ b0 s$ YString key,enter,data; 2 J9 G3 l' g! ]
Enumeration keys=htable.keys();//get all the key in htable
' b% t3 {# |' `. {Enumeration elements=htable.elements();//get all the elements in htable
; @( g& t x: F/ B& MSystem.out.println("/ Z, b; O; T, D+ w+ ^. c! [
Hashtable 简易数据库程序
+ I5 g* {! ~8 Q( \+ ]"); 3 C. |# a& I% Q# q9 i, A# J; }+ {
System.out.println("(1)输入数据"); 3 l/ }' t. A4 c( _6 z1 B5 s. Y- _
System.out.println("(2)请除所有数据"); & |6 A' ?* ^ O' Z7 ? p
System.out.println("(3)显示单笔数据");
) {$ y' {$ x! X9 \System.out.println("(4)删除单笔数据");
$ h6 z7 w! _* [9 ZSystem.out.println("(5)显示所有数据");
: j l k- X0 V RSystem.out.println("(6)结束程序");
/ f3 B) w) _- L' k$ rSystem.out.print("请输入您的选择:");
2 d" m1 B0 `& x4 {; M4 jvalue=select();//call select(),return the function number ! ?8 }$ Z( g- t. C
switch(value)//the function
" ^; P' c3 n: b{
2 i% d5 ?4 n) mcase 1:
2 T8 R* s8 J; P2 zSystem.out.print("
9 y1 X# L7 B/ E9 C请输入一笔数据:");//need data input
* {" [; F6 E& x) O5 Ldata=bufin.readLine();
! d) c4 F3 P `count++; , ]0 _: K: X$ k& l
key=String.valueOf(count); 1 M8 c" Z0 y1 g+ J2 t
htable.put(key,data);//store it to the htable
" v3 s* S1 m! F$ Z4 D3 v$ [System.out.print("8 P: `. y, r% R3 e5 V
输入完成,按任意键继续..."); # a' L/ [# p. l& j; }+ |3 r7 J
enter=bufin.readLine();
% S& `! D0 X8 k. [( m5 C# onew str14(htable);//reenter
5 o: B) r" F2 T& \# z0 Hbreak; ' \% \& T* Y2 m
case 2://clear all data from the htable 1 m# j4 n# r* _3 ^# d& z
htable.clear();
( ?7 K* S/ H2 a1 _" B2 x1 {! ?6 eSystem.out.print("
0 U9 T F6 s- ]5 Z, y已删除了所有数据... 按任意键继续...");
/ |& |2 N2 B7 aenter=bufin.readLine(); " ^; @! \0 Z4 T4 R$ g7 C$ w
new str14(htable);//reenter
- B" B! S: i; ^+ `break; . t/ G! z. m6 g- j
case 3:
& E2 t: s* H, ], j1 b, zSystem.out.print("
. [7 B: }2 z. h6 y5 E0 _1 w请输入要显示的数据编号:"); ; f, R+ P2 @0 |0 ^5 Z" M
id=getid(count);//call getid() . {# ?+ [* y" a# t$ W: e& ?4 ^' k
key=String.valueOf(id);
# N0 t7 r0 h, {& z( rObject select=htable.get(key);//fetch the data from the htable
1 |0 j. g! f8 Udata=select.toString();
6 n \, d, q' gSystem.out.print("
- l) O" H. F0 b7 ] t+ ?编号"+" "+"内容");
' }( g1 O- C" M" X- j8 g$ \System.out.println("
( j7 q; f5 Y6 x& G' E "+key+" "+data);//display the data # D' s# z& f7 l2 w
System.out.print("
5 p5 w1 v' b6 |" P1 z按任意键继续...");
, o+ t% _# q& h4 kenter=bufin.readLine();
! Z1 R+ D0 j. h e- { v0 Xnew str14(htable);//reenter
7 [; B/ ~4 Q( Abreak; & V0 q: f6 \* Z6 n1 _5 q! t
case 4:
& f7 b, W5 h. N2 R% s B- SSystem.out.print("请输入要删除的数据编号:"); / J" Z+ l2 K9 @' O: |
id=getid(count); " ]1 D( ?' F' I- p, I6 K
key=String.valueOf(id);
1 j: n. X2 s) v6 k: b Khtable.remove(key);//remove data 8 k1 x) e3 F0 x
count--;
- k. O1 z; w& }* {" U! Tnum=count;
, |' w+ w8 n2 m# F/ H; c, d+ \2 lSystem.out.print("已删除了所选择的数据...按任意见继续...");
m% h* G5 N" pHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
9 o; b) x& `( O6 k" s! Xelements=htable.elements();//fetch all the data from htable 0 t) Z6 S5 K3 t! D! ~
while(elements.hasMoreElements())
! _4 s' {) f: q{
$ s2 R9 J' q n7 I! T0 Q* q# t) y5 gkey=String.valueOf(num);//a new value 5 e+ M! v( x3 K7 \
data=(String)elements.nextElement();//fetch content of the data / {1 f/ h4 l# ^8 d
htable1.put(key,data);//store it to htable1
, O9 h7 e1 x# H/ z6 I% W9 d- H" K% qnum--;
) ? d+ s, j( t* I. z} . S8 d# I( }) C( I
htable.clear(); 3 ?. C: a. F5 V7 i/ r
enter=bufin.readLine(); & b. B4 Y5 t; x' D. i
new str14(htable1); : ]/ X0 R ^! J4 c0 \4 ]: @
break; * F( k$ U0 D5 s6 `; \
case 5:
- w6 r' U7 H7 lString[] sortkey=new String[count];//create a new sort array 7 I: p1 }! B5 x2 Y3 Y
String[] sortdata=new String[count];
1 a( a# _8 O2 k% p* jnum=count;
t# k" c7 T% O# a0 I( V4 Lelements=htable.elements(); , c- `) {: _) [- `% C8 r$ Q" e
keys=htable.keys(); / m1 D. ~5 \0 Y5 I
while(elements.hasMoreElements())
% I. s* d! M$ D3 A7 _+ b' }{ 1 K6 F) P6 i: t! e h, P
key=(String)keys.nextElement();//fetch value of key & M. i& X* ]9 ?
data=(String)elements.nextElement();//fetch value of data 5 e- B5 ]8 Z* E- {1 G
sortkey[num-1]=key;//store the value of key to sortkey array
% D9 q4 [; W( [( Xsortdata[num-1]=data;//store the value of data to sortdata array
/ V& ^. R' q8 Q% Fnum--; 1 W2 q# l- H+ L
}
0 j6 j8 e# |$ O& L7 O$ USystem.out.println("
( L0 c; e6 p& |! D' t4 ~编号"+" "+"内容"); : h- d0 I& |" V$ b
for(int i=0;i<count;i++)//display all content after sorting 6 J- c" h/ g: f/ L" Y. w6 b/ r7 r% E
System.out.println(" "+sortkey+" "+sortdata);
! V- M6 z5 W% I4 f1 ASystem.out.print("
, l) `: P! `3 y/ M& G& |- P目前共有"+count+"笔数据");
) _5 n9 d9 \. K* gSystem.out.print("
1 V9 \6 F* z3 |6 W" [ j( \
8 `7 `- L0 b8 I3 N: B按任意键继续...");
; E1 X! y3 c3 j$ f [: b; Q. Genter=bufin.readLine(); 6 G! Y9 E: M5 W$ M
new str14(htable); ; L d/ {' J& t, C. S/ O' R, {
break;
$ A) o& F) X. O& C0 P1 Cdefault:
5 P+ @% G# i6 m: G1 H9 ^}
1 J6 |4 V' i8 t4 E5 g$ e}
O& ~$ w9 m X8 v6 t8 s
0 E- B1 o2 Q8 p: ^1 b8 Upublic int select() throws IOException//method of getting a function selector 4 O! \. F& v! r7 B" N. b2 v
{ 0 h$ }/ P* Z. A
String input; 4 j8 c* t% R9 E$ M/ }4 |
int value=0;
: v! j' a6 b6 einput=bufin.readLine();//read a input from keyboard
8 J- p) I8 D9 b' V6 Ttry
- |' q- S: Q6 d- `& C( n0 c) a{ : I* X2 \2 \: F" C& H* P8 v8 e
value=Integer.parseInt(input);//convert a string to a int value 6 O* ^( H: n6 s: R
}catch(NumberFormatException e)//can’t vonverted " [; W' L1 s: P7 | g
{ " U1 R4 S( h8 N
System.out.print("请输入选项1~6:");
. U6 G4 n }; F0 ]# Z7 |+ j' y) Avalue=select();
5 _! B7 O/ }" q* I! u8 d}
% v5 u" w: |# U) w1 c' g) m7 r2 xif(value>6||value<1)//if exceed then print a message and reenter 0 {4 B: M0 p! v1 f
{
7 o% P- b" A- \5 R+ ~7 a4 T) iSystem.out.print("请输入选项1~6:"); 0 `) y4 ~: i/ a' V( ]
value=select();
% G! E9 X. }2 @& D}
; j7 [! b1 F1 p0 Z- xreturn value;//return a value
8 J1 H) D4 ~0 G0 d3 p}
) @% i7 M# [9 h! ?( O0 ~7 n6 D& D- a/ b5 n+ B
public int getid(int count)throws IOException//a method of return the number of data ! B9 ~3 t. {; E) h, E' [: P! _
{ ; w1 c9 n; v; W' A7 L& M# E
String input;
' q2 `: {2 L0 T9 l& Aint value=0; $ o# N' }3 o/ G8 @
input=bufin.readLine();//read a user input string from keyboard : N; J! |0 p1 b+ b& ^
try
8 [: v/ F4 s" e% l{ ) k; \) u7 G/ t, B
value=Integer.parseInt(input);//convert the string to a int ) K, t2 @* G8 W$ i1 f
}catch(NumberFormatException e)//if can’t convert to a integer then reenter 8 x, r0 r9 d6 F0 E) ~
{
' Y+ n" A1 q5 q; @; P' x) sSystem.out.print("请输入数据编号:");
% K# h( W( X- ^$ A% x3 A! Pvalue=getid(count);
7 [3 Z! L4 g1 O* m+ a0 Q7 g. Q} . J# n5 \2 M* X* ^; C) E6 g& u
if(value>count)//the input value is out of bound , R- ]2 R* P, v" Q) _( _0 q5 ~
{ : |2 }9 S+ O9 K7 w8 j# L- L* K
System.out.print("无此编号的数据,请重新输入:");
9 M( O1 _3 |$ o x, Egetid(count);
& n5 u; w0 d/ ^9 }, i6 x, e' S} " a# B" @, {4 y) C+ p7 h$ u# `
return value;//return a value - Y3 O/ C: Z, X n# Y- o! x
} 0 C. i# }9 l; K" `1 U: R
} |
zan
|