- 在线时间
- 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.*; ! B4 ]1 l' T" ^! C' F+ k
import java.util.*; / Z) J" i" H7 j- f$ ~
class str14 * M+ X7 I* {( S L, T8 y
{
# r6 O6 I3 D2 P. D: i" tInputStreamReader stdin=new InputStreamReader(System.in); / f b! e; k" B Y! d9 i0 w
BufferedReader bufin=new BufferedReader(stdin); + c6 O* y" F7 W" Z0 S% I
; z6 H7 s5 q* k; bpublic static void main(String args[]) throws IOException
! L( S9 e. f% C: Q! [{
S( L. N, B$ r- T- z# m/ Z7 o) ynew str14();//run the application
& f4 D- J, a1 m; w}
% Q5 n1 T3 C1 r9 N% p3 g3 |; }: y/ m- q2 F% `0 ^; f- p
public str14() throws IOException//constructor : @; ]7 t4 N8 A0 u
{
) @/ p, n% F+ XHashtable htable=new Hashtable(20,0.75F);
4 V% {. i0 ^2 adataBase(htable); ( ]5 T$ s. D7 m9 C j5 G; @" A0 ~& n
} ' E, [5 u+ \# Z2 Y. m$ B
0 J0 f& ~3 {8 k8 Y+ Dpublic str14(Hashtable htable) throws IOException //override the constructor
4 f6 M: w& P9 `. W4 l{
* a7 }4 }' M0 W7 ` NdataBase(htable);
H. _" x5 ?% `) _1 T7 {} % d5 _- c- C4 P) J, { k2 j
/ k/ h2 l# n$ g. z3 Z4 f1 t0 G
public void dataBase(Hashtable htable) throws IOException 4 P$ C; q+ D# K$ L& }* `5 j: l/ ]% ]
{ X) Y3 R% g" i' t" ` n
int count=htable.size();//get the ammount of the data in htable
3 Q* Z. H. n- d) Y2 ]( [1 T; iint value,id,num; , Y! v0 m3 J# j) A
String key,enter,data;
) f$ g+ d' t3 g5 _2 o' QEnumeration keys=htable.keys();//get all the key in htable
8 W. f/ E! T3 s) W/ T9 R0 G5 uEnumeration elements=htable.elements();//get all the elements in htable ' u! _# | j( ~9 [" C: ^
System.out.println("
+ p/ i0 F. v# u, L$ ^Hashtable 简易数据库程序1 K% x. F) R! B6 N1 j
");
, d* R0 E% _4 k: U2 a* P& oSystem.out.println("(1)输入数据");
) J8 e9 Y7 [+ h: o7 X8 HSystem.out.println("(2)请除所有数据");
. I: E! f% F/ _7 p& ASystem.out.println("(3)显示单笔数据");
0 v; p) l/ S) ?6 @ mSystem.out.println("(4)删除单笔数据");
4 I6 T, c4 f5 O% l, zSystem.out.println("(5)显示所有数据"); 8 Z! m. [; |9 y$ j
System.out.println("(6)结束程序");
" q, M8 C" F( R6 i* rSystem.out.print("请输入您的选择:"); : g5 O/ j' ^, g0 E! _1 A; ^
value=select();//call select(),return the function number
$ S- C3 B1 p1 t$ \' mswitch(value)//the function 4 h) y/ a. \& _7 ~+ F
{
% W/ o. V2 j E6 A$ Y; {4 `3 pcase 1: . h1 W+ V# X( r8 O# ~* ?1 \
System.out.print("
& B+ d6 f/ X, W& h. {/ N请输入一笔数据:");//need data input 9 Y7 W4 x, f1 H2 r$ ^5 ~7 p* T
data=bufin.readLine();
$ X% `, ~3 c! }, X' Vcount++;
, a: ~) {6 d& s" I2 p" F. M9 okey=String.valueOf(count); 8 j6 L5 z" p/ f6 K3 `! U
htable.put(key,data);//store it to the htable
& n) o. e) C5 d# }8 {8 v* N$ RSystem.out.print("
7 `% N! s, H2 e# h) @/ h5 V4 L V7 n输入完成,按任意键继续..."); - P5 P4 z9 T+ M' V% e$ v3 p: C5 o6 J) E
enter=bufin.readLine(); " R) D3 \9 |2 F X, `8 C1 @) h5 u9 @
new str14(htable);//reenter
; `) z5 X* d3 |# W/ p% T7 B8 b, Zbreak; 6 m4 H& p5 A2 L
case 2://clear all data from the htable
. x% n# d( g" V3 |9 Z" }; Bhtable.clear();
- }4 V! e. p/ @! Z# L1 QSystem.out.print("1 c. K) a: S2 o( x5 f) O
已删除了所有数据... 按任意键继续...");
% ]0 h+ O- s4 u, Denter=bufin.readLine();
' W( n7 ~% t/ j( m- |1 S' F0 v; Q% z/ onew str14(htable);//reenter 5 r0 M( |- J& P& y5 ?
break; 7 j3 ^- c$ u1 o8 S3 [
case 3:
- I; B* j+ ^( vSystem.out.print("
# Q4 t0 i0 v b; g$ j- S请输入要显示的数据编号:"); 5 i6 {9 [" c# X( W/ g
id=getid(count);//call getid() 4 d6 ]0 T/ ~: o( ?' x
key=String.valueOf(id);
6 M5 ^4 y7 b6 ?: A0 [7 \0 _9 y) fObject select=htable.get(key);//fetch the data from the htable 7 z9 Q, E. ?3 U
data=select.toString();
' J3 U4 _2 b! M! b' WSystem.out.print("
0 j+ I# w. l. E. y& m编号"+" "+"内容");
X; G, ^+ \" ^8 ` f# p2 wSystem.out.println("/ Q% T; s6 l4 S$ t7 \
"+key+" "+data);//display the data
+ Y! N5 O1 H) HSystem.out.print("; I& y5 M3 o2 V4 T, T
按任意键继续..."); $ M* k; \2 T" t& S4 Y/ d
enter=bufin.readLine();
" D0 v0 }- i% z; [. |new str14(htable);//reenter
, b0 o1 H: l8 Abreak;
( A0 ?8 ]& ~0 Z) Q) fcase 4: * q5 H9 l/ M/ m1 p$ Z" r A. l
System.out.print("请输入要删除的数据编号:"); % v4 k7 M: T& p8 ?) K/ d+ r# ~' t
id=getid(count); 5 a$ R. d0 z2 |3 _
key=String.valueOf(id);
, a: B; ]" U8 C' j- hhtable.remove(key);//remove data
9 m. l ~# b' ^count--; 3 a2 R* D6 i: d+ Y- m! n) |
num=count; + S) D* ?- n. T7 S9 a
System.out.print("已删除了所选择的数据...按任意见继续..."); . b3 V' _5 {, d# F+ I( W) p6 \
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
: ?: N* [4 X5 {6 k/ @elements=htable.elements();//fetch all the data from htable
* W7 u0 V7 g) A8 H2 {: uwhile(elements.hasMoreElements()) ; o1 K7 ~( W* O' [* \' T
{ 7 f7 r: q! b0 T. j+ G6 I9 Q
key=String.valueOf(num);//a new value - W; @8 E$ L- [ r7 r! J j* J
data=(String)elements.nextElement();//fetch content of the data
, m$ Y$ w: q( Ehtable1.put(key,data);//store it to htable1
4 i B! I' ^2 I% Rnum--; + h4 _* l- v5 e7 G8 Y7 V
} 0 Z1 [3 o2 W- M f9 n/ {2 N
htable.clear(); ; L5 `1 ?& h+ L$ a/ U
enter=bufin.readLine();
* n- {1 b+ n; U) d8 s( A) B5 @new str14(htable1); . D* D# a$ A# F# y
break;
/ e l/ g+ `5 i+ M( [- [. zcase 5:
3 ], L4 I4 O1 o) ]( ZString[] sortkey=new String[count];//create a new sort array
% d8 _' B5 E1 i0 ?. f1 c& YString[] sortdata=new String[count];
- u+ v* j: }6 E7 z: X$ I, Cnum=count; ; `2 A/ M9 b* M; a6 U+ c% w5 j
elements=htable.elements();
/ r+ h* V/ G4 G/ Xkeys=htable.keys();
g5 W' |7 w- G9 Q" pwhile(elements.hasMoreElements())
$ z- f, m% ~8 V! w{ $ G; K" q# v+ v5 U$ q
key=(String)keys.nextElement();//fetch value of key 4 H& j9 Y/ ]" b$ O! u6 M
data=(String)elements.nextElement();//fetch value of data 0 y6 s% V8 p, r! H+ g0 b
sortkey[num-1]=key;//store the value of key to sortkey array
7 {8 d' _9 `1 z/ R6 \4 }9 ]sortdata[num-1]=data;//store the value of data to sortdata array
3 H t& F. y1 D4 M8 V+ o+ onum--;
1 E, Q# l$ }; F* y/ V6 n}
2 X% s) q) K, n" `2 I, N6 aSystem.out.println("3 x, S1 F2 A& q' a7 G8 ~
编号"+" "+"内容");
$ t5 J& K" {0 F3 l# Qfor(int i=0;i<count;i++)//display all content after sorting # [6 L; p4 B0 x! q
System.out.println(" "+sortkey+" "+sortdata); ; i9 X5 N0 S! M6 w. {) b
System.out.print("' j" |4 l. @- _9 i5 ^8 X; ]
目前共有"+count+"笔数据"); * u9 P$ i3 ]( b. Z2 M3 y
System.out.print("# ]9 F, s) C1 K
/ k# d" w+ [) H7 w& g+ q4 C
按任意键继续...");
( ^) \4 F# x7 u2 |6 \ f; senter=bufin.readLine(); * u& S; E* n! m/ _
new str14(htable); 6 @! n! i# Y8 U( k# T
break;
* \7 w- V- G) [3 e1 t' ?* Edefault:
9 p/ L v: Z7 }0 F2 Q1 Q4 `} , F; V, W6 S& f5 w" R
}
/ }& k+ i8 r" m- F& ~+ h7 }! t* p% a
public int select() throws IOException//method of getting a function selector
4 v/ ^/ A: Q. H) {$ r* y/ V{ 0 w. y+ e& {( w. ~. i% V
String input; 7 S5 p! `; t4 O8 f
int value=0;
3 N! ~8 H- `' T. Z) b+ ?- D: L( Iinput=bufin.readLine();//read a input from keyboard
) f# {: _3 C1 ]5 x0 @try ' y- {. A4 q- p+ Z- Y# l3 y
{
( M7 i8 b$ @! U6 J- o2 Svalue=Integer.parseInt(input);//convert a string to a int value
# F! U1 ?' k$ M5 A& }}catch(NumberFormatException e)//can’t vonverted 5 I: d2 w* i/ W3 Q: H
{ 3 Q0 J3 E. u+ q. I ?
System.out.print("请输入选项1~6:"); 1 J- ~: C& }3 X! N' z
value=select(); . r" R v9 W4 y' I
} : _2 p& P. Y& _+ i% t! H9 h
if(value>6||value<1)//if exceed then print a message and reenter ; \8 S! d4 i: m& i. z3 {/ j
{
3 ^+ H2 J5 O, k6 e6 k# ySystem.out.print("请输入选项1~6:");
. f+ J& b( V) h4 y+ s' J5 t2 ~4 X: evalue=select();
8 ~8 D6 Z; \1 p$ [( ^) V/ o1 A} / i) G( G* a- u
return value;//return a value
7 R1 ]; U2 o/ ?3 U* `} 4 k# n: y3 v: X5 Q7 {: E( H( n
0 ]3 y% R7 k1 b( X8 J2 r, }( }
public int getid(int count)throws IOException//a method of return the number of data * I/ S$ J9 d7 t3 o7 ~0 B# m* t
{ 2 O( d! Z' A; u3 Q
String input;
/ B: d5 t J' g9 f! Z aint value=0; . s, j# r3 C# u2 @& k' w
input=bufin.readLine();//read a user input string from keyboard 3 ]1 W8 Q, U8 o5 ?, e7 @: w) n
try
8 w" X; A1 {. Z/ y& x4 J) a{ 8 c' }, A# h+ J4 n6 Q8 |
value=Integer.parseInt(input);//convert the string to a int
6 H2 i) m( ?% T9 V}catch(NumberFormatException e)//if can’t convert to a integer then reenter
6 d0 G+ F& H2 V! n) O$ W z0 I{ 8 ], ?. q: O y
System.out.print("请输入数据编号:"); 3 s3 j# Y4 l. t
value=getid(count); Y) w, k9 D5 c6 s( P# B3 ]: y
}
# S" ?8 ]3 \! v+ E9 Aif(value>count)//the input value is out of bound 0 g% R2 W8 C% p4 s, B& ?" d
{ * Z3 t3 i9 {$ T
System.out.print("无此编号的数据,请重新输入:");
2 }9 A5 q- e$ W1 rgetid(count);
1 Q* Y. z9 k: t/ M9 G9 O9 [4 l1 |, v} / w0 K3 B; y: F
return value;//return a value ) b4 {+ u' n% G) J
}
7 `# \3 Q$ Y$ [9 p/ E} |
zan
|