- 在线时间
- 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.*;
" r: p0 B* E! R9 k' Pimport java.util.*;
' k$ r9 l4 U* a% P! z$ Aclass str14
$ c) y, @3 Z5 s( R& V o9 M; F5 t{ . H6 v* J& f7 F- p( b' V
InputStreamReader stdin=new InputStreamReader(System.in); " S _6 l* r: M6 t B5 ~
BufferedReader bufin=new BufferedReader(stdin);
- N$ q5 P, f3 v7 ~; |9 @# k
2 h8 r& X' }5 ^; V& Mpublic static void main(String args[]) throws IOException " H. F; z6 @+ u* p9 V5 _
{
) s6 [- U( g" ynew str14();//run the application
3 S& g6 O, x% p2 O} m* w6 m9 V/ e, e
3 n, H" L8 P* _: }public str14() throws IOException//constructor 4 Y. |( v! u: b( S: I
{
/ c8 T# M7 U1 u% g6 t' o9 wHashtable htable=new Hashtable(20,0.75F); % N( V. a6 D+ w" j: }" c3 ]
dataBase(htable); 3 c0 P5 y) [2 | e1 {
}
6 x: p0 F8 q8 ? b1 D* |7 H( u8 i4 e& z. z
public str14(Hashtable htable) throws IOException //override the constructor 2 r2 m, d+ Q: W' \5 f. j) n
{
" i. Z5 k; h9 w' N% }+ s( J) bdataBase(htable); 7 v. |0 V% W4 Y7 q
} 0 K: d- o+ ~% _
+ }* x" P4 f9 J& B% H8 K6 J
public void dataBase(Hashtable htable) throws IOException
2 D6 Y% l9 g5 D. y; ^; d{
: z! ] f8 i% i$ h7 h& f$ b% Q* Yint count=htable.size();//get the ammount of the data in htable ! Q) c+ I% v+ W+ y8 c
int value,id,num;
6 Y+ I9 i2 X7 s5 s+ E0 g& |String key,enter,data;
# l- f0 ]) R0 e, @' cEnumeration keys=htable.keys();//get all the key in htable 6 p+ o0 k! h. N, f, n
Enumeration elements=htable.elements();//get all the elements in htable / c6 M6 p7 w3 P$ I0 W% T. C
System.out.println("
( X* d0 t# j' c, v" i' bHashtable 简易数据库程序
' f {, ~( v! N1 t4 g( J6 ~, _4 [6 W");
" c& @. @5 k/ Y+ MSystem.out.println("(1)输入数据"); + w& L( J' a& s x
System.out.println("(2)请除所有数据"); # f8 G8 s7 S' ^% ]# T
System.out.println("(3)显示单笔数据"); 1 _" H5 t# s2 o* U
System.out.println("(4)删除单笔数据"); # b( d& t( P. @. C I
System.out.println("(5)显示所有数据");
6 J% i" N* w" wSystem.out.println("(6)结束程序");
/ h) N2 ^9 W9 M( ISystem.out.print("请输入您的选择:");
0 f$ ^; d3 D. qvalue=select();//call select(),return the function number 7 ^6 W9 N/ l# b( ~6 ^
switch(value)//the function
2 n3 X6 f1 r8 ?, a0 E{ - v& f v. z8 H. y. L* @$ P* k
case 1:
# v _+ W/ n4 Q d9 [System.out.print("- p# [, l5 s- q
请输入一笔数据:");//need data input 6 P+ {5 Q' A1 Z! q0 V9 t
data=bufin.readLine();
# }1 b& M6 k4 E1 M: r9 i: bcount++;
0 K5 u* j$ Q" ?( x1 n5 `) ]5 i, k) |key=String.valueOf(count); 5 Y# x C- B8 v/ ^8 O( @
htable.put(key,data);//store it to the htable
/ B" X8 ~( ]" KSystem.out.print("
% D w \. ]# I. [输入完成,按任意键继续...");
w- Z9 Z; x n, N, Uenter=bufin.readLine(); / a9 L. j5 V! C! o/ v
new str14(htable);//reenter 0 J; E: g2 S, z( g. K0 d
break;
2 K7 k, f" ] e7 ?case 2://clear all data from the htable / A9 r( }/ \: u
htable.clear();
# ]& v7 f4 \: Z% U- Y) e% g5 bSystem.out.print("
' V! R$ U8 O3 L. r已删除了所有数据... 按任意键继续...");
' [8 {( `# E- U( venter=bufin.readLine();
5 E) b1 \/ }) }' ^new str14(htable);//reenter
5 ?$ f ] j# V8 X8 Mbreak; 8 W* j& v8 G: r
case 3:
8 @" |3 ~9 H/ s0 FSystem.out.print("
- C8 ^2 e- H5 F0 y请输入要显示的数据编号:"); . d6 y. R0 k K# c9 S3 u- {6 R
id=getid(count);//call getid()
; T/ `7 j" v, ]! C1 W, @key=String.valueOf(id); . L, y7 @2 W; Q) D! A j+ ~& b
Object select=htable.get(key);//fetch the data from the htable
! I2 O* _5 Z! Bdata=select.toString();
, f$ S$ N B9 x, o. d, y( eSystem.out.print("
2 m3 |/ _# Y4 v) _1 T. x# @编号"+" "+"内容"); " u3 k# E" G! R2 D, h% Y% ]
System.out.println("; H/ [% @8 n% s# l. w; d1 C; ^% w
"+key+" "+data);//display the data
# v6 i+ E3 I( y6 T% {System.out.print("
& `, v N9 C" U) Q c6 S按任意键继续...");
9 T4 I7 p+ V$ z6 n# R, [) ]enter=bufin.readLine();
! w" I, z z. G- C3 z) ynew str14(htable);//reenter $ D3 t- Z8 L; v% t7 \) C) O7 w
break;
! b4 ?+ v- f1 S% J1 a' l9 ?# S' hcase 4:
4 _6 z! t m% I+ ?; ^& \- aSystem.out.print("请输入要删除的数据编号:"); 9 R' }1 K9 W; y" B4 Z7 w
id=getid(count);
# j( g$ o4 L, m9 G2 m" gkey=String.valueOf(id);
. v' x/ i( J1 V1 i& s& Rhtable.remove(key);//remove data
# x4 I, n2 W3 n# r. M+ V7 y+ R3 V# O: `count--; : j. f$ I* V) o8 r/ p! G/ L( f
num=count; : x% B! [7 @; c
System.out.print("已删除了所选择的数据...按任意见继续...");
: h$ ~) Z O: k( O" w" i$ JHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 + y" x2 f8 I( T
elements=htable.elements();//fetch all the data from htable * U5 W& Z& k8 r5 t6 T0 F
while(elements.hasMoreElements()) I+ C- Z; o4 \9 }4 Q
{
6 N4 t' r) m" X4 K& ], ?/ V! tkey=String.valueOf(num);//a new value 8 V* ], Y! ~1 v
data=(String)elements.nextElement();//fetch content of the data : t6 Q G: P2 ?, K6 }' s
htable1.put(key,data);//store it to htable1
' a& J- F! @$ h7 C T2 W7 R" d/ I7 onum--; 4 j$ g& q* N, g7 }4 Y2 n' e% t7 g
}
2 M. E1 u7 e; ?+ S3 N9 Y y, Uhtable.clear(); 4 K) T7 q$ \/ C8 A1 ^
enter=bufin.readLine(); 5 T: x7 A- q: g, }" s# A+ N6 r: Z2 b
new str14(htable1);
/ f9 v* \; [: z, l7 C& |break; 0 @' M; q; |) M0 M' L# x
case 5:
2 R/ n4 d* j2 N4 r. M `String[] sortkey=new String[count];//create a new sort array
* u9 k$ m5 ^7 Y y6 H1 j; QString[] sortdata=new String[count]; * U3 I' I& Q& F! a8 Q' r2 s
num=count;
8 C p# n3 i7 G7 Jelements=htable.elements(); : C& n4 F0 k+ H: T
keys=htable.keys(); 4 T: O$ Z+ [- {2 w1 E8 ]
while(elements.hasMoreElements())
, m7 x* v4 C" Q{ ! w6 K* R2 f d& E* x0 M
key=(String)keys.nextElement();//fetch value of key
* V/ A' m, {+ Y Z+ f1 ldata=(String)elements.nextElement();//fetch value of data # l& O+ U! M. i' N* R. Z
sortkey[num-1]=key;//store the value of key to sortkey array
: |, U7 A( p' f5 isortdata[num-1]=data;//store the value of data to sortdata array
0 I8 G2 E( n+ H1 z" k+ @8 D- ^num--;
5 s; h8 b4 A, l% k& P8 l} . @ X2 _6 D: W. E, M% e
System.out.println("0 J7 S: t3 B2 n6 _: B
编号"+" "+"内容"); + p' h8 ~. K( m6 [3 s
for(int i=0;i<count;i++)//display all content after sorting ! u' z" F0 m$ f5 D- H- w" W
System.out.println(" "+sortkey+" "+sortdata);
' y; k Q; a# B; `# J4 h0 ?System.out.print("( b$ r6 L. i" q
目前共有"+count+"笔数据");
$ U( [2 f2 K+ H- dSystem.out.print("
, H C8 M. K# d; M; U h' N: i% p/ W" Z
按任意键继续...");
" z/ b/ Y/ p; y2 q9 F% Uenter=bufin.readLine();
+ ^( s4 B# Z1 l( C/ h7 s" unew str14(htable); 8 M6 j# a% M. L& S y- a$ r
break;
" z* W: C! X. X+ i# `9 Q1 Vdefault:
1 ]9 v% ]. }& K0 r! M; r3 e}
0 a# `" k6 Z& q}
$ F8 w: g& x& P, t" f5 O' [" E2 B$ z1 W# b+ i& w
public int select() throws IOException//method of getting a function selector
S7 \# |! S4 N9 U{ 1 J9 ^+ j' Y9 V5 K
String input;
- x* |( v3 D" j; p; F# V* w& tint value=0; 0 D* a) Z! t- U j- I X
input=bufin.readLine();//read a input from keyboard
+ u; j: Y8 j3 q! b0 ntry
2 `/ [5 G+ B% n; p{ ' U; d, w0 {! L* E( c( \
value=Integer.parseInt(input);//convert a string to a int value 8 {" \9 B2 ?* w2 m; s2 G, U1 o8 j
}catch(NumberFormatException e)//can’t vonverted
/ Q% e7 M K/ _{
. U* Y" Q5 i$ H2 p% LSystem.out.print("请输入选项1~6:"); 0 [& I& ^; p8 H: Z7 b# G+ @
value=select(); 3 v% y4 K6 l; H; B/ P4 ^, P2 l
}
" [5 a$ K0 w' n; F& Z: L: g" W* ?* Xif(value>6||value<1)//if exceed then print a message and reenter / |- w0 |) P/ f5 h9 ~ p
{
/ J3 R- F& ^# X/ n; n1 ^7 K" E6 H* o4 NSystem.out.print("请输入选项1~6:"); 4 o, X' v; x8 D
value=select(); r- c S5 _& [6 P
} . I7 E, J+ u3 n+ e
return value;//return a value
& a. T6 ]2 O) |/ r3 n1 q1 X6 }}
: ]) |; J7 Z( q2 z5 u- g, N6 V2 |
. ~9 d9 j. ~/ `, kpublic int getid(int count)throws IOException//a method of return the number of data * [3 q' W. N8 D a0 R- b
{ 7 I z8 V8 A; @( e1 n) x* ^/ O/ f& e# C
String input;
& a4 n. P! O" ] D& kint value=0; ' `! a& j& t# e( ~+ V
input=bufin.readLine();//read a user input string from keyboard 7 ^; N1 h' i+ [5 K3 L; t
try 2 E B, `" y; [& f, F7 y
{ 5 O, o5 n6 J( f& X
value=Integer.parseInt(input);//convert the string to a int
2 G3 A/ u8 |, ^$ o9 Y N}catch(NumberFormatException e)//if can’t convert to a integer then reenter X% q* b3 x* d' f) f
{
! u" {9 B/ ~+ m( L4 kSystem.out.print("请输入数据编号:");
' w1 O0 L4 Y% L% lvalue=getid(count); 6 R7 [0 O" Q: L/ V5 t4 x
}
/ L( F: F& ]1 L5 T. nif(value>count)//the input value is out of bound
# b5 s% q$ Y" t& D: G( F K- R{ ; d+ @5 m3 i# M, M. [4 \
System.out.print("无此编号的数据,请重新输入:");
2 A( P q6 P! Q, W8 S4 l! hgetid(count);
% \8 O/ Q& \, \5 o} ; p ? f+ d% x. Y1 l
return value;//return a value
' x6 f' |4 h& E( T5 w}
# R/ D) ^) L/ R, d$ [6 l, {} |
zan
|