QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;    \' C) ?' n" @; _, K4 L
import java.util.*;  3 v1 Y+ a8 y8 @  i* C0 \
class str14  / T7 N- h( ~0 v  w, @5 M
{  
" }, Z$ b7 j5 z' RInputStreamReader stdin=new InputStreamReader(System.in);  
  i  e* V- J) I/ P- sBufferedReader bufin=new BufferedReader(stdin);  
" [( D5 x$ {# h  b' g9 @
2 j" U- ]4 Q: Y$ K1 z/ b# wpublic static void main(String args[]) throws IOException  
  L" E6 s  k& l: ^{  
7 ]$ K, Y+ |% \! V/ nnew str14();//run the application  
* D$ U1 t) J4 v9 ~+ d( B, s}  * E/ A" p( O* ~. l+ ]- t3 W& Z3 @
- |: Y7 {4 E5 y$ [
public str14() throws IOException//constructor  
& H9 k: M: _+ [{  " V: j( _5 C8 ~) Z7 |1 w
Hashtable htable=new Hashtable(20,0.75F);  
- T$ E3 k- Q' J: zdataBase(htable);  ; X2 @. j2 y0 `! c0 ]) Q
}  7 ^$ c. K1 K, F4 J( q

* T/ W4 ?; G. B- |' f- B! E5 X' n4 G4 spublic str14(Hashtable htable) throws IOException //override the constructor  - }" H/ t$ W  S  i: r
{  . h) w- L1 v1 j+ {
dataBase(htable);  + x& L- N5 ?9 F5 X7 }6 h- \
}  
: j7 j, {0 I6 Z! e  J9 m$ l: K3 ]8 @" g% r8 e4 \/ M! `  A
public void dataBase(Hashtable htable) throws IOException  
3 O- _  R) K! q2 {1 l' r% g{  
* e. _1 u% p$ G  x! W$ M+ hint count=htable.size();//get the ammount of the data in htable  
# S( o" d! d" _* \( k9 sint value,id,num;  . R" G6 N/ e( [
String key,enter,data;  
5 o6 n5 d+ ?3 r: h/ w9 qEnumeration keys=htable.keys();//get all the key in htable  - m* k' {) {* p* @
Enumeration elements=htable.elements();//get all the elements in htable  
+ |) B& ?% u, \! D7 F9 ~! iSystem.out.println("9 @0 {  [8 o% P6 ~! `; P
Hashtable 简易数据库程序
# m" Y, w+ h- B8 H" T  l");  / w  X' q2 R  c) K' c
System.out.println("(1)输入数据");  
* O5 \) h$ n5 B2 k! cSystem.out.println("(2)请除所有数据");  
+ a/ I# T2 M" i' SSystem.out.println("(3)显示单笔数据");  
( R. Q5 c* Z* X7 ?System.out.println("(4)删除单笔数据");  + ^- o3 k# O# n- D* V3 F0 L0 J% V
System.out.println("(5)显示所有数据");  
: }* V. X8 m# ?$ X# K1 z4 X- BSystem.out.println("(6)结束程序");  : U0 o1 L! _" Z+ W  a7 P) ~7 A
System.out.print("请输入您的选择:");  
6 J2 p9 D$ F1 D3 svalue=select();//call select(),return the function number    p( T4 @# ]7 u# e
switch(value)//the function  
4 ~* a# F1 N; F* I{  % O/ e, W# N( ~( u, A( G
case 1:  
/ R# O' S4 B# q4 D5 [3 c: o4 USystem.out.print("
9 G% }  o/ ^# f$ P: H请输入一笔数据:");//need data input  1 D8 y4 Y' p( P% a9 _" v* {( S: Z, g
data=bufin.readLine();  
* }' `! q# C8 F* W/ t+ hcount++;  * N# Z8 G4 j5 |2 P1 ~. ^/ v
key=String.valueOf(count);  0 V1 Z! i4 T" ?
htable.put(key,data);//store it to the htable  
1 n0 U- m, H( p! sSystem.out.print("
& D5 I( h, W  r& k7 `4 f输入完成,按任意键继续...");  + M1 S/ t+ s2 X6 b; c! c) c
enter=bufin.readLine();  6 G/ L+ b/ e0 D. z) n+ d+ O8 M
new str14(htable);//reenter    L( M: ~5 P( N$ i
break;  
5 D6 c! L1 c' M7 |" r1 d+ o% e/ c/ ccase 2://clear all data from the htable  + }- g" k  y' j# S, U3 B
htable.clear();  
+ B. k/ [7 d7 Y# c$ ASystem.out.print("
% L( h( N- b5 N5 b已删除了所有数据... 按任意键继续...");  
. p2 v4 d3 X0 I" I6 x* ienter=bufin.readLine();  
$ x, B# p0 d3 k1 W0 N/ n. c/ Dnew str14(htable);//reenter  
4 k/ J8 W2 q, }break;  
/ s! t- u6 h/ l/ A1 gcase 3:  # g/ L) O7 @2 R/ a/ ~
System.out.print("/ a6 \6 E5 t7 g4 g4 a% r
请输入要显示的数据编号:");  : G9 l. S' Z# d, u, }
id=getid(count);//call getid()  6 y8 o; S/ L- d: Z6 @6 H! u
key=String.valueOf(id);  
& N& ~1 B9 J; s1 ^7 xObject select=htable.get(key);//fetch the data from the htable  
5 @4 U% D& h/ b( E. _6 O5 _+ u! mdata=select.toString();    ~) ^) t. o" [6 D9 h# L
System.out.print("4 O$ C' s0 E& ?8 j  c
编号"+" "+"内容");  
: D$ g9 D* m% }- b1 _System.out.println("
$ O, s: `! [; G% V% a3 a "+key+" "+data);//display the data  
! L" [( E$ }( p" C! a  t/ e' }8 |System.out.print("5 v+ T% Q! ?% n
按任意键继续...");  , I! d' Y5 O: W# q) k. @
enter=bufin.readLine();  
3 {% v' c8 t# Y) hnew str14(htable);//reenter  
7 B* V; N0 }' ~' |6 W. B/ h' b: ebreak;  4 f2 |0 {2 H7 J
case 4:  . ]! w$ Q$ B. y; Q- ]
System.out.print("请输入要删除的数据编号:");  + l) w% `0 v, l8 l$ I! ]8 ?
id=getid(count);  
2 ]. W  d& y( Y6 L8 Kkey=String.valueOf(id);  
. y. d. b$ Z9 e8 s  T/ {! B6 _+ dhtable.remove(key);//remove data  
2 F% P4 q. F3 E# y6 H9 m# Scount--;  
# E9 l$ N! `: g6 n+ Onum=count;  
! I6 `8 u# D% t. A3 D# }9 MSystem.out.print("已删除了所选择的数据...按任意见继续...");  
1 ?2 I( `! ]0 a; ~/ THashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
/ ?: x3 L. h& F) k1 v/ c6 _elements=htable.elements();//fetch all the data from htable  : ]+ ?; A0 B2 o; \
while(elements.hasMoreElements())  * k* y! p, c3 I9 ^3 i" d3 V
{  ! O& S3 `7 Y8 b" d; I5 W$ Y+ W
key=String.valueOf(num);//a new value  , @8 Q( }. y! U4 ^+ o: P
data=(String)elements.nextElement();//fetch content of the data  
( W3 B+ I- ^, k- V9 ]& Z2 zhtable1.put(key,data);//store it to htable1  ; S1 {: O7 n( g" m: f5 c8 w
num--;  7 t9 h2 D( @2 m) N# l9 H; ^# ~5 M
}  
" t5 l9 c3 W/ R4 |! F( |7 Y( p  uhtable.clear();  * m# n2 W' c9 z! v' ~) o
enter=bufin.readLine();  ! u/ @4 v+ g4 r- F. S
new str14(htable1);  
. N3 J1 G: k8 @+ Y0 r" P5 Vbreak;  / ~% [4 }8 L7 l2 T; H/ i
case 5:  , p% V  @0 z' `3 D
String[] sortkey=new String[count];//create a new sort array  
8 C8 M, j/ `3 U0 F( xString[] sortdata=new String[count];  
9 I% ^( D8 }; R# x/ G6 A% f% ]num=count;  
+ [7 A, n4 _5 ?9 S; Y+ o1 _* Selements=htable.elements();  
8 {' l: c% U, f/ J! |" f% rkeys=htable.keys();  0 E* E7 v0 P. N( `5 _4 C
while(elements.hasMoreElements())  
% K5 n5 t7 p: G{  
' t5 T6 Q* Z; ^/ T+ B7 l6 w* y! kkey=(String)keys.nextElement();//fetch value of key  # U7 ^  V0 i/ W6 r$ N, N
data=(String)elements.nextElement();//fetch value of data  
0 K9 |% F  |8 }sortkey[num-1]=key;//store the value of key to sortkey array  
: W3 Y/ V  z% _sortdata[num-1]=data;//store the value of data to sortdata array  
' X7 {9 R' R" ]" }0 H% lnum--;  - g) _0 c7 V& D; {
}  
) o, g% _1 ?' b' [$ KSystem.out.println("
7 w. i/ K( x& n# }编号"+" "+"内容");  
9 ?  e5 i9 Z2 m% Z. |, O1 t3 Sfor(int i=0;i<count;i++)//display all content after sorting  
0 L0 `( x: k' L+ T5 }System.out.println(" "+sortkey+" "+sortdata);  
. \4 t+ x- F, f, K0 [System.out.print("
1 X0 v, ^. W. R$ l$ k0 `0 Q. C目前共有"+count+"笔数据");  , _+ D$ T; \$ ]/ r
System.out.print("
' V3 x$ u6 q! @
3 s4 Q5 E: V- {  k1 J按任意键继续...");  
* T  |+ L" K; e0 B% w* ?" Uenter=bufin.readLine();  
4 y" b1 ?+ t! _- }  J, Wnew str14(htable);  9 H+ a( N- q+ V+ L; ]5 n
break;  
; P8 K0 n) p  D6 l, tdefault:  ) x9 E9 @6 P! k" Z& d+ l2 W
}  / k& z/ S* U7 M/ `! C
}  $ X3 Z/ h( B" n) y) L5 f

- [* H, P! W! E! ]: Y; Tpublic int select() throws IOException//method of getting a function selector  
* I$ t/ C6 l" e2 r7 R& v{  
& L* C5 _. o% n, A+ W4 U: [String input;  
9 R# |9 ?! n0 O: Lint value=0;  % m* N$ |9 L. K0 P/ N
input=bufin.readLine();//read a input from keyboard  3 K; `. B, @( y' F, O6 \* C
try  
! d5 t1 E1 T2 s, b. X1 q; m, m( r{  6 h5 X8 V* W5 v* P! v
value=Integer.parseInt(input);//convert a string to a int value  
: p* ^  O  k4 M5 y" p2 ^}catch(NumberFormatException e)//can’t vonverted  
, b" @# _+ t4 L4 r# i{  
! }4 |; m3 R( _1 E' X; e1 c1 ^System.out.print("请输入选项1~6:");  
% P" I! D; L0 N# u" @% s( j! h$ ?7 {value=select();  
6 _. w8 g! g% O/ e2 c) L3 b}  
3 \) L/ ?( i( p0 [3 l& c' h  hif(value>6||value<1)//if exceed then print a message and reenter  ; h. j" s4 b6 K/ C0 |
{  * l* O1 }$ ^* z7 W- ^: s9 S7 E- u
System.out.print("请输入选项1~6:");  
5 a1 y0 a1 e9 t2 avalue=select();  
! f: G* m' a& H- G7 R}  
6 z% [" z6 b) Breturn value;//return a value  6 w! b" Y5 b# n1 ^% k7 C( W6 J2 X
}  : m) r, ^7 ?& k5 h6 t
# h) o; d$ }& ], e& C- J
public int getid(int count)throws IOException//a method of return the number of data  
; n* F7 H) ^% _& k{  . a, G, i  {1 E  ]6 Q& K
String input;  5 r) o( ]- z. i/ O( [: [
int value=0;  
7 g6 v; X) y7 _* Winput=bufin.readLine();//read a user input string from keyboard  
/ V. E8 q1 ~  H, y7 m3 D0 Ltry  
) P, y' Z) l* G) W" i{  
! p: H9 y/ Y! U% q# F) ]. \value=Integer.parseInt(input);//convert the string to a int    |" N  ~* {5 f, x; @+ k
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  9 y8 G8 U6 J2 w/ x. E  l- @0 k
{  ! E" C' T& ~5 d4 V! T) E% B6 J* F5 \
System.out.print("请输入数据编号:");  
8 l( W3 }6 u9 M3 R6 ~value=getid(count);  7 A& e0 q" d9 |1 N
}  " A; j" |, q" B/ ]8 N. [
if(value>count)//the input value is out of bound  
9 @- Z, `' I8 ?3 L" P, Z5 H- z  F{  ; U' J! t- ?, T8 n2 L
System.out.print("无此编号的数据,请重新输入:");  5 ~9 @& N& b1 w1 q1 F
getid(count);  2 I! Q0 [, u0 l
}  
, E+ @- H, p1 D( S/ k% H: L1 v/ Rreturn value;//return a value  3 Y6 o5 K3 d' _) l
}    Y: G' G9 L2 U
}
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-10-8 06:29 , Processed in 0.401208 second(s), 51 queries .

回顶部