- 在线时间
- 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+ @' W( P2 J/ ^import java.util.*;
' q; _ y6 m) s$ Vclass str14 2 U2 F# Y! w% R) o5 p4 E
{ - V6 W. ?- d- W6 e
InputStreamReader stdin=new InputStreamReader(System.in);
2 |! Z k; b, E9 U' T/ qBufferedReader bufin=new BufferedReader(stdin); 9 Y5 Y+ K5 t/ O' t
7 M6 k! |& s1 npublic static void main(String args[]) throws IOException
& Z5 j b, T8 n$ ]7 g' q{
, W" C5 Y9 M3 c( h+ [$ gnew str14();//run the application . C2 m7 ?3 E/ b* c1 r
} ) `" c/ j4 i# l3 ]! A
3 u. \, V2 ^7 a0 J& X5 F
public str14() throws IOException//constructor * \1 k, t. \" q7 ?1 T. \- a4 ^/ l, H
{
2 h4 ]! P: Q2 {* V& s, M! t7 F! M9 DHashtable htable=new Hashtable(20,0.75F);
; p5 O* U9 s! f* e3 w7 q1 @) ? tdataBase(htable); * k, r+ e6 R& ]6 y! l6 a7 N
} 2 W) X9 U( X# e
5 U2 R, U! z( q/ k% P6 o) h# Qpublic str14(Hashtable htable) throws IOException //override the constructor " I6 W9 q: F+ C5 {
{ 4 L2 U+ M1 u3 g- R+ ^8 }. V, i& w
dataBase(htable); & ` D( J* i2 r* F+ u8 @
} 9 ^4 `* R, h ^
. U$ |8 _7 t+ k* r3 {2 G0 Dpublic void dataBase(Hashtable htable) throws IOException
0 ~1 `) i* ]/ ]{ 5 A! ~4 i; O6 ]9 R5 O
int count=htable.size();//get the ammount of the data in htable
& `8 Z: A6 Y8 b, W' k) S1 P# `int value,id,num; : o" c5 M- T& M1 h, R) z7 ~
String key,enter,data; 7 Y$ H3 {! i' r2 ^& ~
Enumeration keys=htable.keys();//get all the key in htable
8 _, \. P0 `- F/ K0 j0 bEnumeration elements=htable.elements();//get all the elements in htable 1 |1 Z+ V5 D: n4 X: y: q1 W
System.out.println(" N/ i8 [2 J# K# f/ T
Hashtable 简易数据库程序9 @ X6 l7 h# B1 J' d7 n
"); 2 f7 Z' v* ~; A0 F" H. [
System.out.println("(1)输入数据"); % I. h" x# y$ S+ Q5 D
System.out.println("(2)请除所有数据"); + w* J ]9 j" o
System.out.println("(3)显示单笔数据");
( @, O" S& h6 e LSystem.out.println("(4)删除单笔数据"); ' t( V0 X2 ~" Q- J, k
System.out.println("(5)显示所有数据");
+ D1 N0 ]; F4 {, V1 A: N j zSystem.out.println("(6)结束程序"); ) i6 R5 H5 X# [
System.out.print("请输入您的选择:");
7 a- @* K$ Y3 K! ~, svalue=select();//call select(),return the function number
* u8 P: O2 a& `3 C& ^switch(value)//the function " ^" H; ]# j. F2 W2 Y
{
' o+ g! l/ f, V) d: y8 ^3 g5 ccase 1:
+ X. i M3 `) O# V7 t1 R) p6 ASystem.out.print("$ J) ^* Y; X* U. W! \$ Q8 R
请输入一笔数据:");//need data input
# ]2 [; _$ P6 y* {9 Bdata=bufin.readLine(); . P4 f: ?# v5 k; Z2 a; ~) v; s
count++; - @. c; z' b- w) d- K- o* z1 j
key=String.valueOf(count);
$ k. V9 { R! Shtable.put(key,data);//store it to the htable 3 T+ t) a8 _/ m3 y
System.out.print("8 |+ e* j0 q. v0 ~
输入完成,按任意键继续..."); ) j1 |3 S$ N; W# W% |) @# r
enter=bufin.readLine();
1 D7 v7 l& f5 p& K2 `3 U1 inew str14(htable);//reenter
. K3 U1 W$ }5 w% Tbreak;
- H' u, j i4 P1 Gcase 2://clear all data from the htable
" `. H8 o0 A: M7 Ghtable.clear();
J0 x, l. s5 a. l& |! O4 D! QSystem.out.print(") U# M0 m9 x$ E0 k3 Z
已删除了所有数据... 按任意键继续...");
; U+ ^, A+ D9 `+ l* O# j) Lenter=bufin.readLine(); * n4 R- w! B. r
new str14(htable);//reenter
, t& J# i+ L* D( F2 q( L$ h, J Hbreak;
: E+ Y, K: u4 s4 b8 y3 O1 T+ Lcase 3:
3 H0 P" {! Q1 M. Q" F) B PSystem.out.print("
4 B& d2 M" l* |1 v3 E请输入要显示的数据编号:");
. @7 A0 j8 w" \8 o% O, l5 s4 O0 ?id=getid(count);//call getid()
+ u/ D, a" d, E5 w5 ~5 a4 vkey=String.valueOf(id); 6 k" N4 I' T3 V! W) f% A7 [5 d1 S
Object select=htable.get(key);//fetch the data from the htable ' G( s+ G+ Z$ U4 }" M' p
data=select.toString();
: }4 h, X# o- o. Q5 KSystem.out.print("
" j7 v6 F) Z" C, F* ~编号"+" "+"内容");
8 ?( F5 Z! z' [% B7 aSystem.out.println("6 a' K; P. d5 A- @" N
"+key+" "+data);//display the data % k; K) F0 u( U9 }3 U
System.out.print("+ s. Q0 I+ j8 a" a
按任意键继续...");
: P) N! J% d" V% N: i: A5 aenter=bufin.readLine(); ! _( E4 } F# ?1 r- `
new str14(htable);//reenter ( I0 C" n9 Q4 V+ V# ?. X
break; 4 v6 h( g5 S* ^, P
case 4: " R* f# g8 {3 E% j! R6 Q
System.out.print("请输入要删除的数据编号:"); 5 O# v( e. c% p4 O8 D. B& t- ^
id=getid(count);
4 {' y& c4 y* [key=String.valueOf(id);
& B5 v5 Q, |/ Ihtable.remove(key);//remove data
- O6 |; r! E7 ^; xcount--;
6 C% q O! z, G( Y0 ~, x8 Mnum=count; - `) \ {5 S5 G2 s* b
System.out.print("已删除了所选择的数据...按任意见继续..."); ' F' G R/ T% N9 ?5 p* P6 m
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
. M$ s; w$ T4 E% T: X4 U& Belements=htable.elements();//fetch all the data from htable 5 l* Z p: B& X; a) `- |# X# Q; ^, ~
while(elements.hasMoreElements())
9 A6 W; \' J* q& P8 q{ ) F0 N5 f/ Q* O4 y- W, c
key=String.valueOf(num);//a new value 2 v& m8 s0 }& p
data=(String)elements.nextElement();//fetch content of the data ( C, l; `( T& [; k- L8 q# i- R4 s
htable1.put(key,data);//store it to htable1
- J, ], [) Q( h" B* Y1 V; }num--; 0 R# o' t) _! T5 h. D$ b- ^
} : x% m2 i) L3 J# C" g8 w. F
htable.clear();
' S1 U% X! J' q. J- j$ Nenter=bufin.readLine();
% \! n* }7 s$ x# U h- |6 inew str14(htable1); ; n% b0 C% a7 `- o
break; - _7 \5 X, m# U/ O4 k
case 5:
- a! D: U/ r* R) KString[] sortkey=new String[count];//create a new sort array 4 X5 Q" f7 G5 d9 g% z ~
String[] sortdata=new String[count]; - y1 j9 H( y& z, U
num=count; 6 ]/ t4 n( q# j7 f
elements=htable.elements();
1 ^9 _. d) y7 r: Skeys=htable.keys(); - q5 M: d; n; x( D2 T
while(elements.hasMoreElements())
% l' _# D8 O. m5 U. o* r8 a{
% u2 G, s' `7 V: x8 Qkey=(String)keys.nextElement();//fetch value of key
) _6 O7 I- `' h$ c6 P5 pdata=(String)elements.nextElement();//fetch value of data * n# ]8 B. p3 e5 Y- X* a% u
sortkey[num-1]=key;//store the value of key to sortkey array
@" q- | r2 Z& \7 t3 Bsortdata[num-1]=data;//store the value of data to sortdata array
3 @0 e+ d6 a2 J7 |7 bnum--;
3 s" Q+ ^. X* n9 K2 [% }1 g4 W' A}
) C2 B+ @7 \* F) E3 LSystem.out.println("
2 T/ a" O* m2 n/ a) T3 a% Y编号"+" "+"内容"); ) N" v6 B# I; F, P8 a" y
for(int i=0;i<count;i++)//display all content after sorting
* U* Z0 n2 Y) K% qSystem.out.println(" "+sortkey+" "+sortdata); 5 C ^* ~" u2 ~
System.out.print("
0 M! M' M. j* x6 _ u目前共有"+count+"笔数据");
" C7 e! V' a, B' uSystem.out.print("7 @$ r7 x7 V [6 P3 |2 c2 o
0 u' }3 E6 N+ a2 F9 L. }按任意键继续..."); % G& b% p% h: l" X- P
enter=bufin.readLine(); % c2 l- p* D3 d
new str14(htable); 5 t6 z; F3 W* v
break; 7 k/ X" M$ \0 D$ C* I+ J9 W5 T
default:
) A# y$ g$ @! P: t [8 u( j: y. M} 8 z# `5 o! v: H& J# r2 K
} n2 l1 t) e' M
) T8 M( c- D9 s- L$ opublic int select() throws IOException//method of getting a function selector - h9 J# d, a6 k1 ]" i# Y
{ 7 G0 p4 `" l: ~0 r+ }6 H
String input;
3 ~$ z2 h: {# k% ^int value=0;
3 _5 y/ ~9 W4 C* h linput=bufin.readLine();//read a input from keyboard
9 H- ~: I, H0 A! Vtry ! `3 _% C/ [+ _8 ~+ w+ e- B! y+ Y; l! R
{
/ q' x+ G0 V& @# nvalue=Integer.parseInt(input);//convert a string to a int value
% m" P1 j9 m1 j- }5 [}catch(NumberFormatException e)//can’t vonverted
. G' [8 {$ O, R5 C {& s# _6 F{
& I! {+ |$ D2 W% K+ a* k3 XSystem.out.print("请输入选项1~6:"); 1 S R1 E- W/ P2 n, q" |! \; I
value=select(); , F4 M7 \5 Q2 d, X
}
$ N/ q4 @ d9 @8 k& |if(value>6||value<1)//if exceed then print a message and reenter ( h7 v0 T, l# D: d, P
{ ' a6 ^6 ^; Z0 K" R; y
System.out.print("请输入选项1~6:"); : W! ]0 o: p" f8 {) F' |
value=select();
: Q2 i; C" H. J2 a" F0 m6 T; g: c9 R}
, c- z: P& i/ x" L" d7 yreturn value;//return a value
, |9 m/ S) W* B1 w" R4 }% {} + D: m( \ h/ D- D" E0 P
% g6 `2 I f. g) o7 e& |' r( mpublic int getid(int count)throws IOException//a method of return the number of data * B+ \" C1 d8 p9 C4 ^$ I8 t
{
9 r2 g/ J% p& K/ ~' m/ zString input;
5 A, n- s3 f! j6 c% r5 u6 x0 P1 Zint value=0;
+ q( u# [) H1 \2 r finput=bufin.readLine();//read a user input string from keyboard 2 k( r) @/ B, Q; i$ w, b
try - Q: T! Y: o: q9 b
{
. R2 n: Q! l }value=Integer.parseInt(input);//convert the string to a int 8 d% ^6 E" v! g3 I& G. M) n; s
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
% j T: `/ u( \2 r{ " @& l8 w L6 s+ K
System.out.print("请输入数据编号:");
3 r r- E R4 |value=getid(count); d+ c6 r* |; _0 h
}
$ c7 q7 C" \- ]- |8 c& J( ]6 o; Q2 tif(value>count)//the input value is out of bound
3 I! [0 a2 R; p" G+ T( p2 X$ T{
+ {( @7 w: l6 h2 g9 RSystem.out.print("无此编号的数据,请重新输入:");
) l3 E% S+ X) c! X3 F ~getid(count);
/ Y0 x& o! u# C+ a6 i} , u' D4 f8 I z, Q! H- b% [
return value;//return a value
; S$ a% G" |. X/ G7 f}
' y8 F% q% z$ c} |
zan
|