QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
, U! |" d9 s# [) G9 gimport java.util.*;  
, j, w9 q# b2 B& |; V( G- ~class str14  6 X  r' p1 Q' T6 }( w  [) b7 o
{  
% A0 Y: C. G3 |! K/ E! RInputStreamReader stdin=new InputStreamReader(System.in);  
) a$ H% ?$ w7 Q8 ~BufferedReader bufin=new BufferedReader(stdin);  
% }* [1 j: G; I7 e% X/ i: X% x! F) H
public static void main(String args[]) throws IOException  ) \! t7 q) p/ z6 o; e7 D" f
{  ) y+ f9 F  ^8 L; q
new str14();//run the application  & s  T9 |4 k( Y9 V8 B
}  ) K# e* U7 G8 w$ s1 F( L! O
/ S: J0 A* o- B4 V
public str14() throws IOException//constructor  5 \& j5 z0 h' U$ U/ w( Q
{  . J6 T8 Q) a9 E
Hashtable htable=new Hashtable(20,0.75F);  $ ]3 C+ i- W" J3 F3 s' Q& y1 Y+ L
dataBase(htable);  
6 {" e- E& x) T. m, w3 T}  
" e5 q1 w& K% Q7 p, r+ p  ]# L/ r; E$ G5 j- J$ L# ?
public str14(Hashtable htable) throws IOException //override the constructor  
% ^8 k4 G$ r7 e0 m2 U( Z{  " c' }/ f) z8 R  x
dataBase(htable);  ! x9 f: V- ]4 U. j6 p
}  
6 r. f+ w1 w: c  z/ {6 I8 B- P, W# ?3 p/ [
public void dataBase(Hashtable htable) throws IOException  
, X: _, N* V0 i{  0 x/ [. t; x' g2 t! C3 D
int count=htable.size();//get the ammount of the data in htable  # K- L1 W+ Q% P2 T- n# v
int value,id,num;  
) R! U$ r& H$ N# EString key,enter,data;  . U" [, g) m( g9 f$ g9 }! c
Enumeration keys=htable.keys();//get all the key in htable  
0 M* }1 p- F, Q  S& |Enumeration elements=htable.elements();//get all the elements in htable  # k) E9 T+ e3 _  ^5 }
System.out.println("
+ U0 A+ O  r$ R! N% {. B$ RHashtable 简易数据库程序( F5 L. G9 k. p! h* A, J
");  
- G7 V; Z, z( V! O/ ^8 C' D, \1 aSystem.out.println("(1)输入数据");  
" s! G' v0 A; _1 z% v% B! |System.out.println("(2)请除所有数据");  
; o3 M3 O1 l5 H  t- m0 d+ L* OSystem.out.println("(3)显示单笔数据");  . V0 v5 }& S1 R; W$ r( y5 l3 N
System.out.println("(4)删除单笔数据");  
6 Z1 Z% \" N( M9 q, K/ O5 p7 ZSystem.out.println("(5)显示所有数据");  
  p+ ^4 F7 [0 R0 w. eSystem.out.println("(6)结束程序");  
. h0 ]+ v4 K2 K) E% oSystem.out.print("请输入您的选择:");  % X+ F) g( }6 A' J; z4 f( a
value=select();//call select(),return the function number  . b3 k! \% z7 e9 |
switch(value)//the function  
2 ^7 ^% R0 v, D: N{  
3 M% d  u9 J2 ?; }! scase 1:  / z- p) I; k$ w% c0 |
System.out.print("2 W, B# [1 p9 P  ^$ W) m
请输入一笔数据:");//need data input  $ X, \/ z5 Q9 x% ]  a: Y- b/ o8 j+ i
data=bufin.readLine();  
3 @* v4 |# Y+ Y  v2 b& ]- U- J; N8 Rcount++;  / n2 R, M8 i, M$ j7 m' u& [6 y2 `
key=String.valueOf(count);  
% k* F0 s( |( W8 }+ Mhtable.put(key,data);//store it to the htable  
' U' G: h' ?2 f3 S0 w) Q9 F  VSystem.out.print("1 N# X3 Y0 ?. [" e9 V/ t
输入完成,按任意键继续...");  
% w" n+ O: Z: v: uenter=bufin.readLine();  5 v3 T- r- H. D1 e6 _7 q& T
new str14(htable);//reenter  
9 y% F8 d2 \! gbreak;  9 m. j4 X. C) x; t, A9 z
case 2://clear all data from the htable  
' t+ c: ^  x! C4 Z4 O0 ohtable.clear();  
+ c+ V. C- h1 H  h! W' iSystem.out.print("
9 A& t' y" `( I7 G5 }% t已删除了所有数据... 按任意键继续...");  
) M# o: n0 z( k! ?" aenter=bufin.readLine();  
. d, C0 o8 Z2 B$ y1 n* K. k+ r# inew str14(htable);//reenter  1 M2 g+ {( U. b0 ~0 p4 r' U1 [
break;  # g% E; D" ]6 D& C3 Q7 i( G
case 3:  ; G) W2 ?0 _3 G
System.out.print("
3 g8 V/ D- m! }: B2 I请输入要显示的数据编号:");  
5 b( @* \; _1 I: X0 lid=getid(count);//call getid()  1 J( N# ~1 {1 }/ ^$ u& R
key=String.valueOf(id);  $ a+ P. p% E4 A: }4 G
Object select=htable.get(key);//fetch the data from the htable  
6 ~! R5 m1 W8 s  C$ _data=select.toString();  
$ {- B  V6 N% J0 h! ySystem.out.print("4 ~# y4 ]- L) J- D: O
编号"+" "+"内容");  : g  E7 \4 u  B( _. _1 A+ N
System.out.println("& E/ W" Q' `1 X8 G
"+key+" "+data);//display the data  
0 K  F$ R: T+ `System.out.print("
% B! T& {- R3 p8 Q6 j- E0 i  H按任意键继续...");  $ A4 N" t8 \, s& Q+ n4 `0 d
enter=bufin.readLine();  " H9 o/ U% ]1 {% D+ D( G% _
new str14(htable);//reenter  
# @; S& p& `+ Y3 p3 x3 Ibreak;  
! v! V. u# B. \% d! ~# u5 Qcase 4:  . w, Y  f; ]8 n% K/ g8 U* p3 h
System.out.print("请输入要删除的数据编号:");  
5 a( ?6 d" a& ^4 U$ c' Oid=getid(count);  
. U$ D1 F4 J) h; M! `. jkey=String.valueOf(id);  ) R! @( W, \5 R9 D- v) F
htable.remove(key);//remove data  3 t; y- D6 Z0 G0 u- v' X9 T. ~
count--;  - k& o; P/ e1 \( G' k
num=count;  , X8 b+ s) E  ~3 S% Y1 m
System.out.print("已删除了所选择的数据...按任意见继续...");  ) c0 k4 o1 _$ C4 l2 t5 W
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
  {0 J* A5 |# z% P% v; C( Selements=htable.elements();//fetch all the data from htable  
6 o) v8 ?- ?- n! V; m- c$ Uwhile(elements.hasMoreElements())  $ O% B& ^& W9 C
{  
: P5 N& G' I1 V: ^) wkey=String.valueOf(num);//a new value  
# I! g1 `" O. X2 f. [data=(String)elements.nextElement();//fetch content of the data  
+ h/ u4 l' n+ O5 E5 Ohtable1.put(key,data);//store it to htable1  
0 h- {5 E# T; C" ynum--;  
& A0 A; L2 V- l/ j5 U3 ^}  9 h8 m5 j: g! ]/ V9 y7 `5 c( Y
htable.clear();  
. j2 S( ?9 L4 n+ Aenter=bufin.readLine();  1 y4 w+ Z/ ]3 q2 n; m4 X3 h
new str14(htable1);  % d, w" J+ m+ X5 t. a
break;  9 `/ f9 Z. p/ U( Q
case 5:  
" V; J  k! I) ]' e( Y) NString[] sortkey=new String[count];//create a new sort array  ) G! o- }; s+ B  h- d
String[] sortdata=new String[count];  % C5 b3 m# l8 C# g
num=count;  
6 u. L+ P5 ^- A8 Nelements=htable.elements();  
" H. _1 h( P! f! w1 Nkeys=htable.keys();  8 R9 D; P9 P; t) V
while(elements.hasMoreElements())  
1 J5 W; d% Z- i2 s& ?" ?{  1 r) r2 L* n& i4 o. \& O( M+ X1 n1 F
key=(String)keys.nextElement();//fetch value of key  
7 T* h& P. ?. Z0 j" [, X' [data=(String)elements.nextElement();//fetch value of data  
3 ~7 v- t; I9 F0 h0 j: }( B( d* Tsortkey[num-1]=key;//store the value of key to sortkey array  
% }) u, L# L* L" S4 ?& w4 m0 Rsortdata[num-1]=data;//store the value of data to sortdata array  1 a: A0 v, \. _; l
num--;  . ^0 ?! f2 n4 A+ Y+ w
}  1 X3 C& B& u1 c8 n" o' e
System.out.println("
. }! _: C! l8 }' n8 w) W编号"+" "+"内容");  
' q5 ~6 o! S, b$ \for(int i=0;i<count;i++)//display all content after sorting  ' B" E) O% B, _# \# R& Y/ O
System.out.println(" "+sortkey+" "+sortdata);  
  g" ]/ M0 |; v8 C9 j6 g/ PSystem.out.print(", w! t5 H/ g4 S2 t( d) ]
目前共有"+count+"笔数据");  ' Z0 ]2 N( p- C$ X8 S8 ]' {; \( i( `
System.out.print("
5 ]6 ^% K- Z, J) ~/ t, W
* u% E  O% M/ [( }7 E' l按任意键继续...");  ; y0 n7 }) w/ ]/ ^
enter=bufin.readLine();  ' M$ G& z4 a' V: e) f# q" c# A
new str14(htable);  
! {/ }$ b$ j, G2 jbreak;  . T0 y  t, B( Y) D! g$ E% }- E$ m0 m
default:  
7 h" L# I( ~' H( T( ~}  
' E+ m, Y. J" ?, [7 w}  
) W+ T/ E0 X+ w
5 B' I/ b5 b' tpublic int select() throws IOException//method of getting a function selector  
. O$ S. d/ l- x4 t) q{  
, D) A7 ^. M6 Z/ T9 o+ M5 NString input;  
; k7 Z: @2 U2 b3 gint value=0;  ) j7 v3 x2 L: Y+ ]2 e9 H) \% o
input=bufin.readLine();//read a input from keyboard  
( ]$ p0 S! A: z6 qtry  1 e4 f! q- l2 ?$ W- v. Z
{  6 I/ X! e- x9 ]9 Y4 M/ Z& J0 C
value=Integer.parseInt(input);//convert a string to a int value  / p/ N! Z6 e. [
}catch(NumberFormatException e)//can’t vonverted  # t9 ?) ^, X9 a$ z
{  * v/ M6 k9 T4 k3 ]% O% c* J
System.out.print("请输入选项1~6:");  9 R. n! l1 S! P. ^: E9 F
value=select();  
* e: F3 j2 i- D* U" Z}  0 s1 t! p, f/ ^6 U# s% q
if(value>6||value<1)//if exceed then print a message and reenter    \$ C% S. Q4 \6 Y4 W+ z3 r
{  7 p. C) Y0 }$ f# A6 j* O! p8 o! ^
System.out.print("请输入选项1~6:");  ! ?8 x) p9 W- N% }6 p0 c1 t" ?- }
value=select();  
# s  p% E- F) P4 C, \9 R}  . u) I: }2 [; O/ ?' g. W
return value;//return a value  
/ N8 o! G0 J. B; \+ V' ]}  
( O% e; ]" \6 _
5 B* \3 A4 m% n, e& B  N6 u3 ppublic int getid(int count)throws IOException//a method of return the number of data  0 d; G  i3 m" F
{  
8 o' d1 d) X& e0 [/ UString input;  ( @. k% K7 I! r' G3 r+ M. B
int value=0;  0 q1 A# V6 |% b% `6 A8 _$ H8 H
input=bufin.readLine();//read a user input string from keyboard  6 w6 t" Y; ?, Y' @& r; g, `
try  
# [( X/ i( L: t" B; M" U{  7 ^' `  I6 v3 y0 z; z
value=Integer.parseInt(input);//convert the string to a int  ; n, J1 P/ w$ t1 R, y! B  [
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
/ [, |' a- J& \9 G7 A9 f{  
/ G0 v* R6 d9 p5 U( ]System.out.print("请输入数据编号:");  2 x, W- @5 M8 E. a. H0 G( C
value=getid(count);    v. V- S  |7 Q" u: @# b
}  
& B( y0 W: ~0 ^* \; Mif(value>count)//the input value is out of bound  7 t6 a6 d8 @, d' m2 C) y
{  $ I, V8 U1 [2 g( s  o; d% [/ `% _
System.out.print("无此编号的数据,请重新输入:");  
# y9 E1 P0 Q7 f' l9 [getid(count);  ' B) m/ A9 f2 q$ k; C% N  i% E7 i: D
}  
8 w/ [/ J( u. Z+ _1 J6 m0 ]- v, u/ M4 f( preturn value;//return a value  
; {$ i2 ^0 m8 [" W}  0 X: y) u! b1 ~, }% |: q- c
}
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 03:16 , Processed in 0.312184 second(s), 52 queries .

回顶部