- 在线时间
- 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.*; ; g) x2 k3 O: r5 u0 \
import java.util.*; ( b. X! X& |! Z" w: C4 l/ @2 o
class str14 + N3 _' i5 F$ U# Z0 H2 q( F' I( }
{
3 L7 b" t4 h) A k( l$ c" Y5 b8 OInputStreamReader stdin=new InputStreamReader(System.in); 8 |# b3 z8 a8 U. r. n
BufferedReader bufin=new BufferedReader(stdin);
3 d5 h- R6 Q2 f! ^0 V
8 m$ ]4 r3 Z! { T; ? V2 a( Hpublic static void main(String args[]) throws IOException
- ^( \; Q3 T8 |8 Y% ]+ a{
" r3 Z, V7 t+ @6 |" @7 ?! t5 ?new str14();//run the application
m5 E8 B* k" b4 v% L}
9 X" B. G8 \. q4 n, Q9 r1 G0 d3 q: B% F3 `, i( y
public str14() throws IOException//constructor 6 G; r1 {+ e6 B% e
{
, Z5 n/ j% E) _$ \) gHashtable htable=new Hashtable(20,0.75F);
: `, d* k! w4 Q* H- O, IdataBase(htable); & z0 G0 n' Z, F0 @
}
; K& ^$ b3 s5 g
! C0 I" C( H/ I6 R' q `4 q6 S5 H# ^public str14(Hashtable htable) throws IOException //override the constructor ' n- l7 y& V- K* J3 A
{ " l& F+ H& T2 t n
dataBase(htable);
# f3 X; @4 R& c6 O} & ^2 E5 Z N, T4 I8 T! k
9 i+ u) G: Z: Epublic void dataBase(Hashtable htable) throws IOException
% S9 g: q8 z7 ^+ X" r r y{
) ~! E, ?& B6 \2 u: @- ^# Q5 iint count=htable.size();//get the ammount of the data in htable
( q- |# S) s1 Q, P( R3 fint value,id,num; 7 E& y% ~) Y% |- i# i, e
String key,enter,data;
" k8 m3 Y5 J+ h9 }0 U7 p1 h& tEnumeration keys=htable.keys();//get all the key in htable ) u' i( ]3 c* E( H* }, ] K
Enumeration elements=htable.elements();//get all the elements in htable
9 ?1 }9 t1 t$ x7 i0 C1 a+ [System.out.println("; B0 o2 ]$ r8 d5 G/ P. D$ x
Hashtable 简易数据库程序
1 U3 p' x! V" K9 p; G4 w* U! }: x"); " X% K5 n2 P5 v( T& h/ k
System.out.println("(1)输入数据");
0 J) k) H9 R2 a9 _: G! S" s2 i" x, iSystem.out.println("(2)请除所有数据"); # i* S9 r' M& |) u0 ]
System.out.println("(3)显示单笔数据");
$ a% f+ }1 g+ xSystem.out.println("(4)删除单笔数据"); : I' k, S% n9 \( S a
System.out.println("(5)显示所有数据"); 1 C& j# s. u5 C* K4 t
System.out.println("(6)结束程序"); 7 ~7 M8 W5 a' j8 R; T$ C7 c
System.out.print("请输入您的选择:"); . @% C5 {# } ~1 D9 \( q
value=select();//call select(),return the function number
( ^+ m2 H0 v" O A; Mswitch(value)//the function , r( I2 U9 g% ~) r0 R$ N t6 H' V
{ 4 ]9 ~& u! v* g X+ l9 W- z- c
case 1:
% x8 m+ v+ `& Z' iSystem.out.print("
9 K1 d% e% b8 D: I$ G( e L请输入一笔数据:");//need data input
) [3 u% _4 ^/ d, t9 |data=bufin.readLine();
8 _" m1 r4 F) K* B& h/ {. [count++; 9 q) d, ~4 A" _2 k9 b
key=String.valueOf(count);
: v% J" H! V& H8 N1 ^htable.put(key,data);//store it to the htable 5 @5 W# l% I _" A8 ~# r
System.out.print("
! y$ T x) N' e" w6 p) y输入完成,按任意键继续..."); 2 y( x n2 s& w1 c( j, A! d( k
enter=bufin.readLine(); , ^. {! Z6 J" A1 w( t. q9 R- Z" S
new str14(htable);//reenter ; B* d- i3 M, C" Q8 y
break; 0 V9 U( g0 U. C/ M9 a
case 2://clear all data from the htable & I/ U0 q9 p0 U
htable.clear();
- K% y8 @2 ^+ s7 uSystem.out.print("- R/ J4 Y7 b$ Y( u) n' m. ~- d2 P
已删除了所有数据... 按任意键继续...");
- A$ o+ Z$ G: }enter=bufin.readLine(); ) @$ c4 m0 z/ }% _: ~, K
new str14(htable);//reenter ! @( H& r) v1 l4 f. V
break;
7 k d1 I2 {! ccase 3:
: a1 S9 k r% r0 _0 w- E5 c9 X" sSystem.out.print(") @ W/ g' w. O! h2 [
请输入要显示的数据编号:"); $ n$ M6 u$ d* E$ K) G
id=getid(count);//call getid() , @8 U0 v7 i) j8 a3 E
key=String.valueOf(id);
+ U8 s# f( k8 _* |1 e" bObject select=htable.get(key);//fetch the data from the htable
, X; I0 s( Y4 s5 Y( y+ E9 q1 y7 hdata=select.toString(); 6 H' Y2 e" u! T1 r# ^
System.out.print("9 ~5 j* O" y( y
编号"+" "+"内容");
3 F6 {8 ]$ o- ?2 ESystem.out.println(") f9 h' o9 P$ J, U2 _/ b0 J
"+key+" "+data);//display the data : U$ k) E5 [. _9 i$ f
System.out.print("
/ Q p' x( |& x: g按任意键继续..."); 8 D1 [1 Z( Q# J% O' u' G) M
enter=bufin.readLine(); 8 V- W& ^/ I. a4 }* \
new str14(htable);//reenter 3 ~# Z+ t5 S @4 v0 G2 c/ B
break; $ Y. v5 l# `2 F: T( |: U$ G
case 4:
6 X/ Z. t% l! i! Z F8 s) oSystem.out.print("请输入要删除的数据编号:");
7 L! [& t% z& `id=getid(count); 3 Q" t# F) a/ Z
key=String.valueOf(id); ' F0 n. x9 |5 O
htable.remove(key);//remove data % I" h- T+ l2 ^' d% q
count--; ) D# a8 b' ~9 s; W( q, k. S+ Z
num=count;
) v) {3 ^+ j6 D& T7 T7 a$ pSystem.out.print("已删除了所选择的数据...按任意见继续...");
; q9 w! K; B3 y! ]! |Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 - ?, G8 `! H3 i
elements=htable.elements();//fetch all the data from htable 9 l+ S8 s1 ~$ @6 z& e
while(elements.hasMoreElements()) 2 T- w; B5 V" t/ {' d0 h
{ * A4 W& T0 }+ n* I- k7 `
key=String.valueOf(num);//a new value 6 p2 u% n/ l$ A4 H$ ]+ T$ r
data=(String)elements.nextElement();//fetch content of the data , i% N+ F0 I' }) j
htable1.put(key,data);//store it to htable1
8 V! Y4 u+ F' i& {" d1 `num--;
7 _, O7 V$ W: @1 T; K3 W: _}
8 [, e# w6 c) f; n# z7 G* z- h' yhtable.clear();
+ @2 c8 I( b: F2 G$ ^7 wenter=bufin.readLine(); 9 {4 h2 j6 G, q7 ]. R
new str14(htable1);
4 L* V# D" T k- Qbreak; , j3 z7 d4 B; C. _/ Z9 P' N! c% N
case 5: ! _0 z- _' T2 z! ?! p4 ?# x+ r- L& V) X
String[] sortkey=new String[count];//create a new sort array : D2 j: s2 n5 e$ b) V
String[] sortdata=new String[count]; ) c% H6 c6 |" l5 G* m
num=count;
% Z2 B# V& Q" Q: o R- h" X$ melements=htable.elements();
. z1 W6 U& k& u0 Okeys=htable.keys(); 6 p+ Z0 E1 m$ ]' t
while(elements.hasMoreElements()) ; y) l: k6 s0 _. @
{
2 o/ r! X4 H1 j' H9 z# Jkey=(String)keys.nextElement();//fetch value of key
# e+ w* B- F5 d2 o; vdata=(String)elements.nextElement();//fetch value of data 8 d2 s. d! `. X) O0 _+ c4 Q# Z
sortkey[num-1]=key;//store the value of key to sortkey array
# A/ z, Y& q% M, `4 ^sortdata[num-1]=data;//store the value of data to sortdata array
+ c& o( Y7 ^' F% T" _num--; + l* x& Q' [" R
} % o8 P- a; @& j6 H$ `
System.out.println("6 I- V" B$ ]) w, {
编号"+" "+"内容");
# x+ f* r0 u9 X/ F; K# r/ S: u4 Lfor(int i=0;i<count;i++)//display all content after sorting # l$ a. i/ u) D x' }0 r" r) H, x
System.out.println(" "+sortkey+" "+sortdata);
/ ?$ q6 D' T }& F* x3 HSystem.out.print("9 o( @4 u p* L, I) \& r# W7 g
目前共有"+count+"笔数据"); 4 B' q; _' B' @) C/ B
System.out.print("
& S/ W$ y8 I$ u+ ^- h+ m! y1 y2 Z: Y Y: I0 C
按任意键继续...");
7 J% W( i7 Y, U6 S5 T! yenter=bufin.readLine();
7 @% D" x, J5 W. Z) P6 K( ~new str14(htable); " F# M5 m& G# ~8 M5 v
break; # j! A! q t) J# c
default: * S! z9 ?7 B4 b+ G# }
} $ q+ H9 |( [+ ]9 c. O, @$ q
} ( T# y' |" u- l' s* _ i/ T# m) d
! q' _2 r f7 w v% `5 t; L' [public int select() throws IOException//method of getting a function selector
. z+ X$ R u" U{
# v5 p& R6 i0 K1 w# Y$ g- p: `String input; ( U8 k- D( |' u) o, m( n
int value=0; & @; c( D3 [5 ]8 T9 T
input=bufin.readLine();//read a input from keyboard 1 ^2 H" O3 D4 H4 }. y
try 3 Q; H) Y8 N4 a$ l" L
{ L0 m8 y& z2 b# F; j
value=Integer.parseInt(input);//convert a string to a int value 9 O* c/ P8 L/ P& n9 H7 G
}catch(NumberFormatException e)//can’t vonverted . y- O) _7 }1 ~- _# K
{ 4 ?+ ?: I+ o7 l; Z. u
System.out.print("请输入选项1~6:");
F/ r1 G' |& V* v( Vvalue=select();
* c9 C& |" T# R} " m& E1 A) Y# B$ @5 Y6 G- v! c- @
if(value>6||value<1)//if exceed then print a message and reenter
( p; H' d4 N4 h, Q; l{ % M: D. Y, S2 ~ R0 ]
System.out.print("请输入选项1~6:");
- z3 h0 c! I) @value=select();
7 n1 w8 {& @ @5 s/ Y} 9 X3 l [5 e; s2 t+ Q; f
return value;//return a value
8 m+ r' g0 N+ n}
2 B9 ]: R1 y* _# b1 i. O' b+ U& ? S0 `) U3 j6 P
public int getid(int count)throws IOException//a method of return the number of data
- i$ v- D) k$ l) i# |: X{ 9 ]$ l7 @4 y9 \
String input;
, f1 ?$ k% Z+ E: @$ s0 O+ }int value=0;
9 k8 F, i. H6 V0 }) L; Finput=bufin.readLine();//read a user input string from keyboard
( ^! a9 F9 v" T7 rtry 4 X. K, _8 w$ V3 k# d; p
{ , L. v3 u3 c8 V+ k( |1 w
value=Integer.parseInt(input);//convert the string to a int 2 |3 J2 g/ D X8 @$ \
}catch(NumberFormatException e)//if can’t convert to a integer then reenter 0 }5 z8 ~* ^0 w4 n: a/ S
{
/ l6 S3 @( n& q" u+ C* q' K& wSystem.out.print("请输入数据编号:"); L6 {; f f% j! D' A7 [4 Z$ w
value=getid(count);
4 y L. [* P. y1 `, ?& r* J}
* L6 I/ \3 K7 d6 S5 w6 j) d6 lif(value>count)//the input value is out of bound & `+ D( R6 x. t) R8 k
{ C5 v2 e. ^$ {! V- o! M2 u
System.out.print("无此编号的数据,请重新输入:");
+ p' ^$ D" v& L% z5 Y& k& Dgetid(count);
" N- j! g! W, ?. R3 T( l} 6 l) z* m5 M; `4 d5 R- G# h$ Y
return value;//return a value
5 [9 N5 e- S& F6 T( Z& a} 3 Q* }5 d) x" |
} |
zan
|