QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  
: T; T0 U) S8 Wimport java.util.*;  
) i* V4 I' D. J- `! {class str14  " E3 D( G% n/ K2 O
{  ; m( _8 i4 `4 U
InputStreamReader stdin=new InputStreamReader(System.in);  
* Y1 M- r0 W$ s9 O/ o6 b0 _( _8 gBufferedReader bufin=new BufferedReader(stdin);  # |5 U6 i$ k# N% Y
3 A! ?. U$ F# H
public static void main(String args[]) throws IOException  
- {7 n$ x; @) F$ g{  0 m# }) m1 g  P( l  A( [
new str14();//run the application  
2 @2 c: T' \* L}  $ q2 x- m! P9 d
( U1 \: }+ e$ x9 X. _" f
public str14() throws IOException//constructor  ) J9 H3 @: o) [3 L* x3 D
{  
$ }. q, }* `" _2 Q( F6 v1 C: kHashtable htable=new Hashtable(20,0.75F);  # c5 I6 ~: y% d7 a. a# `0 N% }
dataBase(htable);  % O* K* ^) C( C; `" m3 v
}  9 a! _+ W. _5 f7 @- v* c9 t/ |
) \8 D  X3 {5 v0 L, N" D! u
public str14(Hashtable htable) throws IOException //override the constructor  
0 t1 o" y: g; b. M7 U& W% P1 a{  " m7 r" }/ s' `& R
dataBase(htable);  7 G* A5 `( s2 w* w, I- L1 V
}  
' F2 k. Y& a, B2 I: }
; v4 ~' @4 j: M9 g  u7 u5 epublic void dataBase(Hashtable htable) throws IOException  : ]( _0 [+ r6 ]$ c: ?/ @8 e
{  
! h; R. r: Z+ Kint count=htable.size();//get the ammount of the data in htable  ) R" M$ j2 c- d% H+ c3 [1 ^3 R8 l
int value,id,num;  + N# D, a7 K1 G1 t# O6 q; ~  \+ s
String key,enter,data;  
) E. i( g% R* z. J" I( U' G& \Enumeration keys=htable.keys();//get all the key in htable  
' B9 g# W8 t+ z3 H3 Q; uEnumeration elements=htable.elements();//get all the elements in htable  " j3 \# U0 f6 [7 p+ V
System.out.println("2 k. ^! R6 Q' H  ?
Hashtable 简易数据库程序) D1 A, \2 d/ B1 ~0 ?
");  
- O! j' t1 N% o' W: aSystem.out.println("(1)输入数据");  
+ @# c# q' _9 P- jSystem.out.println("(2)请除所有数据");  
2 m; N# L3 f4 X3 J/ ^- a0 `/ @System.out.println("(3)显示单笔数据");  
6 h( U9 S1 P% Q  xSystem.out.println("(4)删除单笔数据");  
9 _. I& U; o4 Z( s  [System.out.println("(5)显示所有数据");  1 M9 X1 x1 D; F6 ?, c
System.out.println("(6)结束程序");  
9 q* f7 U* E) q, l& J, ~System.out.print("请输入您的选择:");  # A7 i2 M" u. Z; g
value=select();//call select(),return the function number  - f9 O/ r) Q7 _; w3 q
switch(value)//the function  
& S+ c5 Y7 n9 ^/ k) ]! p2 R6 z{  
; b" \5 J% i+ D4 Y: V/ Hcase 1:  0 ^/ `% k% E7 s; ~) R: V( y
System.out.print("2 `% k7 f- z3 D& s# K0 M. l
请输入一笔数据:");//need data input  
/ u1 j) L  R+ P4 bdata=bufin.readLine();  8 Z: B, u+ M4 X' L: g+ P/ m
count++;  
! [! _+ w* u, k9 z' C$ q# jkey=String.valueOf(count);  $ v4 A: Z) T* z5 a
htable.put(key,data);//store it to the htable  4 B- U. a4 ], v
System.out.print("; g2 M  K& E( w( c+ j
输入完成,按任意键继续...");  
  k$ D( M/ E( \' @, Z6 Renter=bufin.readLine();  
# i- l0 p4 O/ U9 V; Dnew str14(htable);//reenter  
9 H" k- E9 n! E$ t. Zbreak;  5 }7 K& L1 c$ m3 I& A6 o
case 2://clear all data from the htable  ; _( S, ]8 I9 Y" l1 F
htable.clear();  7 n( s* m; L! L* i: {9 }
System.out.print("! H7 G; s% d( a' [
已删除了所有数据... 按任意键继续...");  
: b8 o3 J5 Y  ~# Z5 v- |enter=bufin.readLine();  
2 m2 X% n, s9 z5 |$ h4 q% Lnew str14(htable);//reenter  ; B! k3 z: E2 n! f0 x- d8 P" n+ B
break;  0 H. ]) M7 A3 ?4 X$ j. [
case 3:  
* T2 g0 a: p: Y/ W& a1 kSystem.out.print("
+ b3 Y8 t. x2 O  h请输入要显示的数据编号:");  & b) S: w: f" D* i3 i
id=getid(count);//call getid()  
+ p! E, n0 E- a1 z7 j# i9 V; mkey=String.valueOf(id);  6 r+ l, B* R* ?( D" `" {% |
Object select=htable.get(key);//fetch the data from the htable  
9 d: Y- s8 l- K4 m$ {# ^data=select.toString();  1 |0 H; s- q* X* f
System.out.print("
  F7 ?5 h( r; N) G! p' `# D: I编号"+" "+"内容");  0 f2 L  S* C& N
System.out.println("
: l; k* A" I6 Q; z/ P) W8 X "+key+" "+data);//display the data  
& Y) p& h: f5 NSystem.out.print("" ^$ V5 {) ~0 \, ?) R& N: T
按任意键继续...");  
7 y+ ]- S; Q5 ?enter=bufin.readLine();  + n6 z; u1 O# e$ b5 h
new str14(htable);//reenter  7 _5 j! q0 y7 w+ r9 D( n
break;  0 y- P; n5 R! E* T- {: k
case 4:  ; L# j) P% w6 `# ^6 E
System.out.print("请输入要删除的数据编号:");  ) B9 e" [4 x! V3 ~) A2 q& C: l/ [
id=getid(count);  
$ u# O' K, e: Qkey=String.valueOf(id);  0 z7 r( `! p7 r
htable.remove(key);//remove data  . H0 b- ^0 [* C. U$ u3 ^; J5 _- p
count--;  
' Z- ^0 A" g( j; D# Xnum=count;  & G5 z; a' W4 l, t1 Z
System.out.print("已删除了所选择的数据...按任意见继续...");  % d5 p+ \7 {7 `1 N) f  I; r
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
  _, A3 X4 g7 ~" X( U8 l' ?elements=htable.elements();//fetch all the data from htable    Y3 d: {! l% _9 ^. x: |
while(elements.hasMoreElements())  7 b# a# A3 \( E% `, v2 h
{  3 X6 [+ r2 L& x, E
key=String.valueOf(num);//a new value  / r* p4 a0 Q2 v/ N6 a
data=(String)elements.nextElement();//fetch content of the data  # V& g8 F) h) l. D- @& L3 O
htable1.put(key,data);//store it to htable1  9 C  _! Y0 F: ]* d( {, @5 j
num--;  2 E. G2 K  w/ |) e2 `9 h1 E
}  8 U7 g* r( [; v7 w, C
htable.clear();  ; s, x' t  U. J( o
enter=bufin.readLine();  
9 g6 _. F' M/ Pnew str14(htable1);  6 c8 H' _! u  Z& G) S( j) |( Q
break;  - @$ U3 ]& U% r& t4 j- L2 o
case 5:  1 v3 c6 A3 c8 z' A. ]
String[] sortkey=new String[count];//create a new sort array  
8 G: o' U0 i. B5 Z. ZString[] sortdata=new String[count];  3 b. b4 Z5 Q7 t$ i) l/ e9 Y* J4 r
num=count;  * _8 M% V1 r8 G; G
elements=htable.elements();  ' J8 H5 x+ N& h% M# ^
keys=htable.keys();  
3 U* a, N0 [( v, v* hwhile(elements.hasMoreElements())  3 x8 ^# V' R6 Z2 A$ E" k
{  4 v0 Q) L7 g: j- ^& C
key=(String)keys.nextElement();//fetch value of key  
9 o2 ?/ I' {7 p2 Y* sdata=(String)elements.nextElement();//fetch value of data  % S8 d+ U: s- _6 f5 A- A
sortkey[num-1]=key;//store the value of key to sortkey array  * P+ w, L: w: p, w( L3 |; Q
sortdata[num-1]=data;//store the value of data to sortdata array  7 u: y+ x5 g1 a( _
num--;  
/ u$ P& p1 o( c0 G+ @}  
: V7 K1 k- ?9 F1 _8 b. G7 ASystem.out.println("8 t5 @  o  \7 y3 p7 ^8 v* m+ a' F3 m
编号"+" "+"内容");  
" Q' h/ }  J' k4 w( @for(int i=0;i<count;i++)//display all content after sorting  * k' ^" U* V" @' Y' {
System.out.println(" "+sortkey+" "+sortdata);  + r4 P6 K$ ]. E- z0 M: a" k
System.out.print("
+ [8 a% R( [5 B% H0 d6 m目前共有"+count+"笔数据");  8 _2 y! g) J5 X9 G
System.out.print("+ v) ~$ p( R/ ~. x* \

2 p& L5 T6 W* r7 ?. a# O. e5 K按任意键继续...");  
8 g6 X( z! r- v$ T% oenter=bufin.readLine();  / b  B+ l/ C0 p# Z
new str14(htable);  , F* H  E6 Z+ o1 z2 t/ w5 R  g9 `  x
break;  
$ _% |: j/ x9 bdefault:  
5 @# a  o3 v  V' S* d& J# X}  
" k+ d; {8 ^$ ]# T}  ( Y- [$ L5 Q8 z& Z- m
# Y+ `: A& Y0 A6 L- ^2 k$ c8 z
public int select() throws IOException//method of getting a function selector  4 n; w9 O8 W3 [. `8 K, k! L4 T( ^
{  
0 l0 Z- k, F' R8 W9 g  u8 BString input;  ! |# _1 g# \  L& x: ]! P
int value=0;  9 P$ V! ~7 V6 z
input=bufin.readLine();//read a input from keyboard  
, o* y2 Q9 ?/ b+ F* E$ C- C. H% jtry  
, ^$ Z" y& j# i* E7 q{  8 N1 n- r6 B" Z6 ^
value=Integer.parseInt(input);//convert a string to a int value  & E- [* N0 R8 h5 ^! F$ T; R! S
}catch(NumberFormatException e)//can’t vonverted  
* d# i/ L" s6 J/ }4 X{  ! A0 S/ L6 |. o# m
System.out.print("请输入选项1~6:");  
" S1 j- o7 S$ j) Cvalue=select();  
- c+ A1 Z& O( s1 H' s$ L9 z}  3 Y, F9 G6 t8 y1 f( F% L( i
if(value>6||value<1)//if exceed then print a message and reenter  
" W( ~3 m1 l1 L- I+ e{  
7 Y6 s& l* O% ?* S3 b* C1 Z1 hSystem.out.print("请输入选项1~6:");  
2 E1 ?( C+ z; [; Z9 ]/ T  S- D2 Xvalue=select();  
/ b- d7 D6 q4 i) V0 R}  
1 h* O; R- ]' n+ w1 A1 I2 \  Yreturn value;//return a value  3 E- E+ C" P* j
}  9 S* u7 m5 Z3 A5 {' U' d& B8 ~
' N: Y: V) O0 J* e; s; T( Y7 H
public int getid(int count)throws IOException//a method of return the number of data  9 ^5 b- G* x, c$ s
{  
/ i5 M. f0 j; X. C" l4 _String input;  ! `( I7 T) P: `( D. [* ^* q0 [3 w
int value=0;  
; F. `- R# ^, _5 linput=bufin.readLine();//read a user input string from keyboard  0 ~! P; B- n1 p; u- h6 c: s
try  
* [$ C, e7 \' R8 W{  ( c' K0 M$ z' [9 m3 r% B; |9 N
value=Integer.parseInt(input);//convert the string to a int  
" ]7 y& ]6 o$ ^- K& i}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
9 Z& a/ A- ~2 D% t{    r" W( {; G0 t9 d3 z1 x
System.out.print("请输入数据编号:");  : N: T6 q/ ]) J, ?# s& Q+ g
value=getid(count);  7 Y, \- U5 ^5 G
}  
1 l* s. O8 Q. H" Vif(value>count)//the input value is out of bound    O- |3 Q' y$ U' g3 Z
{  1 H7 ?7 S. O! V
System.out.print("无此编号的数据,请重新输入:");  9 [2 |! Z& V4 x! j( Z! E9 G; |
getid(count);  ! V5 i7 f1 Q8 e0 K
}  
2 [. r1 a% i1 l, `9 rreturn value;//return a value  
3 }2 d3 i2 W2 L4 d}  / o; E1 C; @1 u( Y. U" T7 v
}
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
您需要登录后才可以回帖 登录 | 注册地址

fastpost qq
收缩
  • 电话咨询

  • 04714969085

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2026-11-23 06:31 , Processed in 0.163442 second(s), 52 queries .

回顶部