- 在线时间
- 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.*;
8 {' i! n5 \ E8 h1 T! T' jimport java.util.*; % i& ^4 N# U) i: K
class str14 6 w$ N1 L& b" J5 b. q) S1 H, ?, y1 f
{ 6 f/ T# C/ u+ v; Z5 y8 q! N5 |
InputStreamReader stdin=new InputStreamReader(System.in); ; t3 {6 j7 A- f4 S7 |: i
BufferedReader bufin=new BufferedReader(stdin);
6 E( g% o( i4 ?& X2 V. j# G' f }& Z8 B
public static void main(String args[]) throws IOException - Q7 d/ p; Q0 y+ V4 U6 B% b
{
8 c. S- L0 p* @; w3 ~new str14();//run the application 8 E* X6 m" n! a5 I
} / t$ F/ k! B( X. x a" R1 s
& w# b! e9 ]* c% F# p+ t
public str14() throws IOException//constructor $ s9 }: x4 @/ f
{
0 |1 M3 \5 O- s% Y% ^! f! @Hashtable htable=new Hashtable(20,0.75F);
! l7 S$ T* V G- x; `: l/ P( W: ZdataBase(htable); + X4 s y$ u% H
} . I9 D: n$ ?) T, W) p
+ v! ]/ y4 X. F3 D& Apublic str14(Hashtable htable) throws IOException //override the constructor
% |( h' r- f% q1 e ~7 v3 Z{
/ D/ p2 a! D; XdataBase(htable); 4 _- j+ o/ K+ d3 r; z1 r# F1 m p7 {
} 0 C3 j& w" H& Z# b$ ^+ ~- i
7 T9 k" d/ Q, j s2 {2 F
public void dataBase(Hashtable htable) throws IOException $ m$ M1 j0 D( h0 d* Q( n
{
* y% _7 K6 c2 Z. \7 }" tint count=htable.size();//get the ammount of the data in htable : x" |/ r6 X/ @" ^& h# u b
int value,id,num; - y: ?7 z+ l; M9 h- F
String key,enter,data;
% x1 Z# H6 u, h7 }3 ZEnumeration keys=htable.keys();//get all the key in htable
2 J; u& V# v3 \" X% YEnumeration elements=htable.elements();//get all the elements in htable % D( [% Q5 {( M. U- b
System.out.println("
; ?- \, k- {5 k; v% C# \+ GHashtable 简易数据库程序
' F) x6 }" B, ~; n");
" c: ^4 B( S- a8 Y* m- sSystem.out.println("(1)输入数据"); * l' J# d4 M. }; @$ J: u3 N) T
System.out.println("(2)请除所有数据");
2 j$ `9 x/ e9 b- @3 _7 HSystem.out.println("(3)显示单笔数据"); , N7 Z* s, b, C, M
System.out.println("(4)删除单笔数据");
7 _8 T# D* _0 H5 JSystem.out.println("(5)显示所有数据");
2 V$ R8 F% M; G9 lSystem.out.println("(6)结束程序"); 5 M' K- l* p: g8 W
System.out.print("请输入您的选择:"); * ^9 q N7 ~7 W7 ~; V% G' }
value=select();//call select(),return the function number ! c2 O. R3 t, _' z. b% Z) Y1 ]1 |) H# I
switch(value)//the function , P( _7 d$ r9 w* q* M# V
{ . B0 P9 j k* h [ Z' f; y f
case 1: * b+ z! g6 | Q& `) A' r$ x
System.out.print(") c. }( }* j$ ?9 }- E$ M, p
请输入一笔数据:");//need data input
2 l/ G) j/ n8 ?% ], L: y5 z! d+ h1 zdata=bufin.readLine(); 9 J% `6 Z- ^% S' b- s
count++;
5 K/ z- M$ L4 T! S+ ?key=String.valueOf(count); , u# m& U( C/ R4 @! g5 ~& l
htable.put(key,data);//store it to the htable
- `" k. s$ Z( Q2 I _4 m7 `' L" T$ ]System.out.print("6 k' X) z! v5 I# S3 y
输入完成,按任意键继续...");
- t1 A# H, Q) Benter=bufin.readLine(); ( a% m d' G5 A0 Z' [ y
new str14(htable);//reenter 8 h' }! t$ n( C2 `5 V1 c
break;
* Q- k) Y7 N: G+ m/ ?case 2://clear all data from the htable ) {% w) S$ c. V4 }
htable.clear();
' L* U9 r5 F4 L% N* oSystem.out.print("
4 F. d) m, A3 @! }已删除了所有数据... 按任意键继续...");
4 E) \7 z6 b/ ~' wenter=bufin.readLine(); . T. c z8 \4 l9 W& f6 {
new str14(htable);//reenter 2 Y: c" X" c6 G
break; - z3 h& o7 Q f9 p) V3 C g" _
case 3: 4 s- S8 U# z! E: z4 R6 j# ?$ Q
System.out.print("
e: |$ B x ^* \3 ~4 ~请输入要显示的数据编号:"); : J% N# }& F" a( P3 v3 @. y
id=getid(count);//call getid() / d( B2 M) }% `' i3 g1 Q
key=String.valueOf(id);
/ k3 x: ^- ~" aObject select=htable.get(key);//fetch the data from the htable
! X; O8 B2 L/ X* t$ ^2 y- C. W- O2 edata=select.toString();
& ]% t' v1 u9 c& J Z! I! @" eSystem.out.print("
% o( r; m7 Z2 t- z+ u编号"+" "+"内容");
( G. f: B1 Y9 Z; ^- tSystem.out.println("
: B& v% x# [( r: h "+key+" "+data);//display the data
6 B8 ~5 c3 p0 L" c3 mSystem.out.print("
7 y5 v8 V7 ]; T# z/ U: `8 H按任意键继续..."); % R. b" j, A0 f$ K$ \7 J; C# u/ _$ ~4 F
enter=bufin.readLine();
7 i+ Y1 b% \& x( G* \3 A- u% Fnew str14(htable);//reenter
$ [' n4 p' S: R; k {' E# |& W5 bbreak;
8 }) y4 h0 ?% p( [2 Y3 H& `case 4: + B' A |. ~% I
System.out.print("请输入要删除的数据编号:"); ( M3 E# }, b* g0 ]- q
id=getid(count);
& q! R R! P4 F- l9 Qkey=String.valueOf(id);
$ \" Q3 z3 j# G& `) q/ Thtable.remove(key);//remove data
" O) w {4 H$ J) w4 T4 o$ fcount--;
4 L5 c, h$ }7 N- {num=count;
9 L3 n9 ?( l4 ySystem.out.print("已删除了所选择的数据...按任意见继续..."); 1 ^0 U4 }) j5 S& q$ B. Q. p9 @
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
0 i3 {6 @0 v y" t% a6 eelements=htable.elements();//fetch all the data from htable 0 k1 y- f- B8 H6 \/ ?
while(elements.hasMoreElements())
( G, R# Y3 E4 g5 k/ Z9 L{ + k" n' f& w' ]( d* l
key=String.valueOf(num);//a new value
, z* S) Y6 b% G& T2 a! gdata=(String)elements.nextElement();//fetch content of the data " N' \1 {5 J( Z1 w0 y
htable1.put(key,data);//store it to htable1 ( X6 i7 ?9 q0 ]* M% H5 M+ [: x
num--;
- }! w; S* C# @. h, f/ D' @}
5 T8 t4 _) `3 [. Uhtable.clear();
) _) I; M- u3 X$ s2 m8 w( Venter=bufin.readLine();
% _( _$ R( i4 c0 v7 U( ^new str14(htable1);
& D0 J% {: X9 N0 g Sbreak; $ s+ M7 s& C% F/ ^9 k
case 5:
/ K3 {% r6 z/ v' v' qString[] sortkey=new String[count];//create a new sort array
( L- D5 ~9 O* R" N, Z2 N% `$ @String[] sortdata=new String[count]; L5 M- j- J% n' V# T0 Q; }
num=count;
( U2 h; @/ R6 b4 l" t5 Nelements=htable.elements(); % J* [5 @$ n) ?) o4 v
keys=htable.keys(); " [/ U9 r- W, `2 n8 Z4 M. Q
while(elements.hasMoreElements()) 5 B& Z7 V; B( ?1 H2 g" k
{
" S. U! d5 p+ `; }# A+ |$ \5 kkey=(String)keys.nextElement();//fetch value of key
4 P% K2 x, |$ A5 I5 c8 M: ldata=(String)elements.nextElement();//fetch value of data
! ]/ L+ S9 ]2 [* qsortkey[num-1]=key;//store the value of key to sortkey array
4 N! Z5 O, V) D0 s5 Y6 Y$ p$ [2 @6 hsortdata[num-1]=data;//store the value of data to sortdata array
, `( C- M/ h: h! dnum--;
2 Q- i0 x& `2 u |: a} / T, k6 o0 [8 b
System.out.println("
* o6 R1 t+ X$ ` L编号"+" "+"内容"); : O: ?6 E" P# \9 K: k# X
for(int i=0;i<count;i++)//display all content after sorting
* {8 c/ F, S+ Y0 }+ @System.out.println(" "+sortkey+" "+sortdata);
$ ]$ y1 i1 i4 R( R; d/ NSystem.out.print("
- }8 }% R: A$ ~3 h3 ]目前共有"+count+"笔数据"); 1 @# _; c! @" c/ c# u( c
System.out.print("; `9 `( X# ?; g9 `' ^
/ P, Q- T% O4 ~: T$ t- ]. @- ^按任意键继续..."); {' B- a2 k9 ^( k& ~ }
enter=bufin.readLine(); 8 v% V& z: N3 n+ N! L4 t r
new str14(htable); - d% ]( b0 _- G/ m# @
break; & R- |0 v; g" ^4 z; L
default: ; ^! v9 Y4 p) z9 s, F' W( \3 \( N
}
) z! B; V; e4 X6 W} " ?3 G( o5 Q0 }+ o1 C
2 m5 X" C! U( v! w- Ppublic int select() throws IOException//method of getting a function selector 8 @9 V3 Z: ?" K* q' T, `/ x
{ & ?- O8 l5 q% E" X$ G% j
String input;
; S& {/ m$ S/ Q' e; dint value=0;
( k4 D$ H. m5 N6 d3 dinput=bufin.readLine();//read a input from keyboard 4 I- ^6 Z' j1 g$ v: R; f) N
try
. U8 m1 h5 H1 y j* o{ 9 {0 f' L, b: W' ]3 ^6 e r3 g/ Y
value=Integer.parseInt(input);//convert a string to a int value _: p0 C9 ^" o, U# @0 I( D
}catch(NumberFormatException e)//can’t vonverted
, i% f6 ^; U& X{
6 S& |, I$ P; aSystem.out.print("请输入选项1~6:");
2 V+ X$ L! V& y" wvalue=select(); 9 ^6 H9 f0 \: ?5 s: i
} ' t! I" I+ z$ s
if(value>6||value<1)//if exceed then print a message and reenter
' C9 i# L. P0 Q* U" ?{ 2 Q1 N2 Q2 ?: ^3 H+ p7 k" z* R
System.out.print("请输入选项1~6:");
0 s: F. G* Z! avalue=select(); 9 K' v# e+ g1 N* H- O3 _
}
/ I N8 R1 @' @5 Q% E% v7 wreturn value;//return a value
1 f9 {8 H+ Z1 h6 c6 c5 W& G6 j+ u}
1 F+ h/ c, ^1 x7 u: p/ D2 z6 V) J4 _, b, c' R) T& p
public int getid(int count)throws IOException//a method of return the number of data 5 Z8 |+ S8 X- T4 ^1 ?
{ $ T' ]" T; J) k) g
String input; ( L) a/ p- s3 E( y5 q! Z k
int value=0;
# e5 Z' @& V; O: j2 b; e& rinput=bufin.readLine();//read a user input string from keyboard
/ i# t: `4 A9 gtry ) c% V, `1 o/ R( \8 K; x
{
& _ y: d; t/ }( M1 rvalue=Integer.parseInt(input);//convert the string to a int / p4 h9 ^& o% W. k5 J t) D
}catch(NumberFormatException e)//if can’t convert to a integer then reenter % Y. A+ d9 j, D2 w7 T
{ 9 w$ e3 U0 `8 g# y# }! H, I
System.out.print("请输入数据编号:"); / H s% b5 e! N8 l" M) l: e& J2 W% {
value=getid(count); 6 W! A, ?& O' @! v, S" N4 x
}
! L v: ~& J$ S( z8 x1 Uif(value>count)//the input value is out of bound 7 M# d- }4 W- S& @0 ~8 a1 s. L% T
{ ! Z8 o$ ~: t% I: r3 s
System.out.print("无此编号的数据,请重新输入:");
: l2 D/ i9 E6 Qgetid(count); 5 z n8 x$ N- Y' j
}
( v E% X0 b* ?- C Hreturn value;//return a value : t- S, C8 S9 k
} {$ s+ c/ Z5 B) l& C' Z
} |
zan
|