- 在线时间
- 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.*;
4 v# G, b% g6 H- Timport java.util.*; , k# F2 z% F' ]) x: o
class str14
9 `& [+ m' r& w4 r' j2 M{ , E3 L4 k: W5 Z* z7 W( E0 u, n
InputStreamReader stdin=new InputStreamReader(System.in); + {: i- m( ~2 _( ]% T. |
BufferedReader bufin=new BufferedReader(stdin); 3 A* ]4 @' B) b/ s1 ^# `3 T1 S
: T& n" d2 h0 S
public static void main(String args[]) throws IOException
, X; N; X$ \6 {+ t+ V0 D. g b{ , Z% G( U4 `1 B: c
new str14();//run the application " B3 ~/ {4 J9 s/ D, h9 v% ~
}
- }$ h/ `& I. p% C' r6 f, h' u9 Z1 \# \% e/ p4 |- c2 u( u! F
public str14() throws IOException//constructor 3 a' Y) c; D+ `
{
- l# c1 |4 G: A1 A- U2 t+ bHashtable htable=new Hashtable(20,0.75F); 2 ~: ?, D8 Y" Y' Z; `- X
dataBase(htable);
$ Y7 h0 o- F: Q1 l} 9 S9 J; G; U# r
b* h3 h- @- e- ~) ]' a1 u: k$ Y
public str14(Hashtable htable) throws IOException //override the constructor
' i$ m) t# l$ r7 [5 |{
& U. T1 P8 U% e2 a8 _1 jdataBase(htable);
1 I: f& t+ Y3 D}
0 \& Z: V u! S3 V6 V6 ~
8 b* ]" Y4 ]! E) _3 H9 qpublic void dataBase(Hashtable htable) throws IOException 8 }/ f7 e9 M- _" P3 J
{
, M. i$ o5 k; o. sint count=htable.size();//get the ammount of the data in htable ! ~% N" p& [6 ?4 Z
int value,id,num;
& \7 W# Z" y$ B) L. k: C5 FString key,enter,data;
; G" ]- w+ ~) i0 C. {! qEnumeration keys=htable.keys();//get all the key in htable
& w$ b* W9 s& F' X8 ^' \6 k3 wEnumeration elements=htable.elements();//get all the elements in htable
" `- ~" S8 u8 x% H( WSystem.out.println("0 ?1 |; D, [9 }- `
Hashtable 简易数据库程序0 {8 e: e# O5 F
"); 2 ?2 v8 H; T5 N% J7 T+ {( b* D9 O& I8 H
System.out.println("(1)输入数据"); 2 i; r0 O3 U& B- a! b
System.out.println("(2)请除所有数据"); : @$ ]3 [# h/ C1 Q
System.out.println("(3)显示单笔数据"); ! o5 B9 D5 g b W, c
System.out.println("(4)删除单笔数据"); # P+ Y9 X. {8 a
System.out.println("(5)显示所有数据"); ; x% g3 W4 @# m# P" Q" v$ r, Z
System.out.println("(6)结束程序"); 8 [, V( w6 j7 y4 T, M
System.out.print("请输入您的选择:");
! e3 r) `. g7 k+ `( l- J4 ~value=select();//call select(),return the function number 0 s/ n, r* K1 [' P8 i6 I( d/ j
switch(value)//the function * u: i7 Y: \& [1 t$ h3 y# G
{
2 s3 e# A4 N% y4 {( Jcase 1: & R0 \9 j* j7 K0 R
System.out.print("3 j+ `! i+ a- Y |7 @" p
请输入一笔数据:");//need data input 7 g% n3 O i/ M, {% X% `3 j9 J" ^
data=bufin.readLine();
9 o% C L* h; r/ vcount++;
0 G/ }; y3 U( O \" P( }, Akey=String.valueOf(count);
0 V# \1 i! E. }3 X9 vhtable.put(key,data);//store it to the htable
2 x9 [) |, [& w6 |0 ^0 T1 YSystem.out.print("8 _ o6 n2 O& e. d, e3 O
输入完成,按任意键继续..."); , u5 @9 ?8 y% `' ~; J6 I4 J
enter=bufin.readLine();
: q! ^& Z) B; y' d6 Y- Bnew str14(htable);//reenter
- l$ I3 P+ p! t, S; F6 rbreak; @! M- _0 D2 d
case 2://clear all data from the htable ' F$ H# M5 v. a- h; p) A8 O
htable.clear();
a: A" o# N$ X% K0 C; d3 P5 A! CSystem.out.print("
$ E% a ~( j" g3 z M, s已删除了所有数据... 按任意键继续...");
+ ^4 T. W+ D* ?) u- ?7 Genter=bufin.readLine(); 8 H# s( W" ^4 v; C6 e
new str14(htable);//reenter & ~3 A3 b* \/ l$ ] t3 N% [8 n% a- _
break;
0 x, |! ^7 z6 ycase 3:
2 X8 Y# F3 M9 D, M4 X0 y% G. `# bSystem.out.print("$ l( x8 ?7 ?. f4 C* x1 A0 l
请输入要显示的数据编号:"); # W- v% a2 d8 S+ \% s1 Y
id=getid(count);//call getid()
8 Z5 O | F( N# G$ |key=String.valueOf(id); 0 g, _& S' u4 ?) G; V% n
Object select=htable.get(key);//fetch the data from the htable
- Q" D3 c: n7 B' Y/ w. gdata=select.toString(); * E8 [% Q( Q$ w( r4 J& j
System.out.print("8 h# @/ s. F: z e
编号"+" "+"内容"); ) G, u: c' E1 d) G
System.out.println("" \: r$ c3 Z/ ^, B% H& A( m* `8 ]
"+key+" "+data);//display the data }0 N" N$ U- [' m+ U# J2 o1 \
System.out.print("
9 |7 r4 K; b5 e2 R按任意键继续...");
6 W: h! V# o- U& benter=bufin.readLine(); ) M0 j2 `, w' {& d! H2 }
new str14(htable);//reenter
" n }- k- O) Z' E G/ O4 Sbreak;
3 v* K, L, I) Hcase 4:
3 _ h+ n! m" M% o0 _5 m: bSystem.out.print("请输入要删除的数据编号:");
+ p/ W' P& p6 g; T2 q; @" S; Mid=getid(count); 5 J; i+ B F. i3 V6 U
key=String.valueOf(id); 8 q+ N# c. h3 O* w+ u4 L9 i7 G
htable.remove(key);//remove data A. g- i1 {. p' s8 M
count--;
* f! r4 i6 e- n$ N9 Z1 Z1 h4 H/ Q3 e3 mnum=count;
& ~, }7 J6 S; u7 v, O+ ]/ c' sSystem.out.print("已删除了所选择的数据...按任意见继续...");
% t% }9 h- ?+ k! E9 c$ Q5 L1 rHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 * ^% m/ n' X# F% O1 z3 o( V% O6 u
elements=htable.elements();//fetch all the data from htable
9 }. \. g+ b" ~- P! E. y+ Xwhile(elements.hasMoreElements()) , |8 I! p d' K% x+ w
{ 5 ~4 l$ b% P0 U, A6 a$ _. H
key=String.valueOf(num);//a new value 1 A) t6 A4 e, _$ V, |( H L' h8 d; {
data=(String)elements.nextElement();//fetch content of the data
+ p/ O' X9 g3 C% thtable1.put(key,data);//store it to htable1 9 a! l" g/ }& v+ b( G
num--;
+ ^. [, h8 V( _}
2 s$ f. E: y8 L5 ]4 |htable.clear();
$ y$ k0 d, y- _4 Penter=bufin.readLine(); ; \) Q: b- Z0 A% S1 `" N- m
new str14(htable1); / W3 {' E$ P2 r$ y2 N
break;
$ n5 Y$ _ v# {4 t! m# D/ d1 H, zcase 5: / \3 R4 |- g5 H7 ~
String[] sortkey=new String[count];//create a new sort array
$ ]$ \2 q- ^! h5 U, dString[] sortdata=new String[count]; 6 L. V7 }( M; m, q
num=count;
; L B |2 i7 c4 [8 f* D' i1 welements=htable.elements(); * G2 q8 D/ [& y( k& Y6 X
keys=htable.keys(); ' O- d8 Y( h* [' n
while(elements.hasMoreElements()) " N' |- Q8 ]( I/ t7 S9 J0 S. l" M
{ 1 ^3 |; m* }" {9 J( z5 L
key=(String)keys.nextElement();//fetch value of key
- t/ A; |" O2 q6 L# U, wdata=(String)elements.nextElement();//fetch value of data
% N1 a6 _/ b& o& x* l! x8 N1 i Csortkey[num-1]=key;//store the value of key to sortkey array 4 J6 u# p, T, _+ V( |
sortdata[num-1]=data;//store the value of data to sortdata array 1 k' L- Q w8 K7 P# ?: R+ ~* {
num--; . t( z) X H. q. c! T
}
6 L* c+ B# |, C0 TSystem.out.println("5 D' ^; ?! T! D( H8 o4 |* q; a6 T
编号"+" "+"内容"); 3 U2 L# P9 [8 c- Q* f
for(int i=0;i<count;i++)//display all content after sorting 3 o/ j4 `, L9 }+ f j
System.out.println(" "+sortkey+" "+sortdata); ! g. [6 D. b7 R2 j7 W. O! N
System.out.print("
& v( c; j+ D! G2 d( _& z$ S目前共有"+count+"笔数据");
/ I+ `9 X- ]3 K4 \8 V. {' dSystem.out.print("$ ` ?" q% Q o
2 S- x( o N2 H; ? B! t4 n' s x
按任意键继续...");
+ Y- K* Z, K2 G2 g. Y5 X- }$ m& d6 Nenter=bufin.readLine();
- {3 G3 W( e& E" g% knew str14(htable); 3 C% Y! u; l, f0 ]. l8 {
break;
9 t" v' i* U! J; w! g8 z+ p$ vdefault:
& J! m Z" z. T}
6 P) L6 b4 D6 S# C4 u}
- \7 t* B1 W7 j4 F$ ]0 N
% k K; g) u; j, Vpublic int select() throws IOException//method of getting a function selector
& B. F$ N y" v2 c5 _" f3 V1 i! ~# o{ # R6 ~) P9 D0 m" @2 h* H+ {
String input;
5 j0 q- r- A# R& c9 a! W' L1 O$ lint value=0; , ~0 ]9 k) M! W
input=bufin.readLine();//read a input from keyboard % R5 m! r, @: x
try ) V d" i3 \1 A; B
{
6 | \1 C$ q0 ~7 C0 H, Ovalue=Integer.parseInt(input);//convert a string to a int value {0 U& B, O$ j: V
}catch(NumberFormatException e)//can’t vonverted 2 M7 z9 N. N3 i+ s+ Y
{
+ |6 S* x+ U2 t4 ESystem.out.print("请输入选项1~6:"); / @3 ^" {5 i I0 I+ N/ x
value=select(); & i6 \0 N- M% b7 Z8 [% U
} 6 M) g7 X. u, h7 R
if(value>6||value<1)//if exceed then print a message and reenter 9 b& Q3 I! v: `8 J& S
{ # H) \6 D2 ~, D$ H; q% K
System.out.print("请输入选项1~6:");
) H0 ~1 [, k/ s% u2 v1 Wvalue=select();
! k( |, E. i( t} " [5 k+ i/ X3 \, D. p+ \
return value;//return a value 5 l" W8 \& }% y% @
} # c2 \- I( T# ~# j# i6 f7 f& q
! P# Z; R" L4 ?- v" X' V* M: Bpublic int getid(int count)throws IOException//a method of return the number of data
2 u- e& b7 @& r0 r4 y{
: \( D' J, y) J# v1 y2 L1 hString input; 3 C! k# n+ L8 w
int value=0; - ]; a. |4 e3 O9 D: f0 L
input=bufin.readLine();//read a user input string from keyboard
1 s, E2 T( ^" x% `' i+ Gtry
( ^8 p* ]2 c4 U$ \$ E{
$ Q# {: v/ n) Q6 Y4 Dvalue=Integer.parseInt(input);//convert the string to a int : n# E6 {8 b/ a# z
}catch(NumberFormatException e)//if can’t convert to a integer then reenter ) L* l4 [! k; R; u- v% e3 M5 T3 @& z
{
" J# e( {+ B1 n. s) O/ h6 j7 |System.out.print("请输入数据编号:");
$ `$ N: P# u+ o0 ]; ~6 n1 C, Cvalue=getid(count); 3 q1 V9 r" ~, F5 G6 A4 ~/ k
}
4 D; c* h' F- i, Y( h/ f+ p& P- zif(value>count)//the input value is out of bound 6 J* H, s! ^ t- T
{ 4 H3 |" ^: R9 C5 L' A; V9 y: O& J
System.out.print("无此编号的数据,请重新输入:"); * C2 @+ T+ Q" X0 y- X+ M) I
getid(count); P1 F+ o1 \% t% f
} % I8 B6 _. ]8 e) V( i c$ I
return value;//return a value
7 j7 M9 j0 `: y5 W' ]}
6 S+ U4 ~0 f/ g} |
zan
|