QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2735|回复: 0
打印 上一主题 下一主题

[转帖]Hashtable简易数据库程序

[复制链接]
字体大小: 正常 放大
god        

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  5 A" }% M- A+ `. u0 d
import java.util.*;  
; @7 h7 Y. j* d- Rclass str14  
1 v% V& P6 F4 M) O5 F{  * o2 {0 A9 q! O( V
InputStreamReader stdin=new InputStreamReader(System.in);  
0 b& U9 m# j3 Y& k2 k5 t2 sBufferedReader bufin=new BufferedReader(stdin);  
& N9 V: N& Q9 g3 h! P: Z' ?* z1 ^5 l) D' a
public static void main(String args[]) throws IOException  ) R7 h  |! A- a' P( j4 ~
{  
; p  \- l- n. {/ n: Tnew str14();//run the application  : o% y* p  z7 z  _# O
}  - Z8 U7 _6 |/ G& J5 h# J# L

/ X1 d) Z2 X: l9 v, Spublic str14() throws IOException//constructor  
$ P1 S4 Y# s6 V0 X4 _  ^{  
. ?6 ?) g* u0 Q' i( `Hashtable htable=new Hashtable(20,0.75F);  - w: t2 S% ~/ B
dataBase(htable);  ) w, I9 U9 F( I" O2 _! S! O
}  6 ~4 R! Y' L# V: G8 a7 r; X! u/ t
& `7 R- i" W+ ]- m
public str14(Hashtable htable) throws IOException //override the constructor  
5 g( D' {2 P( q' ~2 a{  4 P, l0 U& D# y* u1 S
dataBase(htable);  
$ u7 K; M* R3 x5 g, s}  
8 m' v$ N8 T; I) z" Z( u* C! z( w9 F6 {, L
public void dataBase(Hashtable htable) throws IOException  
" i' _4 t" l. j( z{  , u" s7 L" I6 ]3 q6 B8 w0 I& m
int count=htable.size();//get the ammount of the data in htable  
+ {' z7 w, t" X- Z" b( iint value,id,num;  8 |5 h( Z1 e; E5 O3 D! |" l
String key,enter,data;  + q  \( r# a0 b% @' o0 E) H5 \
Enumeration keys=htable.keys();//get all the key in htable  % @2 B$ i" X) ~& |2 r! }
Enumeration elements=htable.elements();//get all the elements in htable  5 y8 R) _; H% J$ C7 _  j, u
System.out.println("  |1 b' C' U5 y0 z
Hashtable 简易数据库程序
- u$ E# ?2 O% `, W  h");  2 V' Q  k9 g. `( Y& ]* q. e
System.out.println("(1)输入数据");  
" _4 t2 y6 w0 h8 {+ F/ l4 VSystem.out.println("(2)请除所有数据");  
. H$ B  F& r/ T6 ^: ^# _. DSystem.out.println("(3)显示单笔数据");  ! [( w6 n7 p9 m8 K+ q
System.out.println("(4)删除单笔数据");  
" V4 N# p) E* Z" t0 b# vSystem.out.println("(5)显示所有数据");  
8 r6 o' J* o0 i% t; _% ]System.out.println("(6)结束程序");  
, Q) S8 A9 p& x0 Z6 e  W: G; VSystem.out.print("请输入您的选择:");  
* g' A7 B, e9 E1 Ovalue=select();//call select(),return the function number  * c% i' u9 X; B; G
switch(value)//the function  " X; P( l0 `; Z: \4 j
{  
4 g: i* P0 [) |. Scase 1:  + d7 Z' K% U% J% P1 S& ~4 l7 P. D8 H& E
System.out.print("
# B8 ^8 o, b- r7 d请输入一笔数据:");//need data input  
# e) S, W1 a9 ~+ cdata=bufin.readLine();  9 g2 E) a! H3 U' I% f! F
count++;  
4 \) {" D5 H4 @; Y1 Q0 [5 O! w9 Lkey=String.valueOf(count);  
, e& W- [) j7 p* T. u2 i) Vhtable.put(key,data);//store it to the htable  
  N* a3 t* c. G/ W/ @System.out.print("7 r3 ~5 o2 k6 p; h2 G- d1 Z' }
输入完成,按任意键继续...");  5 d; y, |( d9 q1 x  u7 F
enter=bufin.readLine();  
9 }  T# N+ S0 F" F, m+ b  mnew str14(htable);//reenter  ! F; U) N( r# j; i/ Q% c9 K$ B
break;  
5 o1 M$ q9 `. Icase 2://clear all data from the htable  0 z9 w6 p5 `% p  _. |9 M3 @
htable.clear();  0 |, o0 T: x4 f# Z( P
System.out.print("- ~' \" r+ O4 F* |7 M" j+ s4 A
已删除了所有数据... 按任意键继续...");  
1 k+ k0 o* j" B; _* n6 ~- F1 L2 Oenter=bufin.readLine();  
0 Z. P3 S9 S! [, S" knew str14(htable);//reenter  
# d1 r0 ]% ^& s& Zbreak;  + L5 m! p+ d& F7 I
case 3:  
2 {5 r6 a. z% n% U& |6 s8 xSystem.out.print("# h* z/ n# U. y: X% D/ `
请输入要显示的数据编号:");  
, a: [4 V1 h* O/ M* q; Z; [id=getid(count);//call getid()  
* ^! M; g- a8 L2 Nkey=String.valueOf(id);  
: \4 z6 Z6 o6 lObject select=htable.get(key);//fetch the data from the htable  6 J7 `. O/ G2 x9 i: Z4 B
data=select.toString();  1 \2 |6 S) g( {0 t$ r
System.out.print("
) \- c4 s3 y# h/ \8 R; B! e编号"+" "+"内容");  
& l7 p+ t6 B  v. gSystem.out.println("
% w4 ~+ r, D& A! S! G8 _! o "+key+" "+data);//display the data  ' m( e* u0 v5 V# i' ~5 W
System.out.print("
# i2 x" F+ {- }9 ~按任意键继续...");  / }; p; z, |+ \. b( J
enter=bufin.readLine();  ; H. q- f9 `5 N' M  ?
new str14(htable);//reenter  
7 ~3 U* R9 c' s2 n8 Xbreak;  7 K# M. s6 g; s% m5 D
case 4:    i# k/ v2 B2 `' p6 x
System.out.print("请输入要删除的数据编号:");  
5 l& t( C& Q0 B/ mid=getid(count);  
/ M1 {9 F* I, \: B% k* Rkey=String.valueOf(id);  
/ r1 F& V5 ^7 A  |$ lhtable.remove(key);//remove data  
, f/ h" y/ I4 U( K" \* Lcount--;  
( P5 o. T2 o. d/ \2 Y( \1 Q$ Znum=count;  
/ p! y/ L  b. LSystem.out.print("已删除了所选择的数据...按任意见继续...");  ! I8 P: I* Z5 N2 e7 u- ?
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
, ?# M: d0 L  m/ J9 m+ V5 S# j: Eelements=htable.elements();//fetch all the data from htable  
( t1 D0 [$ b* x* X$ Rwhile(elements.hasMoreElements())  # F( k! R) I# I0 v# }  J) r% w
{  : D, N: Z7 B  f; e& P4 {9 y  ?
key=String.valueOf(num);//a new value  - v; v: g7 b  \! j7 E
data=(String)elements.nextElement();//fetch content of the data  : ^% |8 Y: h6 e0 A$ v
htable1.put(key,data);//store it to htable1  9 u1 K' Y7 h( T& w
num--;  0 ?% c& y9 j% G6 {* c( E) ]$ w
}  $ c5 W$ ~  J' m! D* d* S
htable.clear();  
# y7 g2 Q& y) c  K' s7 G2 B9 r! Kenter=bufin.readLine();  
2 F5 L2 H' Y; W: bnew str14(htable1);  4 I6 `6 V+ ?% ?) K8 @  c
break;  
3 e! Q' m/ G. S0 @7 i1 ?. Lcase 5:  
! r  K1 P, r0 FString[] sortkey=new String[count];//create a new sort array  
: R: j% G' d$ _/ `$ d/ u$ f7 ]String[] sortdata=new String[count];  , a2 h% f- X, E: w
num=count;  2 u6 b, {( F+ I* ^' \
elements=htable.elements();  
1 Y9 F9 @) F9 R2 ykeys=htable.keys();  
# ?7 D+ O/ T  i7 |while(elements.hasMoreElements())  
% l2 P1 S+ {" I8 B/ p{  ; \8 Y* L& }/ F" ?! w) M
key=(String)keys.nextElement();//fetch value of key  
! ]$ [: e8 \& ?* N& ~data=(String)elements.nextElement();//fetch value of data  
$ Y( V5 `0 H* b! Vsortkey[num-1]=key;//store the value of key to sortkey array  
+ T) t) E3 [# s. ]5 Lsortdata[num-1]=data;//store the value of data to sortdata array  
# f0 m5 a% Y, i8 _! O" ~num--;  
7 N5 K8 u8 z# V2 ?}  ) L: x9 f; |( K7 U6 N2 n
System.out.println("4 M- I% O( ]" C8 q; T- z: w  r& N8 W
编号"+" "+"内容");  
! S# D( g0 D& d8 ?) m( H3 Cfor(int i=0;i<count;i++)//display all content after sorting  9 c- ~6 A( f/ O" ^, ?% n
System.out.println(" "+sortkey+" "+sortdata);  
" O% u3 k/ B* _4 _7 SSystem.out.print("! b( a( j( |( Z' l; X! Q- W
目前共有"+count+"笔数据");  
$ U; _& C' L& A/ B+ X$ KSystem.out.print("( F5 m  K, L1 u! w5 D
, c& p- z2 A# `! C% F* x/ c
按任意键继续...");  2 i9 t2 L3 n5 S( w  }7 R4 M" E
enter=bufin.readLine();  
& z1 @$ p! X9 D: Lnew str14(htable);  0 n/ Q' f; s2 o6 h# K! Z
break;  + B# q% x0 l% k+ {* r+ s' Q
default:  
9 q) Z% I$ o/ ?1 j7 w& ^: x- V& T' I}  
( U6 f# Y5 j& Y) A4 @( o: N2 v}  
4 t+ H6 p3 f" b+ Y8 s7 l8 s, W. O/ T: N
public int select() throws IOException//method of getting a function selector  5 r$ U! \) }8 j) C  C4 w
{  . L8 G7 J5 s( E) ?/ u1 z+ k3 u
String input;  0 n  [2 ~2 T, x$ K& [+ ]* D$ ~: w
int value=0;  1 j7 ~6 {  h: e, X1 \4 O
input=bufin.readLine();//read a input from keyboard  
. e, {% j2 S0 O- T6 Xtry  % ~& J( C; \) h2 {* }7 [+ n3 s
{  2 K& d+ d8 i3 r
value=Integer.parseInt(input);//convert a string to a int value  ) S! [2 ~1 z: ?! s& g
}catch(NumberFormatException e)//can’t vonverted  
" Y6 J* t& `, w/ Z, u8 _{  1 z& y5 i6 o) q0 t/ v8 }
System.out.print("请输入选项1~6:");  
! @0 ]9 H0 W( k, C- `' k, k1 T  |value=select();  
2 b. j/ w9 T& l}  ; b" t$ h# s7 M* o5 s6 d& C$ ~
if(value>6||value<1)//if exceed then print a message and reenter  % z% e9 b  u3 I" _* _9 g* o3 F
{  * f/ ~1 W1 }9 W
System.out.print("请输入选项1~6:");  9 W1 l: t& v/ ?, O
value=select();  
/ D, l& r" }, D. D4 e4 O/ g}  , `& I: i' z( _$ o8 y% W
return value;//return a value  
1 Q: o  K; `! C' A3 V1 a/ e$ d}  
6 s+ `2 Z' P9 d2 c: z: j# R2 O  i5 o* ?6 z0 D4 K
public int getid(int count)throws IOException//a method of return the number of data  4 Z2 w& b* w/ }/ b0 G. q9 |1 b3 z
{    |3 p4 R( ^/ y& P+ i  d
String input;  6 g2 w/ `( Z$ l/ F, a: Z
int value=0;  . R+ ^% Z$ d$ D$ t' S  o9 K$ e  d
input=bufin.readLine();//read a user input string from keyboard  
9 B8 o2 R8 x& Z" @* Qtry  . K/ A7 g9 G7 |2 L! b
{  " k: I7 m( _' Z& B6 x* f9 f
value=Integer.parseInt(input);//convert the string to a int  
+ T$ L0 d2 k' j; U% w}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
2 f* i8 d! o! M{  8 e; ], q  ^5 r% ?. t( ~! V
System.out.print("请输入数据编号:");  
0 o; R; n3 e2 a6 i6 C2 l# Fvalue=getid(count);  
4 |# n. W' N2 M5 `}  0 W  T, i: f+ W6 [7 a6 f
if(value>count)//the input value is out of bound  9 Y* [5 H1 ?: j& _( a% U- t
{  
% J' J0 @. O8 n# U! ?5 o. N+ }System.out.print("无此编号的数据,请重新输入:");  " @6 ^! a+ R1 I) c2 ]
getid(count);  
( v# ~: c6 l! {: C* g! F0 Y1 R" }, g9 R}  
5 M1 }' n( A+ N$ Kreturn value;//return a value    G1 {- @3 |4 T: H8 ]  u2 `
}  , W4 H- `) o2 E$ b3 z
}
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-10 14:16 , Processed in 0.296670 second(s), 52 queries .

回顶部