QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  / C* V  `+ m) C+ q" _
import java.util.*;  ' V& Q7 Y$ _( O2 s$ h
class str14  
6 S  S# ^+ C/ i{  4 C$ a" x6 M- `# P% G' N6 o; u
InputStreamReader stdin=new InputStreamReader(System.in);  
" X2 @$ J/ F$ T" ]% N( F  d! L. c7 xBufferedReader bufin=new BufferedReader(stdin);  
3 M9 H% h) ^, r. t& O) f3 n: Q7 m
, H4 P- h7 O! m) U) A# f* jpublic static void main(String args[]) throws IOException  0 W3 y4 t+ b# X* j/ O4 D- F7 G
{  8 O* |# A2 m- V) @) Z5 N
new str14();//run the application  ! J; q! o3 |: r
}  
; {# ^/ ]  F; b) T) A: l3 T+ @5 j- q& v; {& E$ |) U# X. q
public str14() throws IOException//constructor  # y; W& y+ C; S  |: A
{  
# S, N) n! h( nHashtable htable=new Hashtable(20,0.75F);  . Y; w, C! v  ^$ S3 ~
dataBase(htable);  
4 u6 d! F* Q1 g$ _8 z- y" a+ I}  
& E) s  q2 t9 {% a+ `% V  {
. ]2 H4 b/ o6 B+ p4 J8 a* v7 t3 _public str14(Hashtable htable) throws IOException //override the constructor  ( c, A' x) ~! t( T/ R4 q8 A
{  
0 f" A; a# O+ `2 P0 |1 CdataBase(htable);  ( }; B! `; m( n. s
}  " u# C$ h$ q6 E5 L
' ]) M  Y  B& Z+ D3 X. Z1 P, I! d
public void dataBase(Hashtable htable) throws IOException  
8 \+ s3 M, _7 G/ j/ B{  % N, w9 |. R' N# W2 l
int count=htable.size();//get the ammount of the data in htable  + Y, b) f7 X( B( B3 X
int value,id,num;  
  ~7 _5 \: f( ?1 S* j. Y# CString key,enter,data;  * V* B& J9 p8 ^
Enumeration keys=htable.keys();//get all the key in htable  2 D* d- }" g" S) C$ A$ t" n
Enumeration elements=htable.elements();//get all the elements in htable  ) d9 t7 d; s+ _) O* i8 E7 e3 B: M
System.out.println("2 g& |; P: i3 H0 e6 ?9 z
Hashtable 简易数据库程序
0 M3 Z6 c7 G7 W+ F");  % L/ P- s) }5 g, q
System.out.println("(1)输入数据");  : |4 A! S# M4 s7 z2 J. _, o/ ]- y
System.out.println("(2)请除所有数据");  
- b1 H. O. h/ GSystem.out.println("(3)显示单笔数据");  % g) R8 w* Y; p6 c. v' {
System.out.println("(4)删除单笔数据");  7 \+ Y' Z# n" ~7 m+ Z7 k1 [
System.out.println("(5)显示所有数据");  
' Y$ E  I# |' X# OSystem.out.println("(6)结束程序");  6 ?; y! \5 Z6 Y& m2 H
System.out.print("请输入您的选择:");  # i* i) ^3 P) R  }6 k# L
value=select();//call select(),return the function number  
. E- i& s5 J) i1 o- w8 jswitch(value)//the function    f7 r' y. q- u9 _# ?, _+ y
{  
& }4 @/ u' B  V  W: r" scase 1:  
6 A! U( \' x+ H; n4 U# m, CSystem.out.print("
) x+ t: b9 ]% A- w  S$ Y请输入一笔数据:");//need data input  4 _- F# t- n( z: E$ u4 U, o
data=bufin.readLine();  : v' v/ G9 J2 C+ k# f, @
count++;  
6 `: r, c" E) A2 skey=String.valueOf(count);  
, t3 B# C; z3 H% {$ Fhtable.put(key,data);//store it to the htable  
# y2 A0 U& W7 N9 u$ v* {! BSystem.out.print("
! W" T9 D" C) M- Y# _输入完成,按任意键继续...");  2 n; v* P+ r/ d9 [8 X5 p$ l8 B
enter=bufin.readLine();  % g* t( o$ O- L5 ^
new str14(htable);//reenter  
- Q6 e) N, R, P/ B9 g3 C; pbreak;  . _8 d) r( `" f* @8 w5 w  l
case 2://clear all data from the htable  
  X+ a! C" X( ?4 O' `htable.clear();  7 S% P# Y% G8 u  Q( D
System.out.print("
9 G1 Q+ G! M- |4 ^* ]已删除了所有数据... 按任意键继续...");  
2 e1 @& Y  B$ ?9 C* W( ~! `enter=bufin.readLine();  0 [% N( k" e7 b+ J9 Z
new str14(htable);//reenter  
. Q6 l3 l, y* ^9 {7 L! ybreak;  + q( q# A# \! Q* Z! k
case 3:  8 u) c; S0 H$ {( S  z
System.out.print("
8 Q8 [$ H1 r* G0 u4 Z/ {' J; H4 F( n请输入要显示的数据编号:");  7 F  d! u: p+ U. E) T$ d
id=getid(count);//call getid()  0 J8 _! P+ M: o( C& J  J
key=String.valueOf(id);  1 j0 N7 n0 O5 d6 n4 `  a) s0 X
Object select=htable.get(key);//fetch the data from the htable  
! E3 ^( m* |8 {, edata=select.toString();  . W6 R. Z1 t: r8 G. C1 \
System.out.print("; x) K1 N$ {: Z
编号"+" "+"内容");  
, m9 a3 X; `1 `$ a% xSystem.out.println("6 Q* }$ f! n$ c7 B* w
"+key+" "+data);//display the data  
  E7 C5 ]+ c# x" p6 s7 WSystem.out.print(", Z) i$ O8 k7 l5 G; P
按任意键继续...");  ! l! }: N8 z! M8 |& d% g
enter=bufin.readLine();  
5 Z8 D3 e+ d1 o: M& ?5 P- |6 nnew str14(htable);//reenter  , @: P+ `- _3 p' c# z$ T, x8 H
break;  % E- P$ B3 t& m# M, `
case 4:  
7 I# J0 y6 U. ]$ I1 Z! @$ hSystem.out.print("请输入要删除的数据编号:");  1 K. }( t# K. f% J/ |" t4 c/ f+ N
id=getid(count);  
" E5 w( n1 `& v6 e9 Tkey=String.valueOf(id);  
/ W. X8 ~" C9 Phtable.remove(key);//remove data  
9 z& s! Y; v  |count--;  
* B" n# O" a. ?! x- F8 H$ ]$ _; znum=count;  / y6 Y. Y# i( f7 j+ V4 J
System.out.print("已删除了所选择的数据...按任意见继续...");  
8 V' g# v8 {, `# a% k: l0 jHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  ( k9 Q5 _1 u6 p, B
elements=htable.elements();//fetch all the data from htable  , `* r0 `/ f' [) Y+ d6 O9 c
while(elements.hasMoreElements())  % y/ q6 [' z2 c0 Q: ?% }
{  
8 n* R/ D, s, T3 j) C2 V' ykey=String.valueOf(num);//a new value  
8 N+ p# m0 e3 U0 h  m2 N5 P! @& zdata=(String)elements.nextElement();//fetch content of the data  " V* V9 b& `: R" {0 l5 c2 j* x7 [- k
htable1.put(key,data);//store it to htable1  & [  J! \; D3 G! I" p
num--;  
  c: x+ [6 N4 N2 Z0 A}  . \; `: K/ p6 u
htable.clear();  
2 S  U0 Y: ]8 g8 V) oenter=bufin.readLine();  
7 Y8 O; s5 F; }, F  knew str14(htable1);  
. g* R& d1 L' \2 E8 |9 N" mbreak;  7 b# m. u/ M2 @. B
case 5:  
5 l/ F  ]+ a& ^+ q( w% A6 c3 d8 ^String[] sortkey=new String[count];//create a new sort array  
& Y- J* k0 v, eString[] sortdata=new String[count];  & r$ e& Z1 D* |# M1 s% A. ^
num=count;  * G+ H( g& Z6 t7 E+ O' t
elements=htable.elements();  . B* f! g/ ]* w; F4 t' A! J( Z
keys=htable.keys();  
1 l: L! n2 u+ C" \6 T9 G# g$ |while(elements.hasMoreElements())  : |+ w) O4 d" F) h' V3 E
{  3 k7 v9 V( c  m
key=(String)keys.nextElement();//fetch value of key  / D/ @! ~1 y. C
data=(String)elements.nextElement();//fetch value of data  
: J$ d$ t! O; k: s9 G# |7 isortkey[num-1]=key;//store the value of key to sortkey array  9 ]+ Z* G5 P! @$ D6 `6 N
sortdata[num-1]=data;//store the value of data to sortdata array  
. j8 N6 P8 U, T/ Wnum--;  ) A& i0 V8 t1 Z0 n& k0 M' }: T: a
}  ) m3 I5 P. i3 Z- ~
System.out.println("9 y9 R$ Z8 F1 B# O, e1 b
编号"+" "+"内容");  ) u( I8 A+ e9 E3 ~( `
for(int i=0;i<count;i++)//display all content after sorting  ) z* i- B" _; O
System.out.println(" "+sortkey+" "+sortdata);  
' \# q" o4 k, rSystem.out.print("
! a. F# W8 u$ r+ s目前共有"+count+"笔数据");  ' L: B  j( I& `" K- j
System.out.print("# @% I$ a) `! n- `) [0 q' {
  @' z  X- W7 w& R2 ]% r- O
按任意键继续...");    L+ ]/ k8 P7 C9 {
enter=bufin.readLine();  8 [8 i6 Q1 A. e: }" _
new str14(htable);  
! L& F) l2 W- Kbreak;  
+ z- S2 G, y" s1 d9 M4 W6 X. x0 ]" I. vdefault:  % A8 s5 Y! t/ ~1 n& z% a' Q
}  & s1 z" A3 u7 h# C* y% B2 b
}  
( T" i+ x) s- g: J- y/ \! s2 a$ [3 o
public int select() throws IOException//method of getting a function selector  - {  F1 _$ ]9 J' X5 k. S; H3 r
{  & `, i2 D& Z/ i& c6 g& ^: M) |
String input;  
% R" }: l3 j4 F; Y! aint value=0;  
, c$ q  @) @! U8 O  I  P3 Ninput=bufin.readLine();//read a input from keyboard  
, Y  h. V" d; [( |; E' wtry  ) e' S) S' z5 C9 {* w7 |
{  
9 a' D) K( E; y. p) ^0 @value=Integer.parseInt(input);//convert a string to a int value  
  T$ Y, `, F) X}catch(NumberFormatException e)//can’t vonverted  & |3 S6 O# Z) s  ~$ `
{  " R  ?1 ^. A; a0 D, `1 e
System.out.print("请输入选项1~6:");  
8 g/ D4 T! @* G) l( M$ N: O# l& hvalue=select();  ! r6 h0 M5 ~( k& L$ v9 _% Z' F4 \
}  
: v! J4 |; `' Y0 J( [" \if(value>6||value<1)//if exceed then print a message and reenter    k7 {5 T8 H2 P1 E  h9 }
{  
7 X5 w! P: R9 [6 J) Y0 G& NSystem.out.print("请输入选项1~6:");  . V, Q; O* t$ b: e9 I3 i" I" o. P
value=select();  
2 B% t- w8 q. g% Q* j) b) |  b}  
+ b  h- c) ~3 G: ]& d3 Jreturn value;//return a value  ; G% {3 ^% T- ~& K* B/ p
}  
% N1 L+ T5 ?/ @% B8 }
+ b" z2 @) A7 y' m- B: mpublic int getid(int count)throws IOException//a method of return the number of data  - J- S. _( X) e8 {' K
{  ' i$ W  X6 e" G2 h
String input;  
5 d) k- o! p5 {1 W) d3 ^/ W4 S7 vint value=0;  & k" I- ~" |  G) M
input=bufin.readLine();//read a user input string from keyboard  
+ G& v$ n, c* ~$ T: m! k; Ptry  & s3 R/ t: p# w0 D3 B+ w3 h
{  
9 G# R& _9 k& _value=Integer.parseInt(input);//convert the string to a int  ; r  K) W  T* Z( {
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  : R& p+ ~5 j& F; l3 y- W6 S
{  0 b" J, K7 L* Z4 h
System.out.print("请输入数据编号:");  
( `6 L; t4 w/ i6 i3 @value=getid(count);  
! @, F2 w) J# q}  . B3 `. j  U- T/ Z0 S, z- P
if(value>count)//the input value is out of bound  
2 t" O2 h* @, R; Q{  $ E+ C4 O- @9 ~- A9 K( [
System.out.print("无此编号的数据,请重新输入:");  8 a7 {* D1 o, H* t" [3 l; j
getid(count);  
- X, ^8 U. c4 i4 {1 m}  
- S" f" s# V+ i1 m( g/ P' }( b* Oreturn value;//return a value  # p2 w1 \/ m- ?3 E
}  # m# T: j9 r3 v9 n; Y: `3 H
}
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 08:16 , Processed in 2.047675 second(s), 52 queries .

回顶部