- 在线时间
- 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.*; 5 Z. ]$ e* L4 I# }; `
import java.util.*;
2 Q/ E; J" X# M+ Cclass str14
8 X/ \3 W$ t3 ]( G \{
0 S8 b# F8 q! u9 uInputStreamReader stdin=new InputStreamReader(System.in); * j3 A: \4 C) n0 h
BufferedReader bufin=new BufferedReader(stdin);
4 G+ t7 m5 e0 S, _, s8 u2 n$ O% r: N/ C" n8 N4 U6 X! K
public static void main(String args[]) throws IOException 6 {- f* k0 p) D% B; X8 |4 Q' M
{
1 j- w; K3 ?3 `( u: Vnew str14();//run the application " T) X! \* U' i7 q# z
} 5 ]" y6 v8 p D5 J9 P5 {
4 @/ v- v4 g6 e- |/ n+ d
public str14() throws IOException//constructor
% T) Y$ L( O8 z+ @6 |7 o8 U/ P% f{
' y, d8 @. \# W! d1 bHashtable htable=new Hashtable(20,0.75F);
2 f) A r5 J% ^" d0 edataBase(htable); + ]' t/ R/ m6 ?) p" d
} 7 S9 a' C0 {+ w7 A1 o4 }0 x
) P" Q3 b& F: j% H, u: P/ Kpublic str14(Hashtable htable) throws IOException //override the constructor
- G# t7 h2 U$ E$ l{ + U# \/ E% w% k8 _& P z/ F( g$ x
dataBase(htable); + e- x9 F0 J0 w2 Q5 L" l9 ~
}
, ]! U; x; z- H9 o
* S' M2 s1 l q( U9 I$ Cpublic void dataBase(Hashtable htable) throws IOException
; }! B0 ]! {9 g( p7 |{ 8 W0 {5 p" E- K+ }6 L
int count=htable.size();//get the ammount of the data in htable 8 \' T7 M& [: `* C$ C: Z8 m% d9 t
int value,id,num;
# t3 c% @5 p2 q7 _1 I" j# xString key,enter,data; $ ~. F/ w) [: M4 z& }& Y
Enumeration keys=htable.keys();//get all the key in htable
0 d5 o3 b* A* V# L/ m" L: CEnumeration elements=htable.elements();//get all the elements in htable
) `( ~( G9 R+ ` F3 G2 CSystem.out.println("
2 M% @5 E7 t/ V' h0 u1 ?" l+ jHashtable 简易数据库程序
6 J" L5 c- H, C2 z"); ' W9 J) z/ [& O r5 v+ [+ X
System.out.println("(1)输入数据"); / j5 T( r1 l( R% x
System.out.println("(2)请除所有数据"); ' L1 T$ M' x9 Z: |" [
System.out.println("(3)显示单笔数据");
/ C8 Z0 ^. F# ~& hSystem.out.println("(4)删除单笔数据");
9 M% d2 y! s% H F5 f# F5 Y% dSystem.out.println("(5)显示所有数据"); + k. d5 o7 V. f& s
System.out.println("(6)结束程序");
' z% b& R* w; |4 [. m+ f3 lSystem.out.print("请输入您的选择:"); + A, i5 ^5 ?8 c$ w4 S( d
value=select();//call select(),return the function number 8 H/ g2 B6 k9 o% F. d w! x
switch(value)//the function
5 Y9 l' s) R8 f! ^+ }{ " e8 q; C8 M, K) e; B4 |- b, Q. m
case 1: $ s/ B# N- M- y V H
System.out.print(", x9 P% L( t2 z! X4 M! W
请输入一笔数据:");//need data input 7 W4 _( z9 k$ o# m5 V
data=bufin.readLine();
4 R. q3 M0 ^+ u2 @9 L0 W3 acount++;
- E+ D4 ~6 K8 \! X- K( [key=String.valueOf(count); % M- j- t7 |+ [
htable.put(key,data);//store it to the htable / C9 k! F+ n' l5 D
System.out.print("8 R2 h5 A1 c& @7 h) _
输入完成,按任意键继续..."); 3 M# ~. w2 f5 t1 {+ v N8 Z
enter=bufin.readLine();
5 h8 K6 b! K- f1 M X$ {new str14(htable);//reenter & {% {. U# s" o. Z9 Z9 }6 t2 n
break;
' n% W2 D |4 l2 f8 L, c; F9 L7 lcase 2://clear all data from the htable
! V* C k7 z. u8 } M( t0 s0 shtable.clear(); * R0 k" [# x e( p7 \1 y* D" Q
System.out.print("* ~, X" S5 b) b9 W
已删除了所有数据... 按任意键继续..."); - V2 h- @) J! [) h
enter=bufin.readLine();
* |4 w& T6 n$ o0 F6 G8 inew str14(htable);//reenter
3 m* ^% Y; }$ ~2 Gbreak;
! p3 F4 ^8 l3 W( ^3 @$ R# B# \) r/ bcase 3:
$ @& R7 J3 U+ I# jSystem.out.print("% `" L8 E1 n: j& z1 t
请输入要显示的数据编号:");
! q! o* B9 c4 h/ F. ?. o0 ~8 h+ _id=getid(count);//call getid() 1 t: j9 t8 p9 J. F; S5 }
key=String.valueOf(id); ( k9 P0 X# [! Y6 T: S: ^" z& F
Object select=htable.get(key);//fetch the data from the htable 4 Z1 O* T5 P0 `
data=select.toString();
& D) \/ |3 q5 _System.out.print("
. w3 x8 @+ H: f编号"+" "+"内容");
+ `, X8 u: d0 p$ E, Z- rSystem.out.println("
0 W# K: d1 f- [( T5 E "+key+" "+data);//display the data
, v. F: m- c7 F$ FSystem.out.print("
! @2 n' o+ Z0 j% a按任意键继续..."); " O8 s- X0 |: R1 q
enter=bufin.readLine(); ) q$ F, o$ E$ E" _
new str14(htable);//reenter
* I8 w6 F% y1 tbreak; 6 }% y* U1 m: f" ]$ o4 p( W
case 4: # u6 O: n4 r( P/ X
System.out.print("请输入要删除的数据编号:"); + d; _7 u( u! J; D0 d
id=getid(count); % M3 E( ]4 k( x4 ^9 r0 x
key=String.valueOf(id);
8 j- n; l# K* M5 b& M; Hhtable.remove(key);//remove data ' s! |% Q; q. p1 Q5 H- m
count--;
' Y5 [% }) B# ]% n8 y1 k2 Xnum=count;
4 H9 o6 u4 }6 ~7 H5 i, k% OSystem.out.print("已删除了所选择的数据...按任意见继续...");
! |* T: C) H! xHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
2 }* q, d9 ?7 v3 f& S5 f( Aelements=htable.elements();//fetch all the data from htable : X9 x* ?+ u, ]8 N" P; G
while(elements.hasMoreElements()) ( Z) G- Z" l @
{
1 `8 H0 p2 S/ J5 o6 S! q5 `key=String.valueOf(num);//a new value
# g% Y5 ^( N, {/ {* bdata=(String)elements.nextElement();//fetch content of the data 9 I5 w% p7 i( M& q! y* S
htable1.put(key,data);//store it to htable1
b' c @$ L2 ^) C# Nnum--;
: m5 y4 ~, P1 y& C} # w/ X& a. X! Q, R. ]# ^# }& w
htable.clear();
7 ?. |0 a6 `9 t+ a6 B4 Lenter=bufin.readLine();
5 }) O9 h) [! C3 @, L; v+ l8 ^0 q( rnew str14(htable1); / f' L3 y. `! h6 W$ P$ s
break; * l6 [3 \ `& i* o1 @6 u
case 5:
' e6 {0 Y: @. {* T! z0 H/ G: TString[] sortkey=new String[count];//create a new sort array 7 W! C7 a: H4 n+ `. V4 |
String[] sortdata=new String[count]; & |5 l M7 y& \3 p+ d1 u$ W
num=count;
7 _: _5 u7 p8 `5 }* Y, relements=htable.elements();
k8 ~& p8 Z0 ?# Wkeys=htable.keys();
* |( Z0 z) r: f6 Xwhile(elements.hasMoreElements())
5 [8 n4 V. G0 _ I- A{
% l; |. m% J! }key=(String)keys.nextElement();//fetch value of key " o2 M8 t+ q6 S: u2 S+ _
data=(String)elements.nextElement();//fetch value of data
) }( r4 z3 h& M8 k7 l- usortkey[num-1]=key;//store the value of key to sortkey array
9 _7 W8 J: P6 P! Vsortdata[num-1]=data;//store the value of data to sortdata array ; E& \! |+ F j4 J# C- a0 T
num--;
m' g( g1 ~ Y6 P" P} ! L" G' d: v, n7 c5 h
System.out.println("5 s) T2 D* j6 H7 f0 n
编号"+" "+"内容");
[ H9 r1 O& G/ vfor(int i=0;i<count;i++)//display all content after sorting
' r2 y7 z1 x; `- W4 }& i/ {System.out.println(" "+sortkey+" "+sortdata);
) D5 A# c+ f% g, \9 T ]System.out.print("
6 f [# l! H# H6 E目前共有"+count+"笔数据"); & B9 u' a5 S* l! I* \
System.out.print("
! i6 K: i! N, E1 D5 L2 Y0 L, S% I6 t% u- c- A
按任意键继续...");
2 @$ j2 O! y1 r# C: Qenter=bufin.readLine(); 8 n# }8 W: F: f; y; f' z
new str14(htable);
& f6 x( w, n3 s$ K$ [, g, Xbreak;
: K6 O' E4 A8 E5 c/ v6 fdefault: $ f ?2 o0 h5 A# b. T0 }2 W. H8 [
}
4 B% m2 Z2 h$ o T- [( P3 C6 F} 0 z- c0 {) g6 d. Y1 @
+ M# @" |# U: x' v1 P
public int select() throws IOException//method of getting a function selector
& }; w+ h- ~9 F: m# u9 \5 p{
3 H) {7 e5 ^: t9 h PString input; ) [ A5 r3 B$ N8 M" S
int value=0; , S! }6 B( o' m- z" Z8 l
input=bufin.readLine();//read a input from keyboard
/ l( z! K; F* itry 4 S% B( U, L$ d# P$ g* t% e
{
- b# c7 N8 j. O( e) svalue=Integer.parseInt(input);//convert a string to a int value
. [# {& P8 u1 Q. H* ^* n$ V6 s}catch(NumberFormatException e)//can’t vonverted
i) V0 `" }5 v! D) Y9 m: z& @! i{ 9 ~7 j2 T4 o. ?! Z
System.out.print("请输入选项1~6:");
- L4 m5 l+ h( c) }, s2 [) e6 q' Svalue=select();
# U# ]+ q0 d% x$ o& g, v} 2 H9 a8 w) _5 J
if(value>6||value<1)//if exceed then print a message and reenter
7 ?( @' s: M N{
2 s C3 P% n! C% v8 w6 [System.out.print("请输入选项1~6:"); - I( y- S2 |4 U! r) c1 ^5 a9 m0 d2 H
value=select();
, @# J+ p6 @2 g5 i- I4 o6 v}
8 t9 H8 X+ u+ x: qreturn value;//return a value
' g* E! T3 r* W! S' y6 \; D8 Y} + t' p4 a; s0 M. h9 G
! B3 G& k% V- xpublic int getid(int count)throws IOException//a method of return the number of data
6 s( O5 g* d- y$ j: {- x{ 5 s7 g8 M- k, e
String input;
" C7 }$ b3 N8 u0 S5 X6 hint value=0; 1 R9 a) |% P! }( o0 M$ A: [
input=bufin.readLine();//read a user input string from keyboard 6 b$ p# d- k* u
try
; N3 x0 ^4 V* c) Y+ J# e& L0 C2 X{
4 ]' V+ [2 a7 [7 Rvalue=Integer.parseInt(input);//convert the string to a int 5 Q$ {8 t& c, w2 Z
}catch(NumberFormatException e)//if can’t convert to a integer then reenter , w! e$ ]. ~2 F. _ s
{
0 x6 J" O( Q9 L, u ?2 ?System.out.print("请输入数据编号:"); $ p9 }" C/ m# y- D& {) p, A
value=getid(count);
+ ~; j" u% S5 c$ ^% Q7 L8 \' e}
$ Q) `9 s4 |( uif(value>count)//the input value is out of bound
% m+ U1 m( |, Z2 \# m{ & T+ y2 r/ S1 l% E; ~6 B
System.out.print("无此编号的数据,请重新输入:");
$ r9 Q! j% l" ~) U$ ?% E1 Ngetid(count);
0 g+ y- O# w2 _7 F) }/ K) \5 b} 7 G, ]3 V; l$ D% {2 _
return value;//return a value
& h4 x- p( M& i7 O6 @" Q3 B} 7 O5 f2 E: j1 p. Z
} |
zan
|