QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |正序浏览
|招呼Ta 关注Ta
import java.io.*;  & \& i9 H; W9 X1 G) P/ t6 A, p
import java.util.*;  ( E+ I! @! J( y& Z) |9 T
class str14  1 ?. x- t- Y" C; q
{  
1 i0 g& {! m2 k3 ~! ^& sInputStreamReader stdin=new InputStreamReader(System.in);  4 c& l" n; H3 [; [( O4 g4 O0 t9 z
BufferedReader bufin=new BufferedReader(stdin);  
% c+ g( {; U: x5 B- v1 b1 @. n$ g/ c5 w9 n  U  o$ r6 C) O+ E
public static void main(String args[]) throws IOException  
  g1 e0 A# K" }  i! _{  ; w: v: `- `: }, N, N
new str14();//run the application  
5 Y7 N$ B9 K$ X% S1 W* k}  % F. n+ k) j' ]' {$ U0 |! S9 k+ ]
5 S6 }) ?# |! \1 }
public str14() throws IOException//constructor  
' B) ~5 I  _* Q0 Q0 S{  
/ E1 X  X' v3 R/ t; N- P! N; C) uHashtable htable=new Hashtable(20,0.75F);  
3 Q4 S9 _6 W0 N# l' z, ydataBase(htable);  1 a9 p/ [  r. t1 v1 w! d2 S
}  . C) ?! S% ^2 ]2 g8 P+ Y$ Y6 j

# b; _# q( W& d8 i( Upublic str14(Hashtable htable) throws IOException //override the constructor  4 ]( n  V% o& }% H- m
{  
( T$ m) q7 O  K8 N! fdataBase(htable);  
  P0 O8 b% Y. K8 v+ `8 r5 s4 T}  6 _; J  x2 W" {/ L0 e  A

1 G4 y0 Y7 r2 ]& o3 D1 u; bpublic void dataBase(Hashtable htable) throws IOException  4 T  Z: h3 L: j! h+ M8 G+ n
{  
2 U* ^* R4 U* ~2 U1 |9 o. u+ s! J* Iint count=htable.size();//get the ammount of the data in htable  
+ G/ @. s3 f( U- P" v+ O5 sint value,id,num;  2 }7 [1 h: ]/ f) Q( o
String key,enter,data;  4 P" v( b5 y6 |- g- m% R9 j
Enumeration keys=htable.keys();//get all the key in htable  - i' g6 a6 m& c* @- |
Enumeration elements=htable.elements();//get all the elements in htable  
( ?# M1 N% ~# ?  R9 v# A* BSystem.out.println("5 u$ |+ J& T" P/ O, \3 K
Hashtable 简易数据库程序
( J& ^, O. w8 l# g+ V& x0 [");  * c0 O2 d- d' ]$ ?: y3 I. S
System.out.println("(1)输入数据");  
9 q7 \4 [; t' @) n4 ASystem.out.println("(2)请除所有数据");  % @6 u2 [/ j4 z) {/ C: Y$ [
System.out.println("(3)显示单笔数据");  
3 _. s& G& U, TSystem.out.println("(4)删除单笔数据");  1 k5 q# e6 c  _0 t3 b
System.out.println("(5)显示所有数据");  - w. G( r( e, Y8 {# ?7 X* d
System.out.println("(6)结束程序");  9 |5 K  t5 u8 Q' Y
System.out.print("请输入您的选择:");  
" g0 [7 V( U" v! o8 e' P% Fvalue=select();//call select(),return the function number  
8 G- Q1 ]# @1 w' yswitch(value)//the function  3 u% a& H5 U  y  y/ I
{  
4 J* L, O% I5 mcase 1:    T+ Z. G$ n2 w5 w7 c
System.out.print("
! ^7 o& E  |! D' P1 Q+ W% a" n请输入一笔数据:");//need data input  1 H6 R+ k- l) r( X2 l9 B
data=bufin.readLine();  
$ A/ I  V+ e0 O0 Z% J  |count++;  6 w& h9 U0 B: n' l: r" l  g
key=String.valueOf(count);  
  O/ W0 ]0 I, t; ^htable.put(key,data);//store it to the htable  - C, l# D. [! b0 Y" O% e
System.out.print("
9 j" M' C( B8 N+ s* Z输入完成,按任意键继续...");  
4 Z* ?3 K2 h9 l9 ]5 N- ^enter=bufin.readLine();  + x/ `( Z; p+ y% }
new str14(htable);//reenter  ; a5 x, M. @/ J, p. Z; l! T
break;  ! n0 R( F7 D' G5 H8 I& M* K
case 2://clear all data from the htable  
0 |$ O! @3 ~4 w. Mhtable.clear();  
6 }, }$ U+ U* f, z/ ?* USystem.out.print("  V5 c- o  S% O5 {/ N
已删除了所有数据... 按任意键继续...");  5 v: I# d6 @, ^. @( W! J
enter=bufin.readLine();  / \: ]8 K  o7 c# O# i6 _* J
new str14(htable);//reenter  . F4 `# T& m2 `, Q: ^+ A  \! q0 q
break;  
3 I  y9 K* p: U) ~  x  ^case 3:  
0 O( M+ X2 t9 o, K2 W; b8 ]: P) \% JSystem.out.print("& U) U* `8 J% [4 D
请输入要显示的数据编号:");  1 o3 p( ~2 O) a
id=getid(count);//call getid()  , d1 E2 Q: ~; [7 w5 `6 _% {
key=String.valueOf(id);  9 C  t9 X8 P7 E  C4 I
Object select=htable.get(key);//fetch the data from the htable  + m) k3 b" o, A$ J8 z& d. l. ^
data=select.toString();  ! W( o' p! u; p- D6 K" @! K
System.out.print("
8 O  z( _2 [7 Q( b# g编号"+" "+"内容");    D' R: O# Y& m- \! h( y# L- f
System.out.println("
# w& e& j  g+ ^. m7 A' G7 X "+key+" "+data);//display the data  
% j! n; b0 x; {2 N$ g8 Z; F; zSystem.out.print("
; d) m# |' D9 q! q" k按任意键继续...");  
$ U- n2 a( r# G. Denter=bufin.readLine();  % n& M0 F) W% \+ {
new str14(htable);//reenter  
! U- F% v! C; l/ z" vbreak;  - Z. X. |6 ^5 v" |0 k
case 4:  4 X) l% Z7 f) E. o- w: ?
System.out.print("请输入要删除的数据编号:");  ; ~. Q# S$ u; T5 c
id=getid(count);  ) S( h( [: G' k. z) S
key=String.valueOf(id);  6 y/ T2 W; B$ }7 n" `' w0 A
htable.remove(key);//remove data  
% J# D0 r3 @1 N5 }3 {# r0 e7 ecount--;  
" @# q7 A5 q; qnum=count;  8 _3 G* _0 @  w' t5 H
System.out.print("已删除了所选择的数据...按任意见继续...");  
/ C/ v. j& ^% V- b7 j" BHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  , \3 b. p1 e* Y& q7 h
elements=htable.elements();//fetch all the data from htable  9 b, `- ?- |  T) n: F
while(elements.hasMoreElements())  2 O# M: w8 A% W* R" }& L* D
{  
' N* ?" D: `+ A- f" h2 l0 c8 V  ckey=String.valueOf(num);//a new value  & ]* `. H8 m( a* {% ~
data=(String)elements.nextElement();//fetch content of the data    m5 H( L# V: x5 h- A0 X" }+ i- v: @
htable1.put(key,data);//store it to htable1  
' ^( d, ^' @# c6 bnum--;  + \6 A+ Y) M. v2 B+ J- g' J8 v
}  
' l' C% i, h. @htable.clear();  1 S- a: q% E. g% ?' M; ]
enter=bufin.readLine();  
+ ~' C" m9 u3 ~# v/ O2 bnew str14(htable1);  
4 z$ n5 z! \. _1 K/ gbreak;  9 \2 z3 D( }4 ~5 @6 g' }
case 5:  
& n4 Y5 m  u2 s1 M+ D6 J2 AString[] sortkey=new String[count];//create a new sort array  
/ i( G3 M& q: g. T) ~String[] sortdata=new String[count];  0 S7 L2 o7 X1 U/ h6 _8 Q& C
num=count;  
% v0 g1 \0 l9 f) d' T" yelements=htable.elements();  $ h/ N4 M2 E2 _  k( b4 R1 S# E3 \
keys=htable.keys();  
5 D+ G' D! G! Qwhile(elements.hasMoreElements())  
$ \5 d8 [0 K9 p6 o4 q2 q{  
, [& X% {8 `$ {2 O" h! Kkey=(String)keys.nextElement();//fetch value of key  % f0 U8 G( b* ^
data=(String)elements.nextElement();//fetch value of data    [- H1 O; \) T' o7 i& x  P$ M# y/ q
sortkey[num-1]=key;//store the value of key to sortkey array  
, z" v$ p7 y; I/ f' J0 v" dsortdata[num-1]=data;//store the value of data to sortdata array  8 J; b3 i# {; Y7 o
num--;  
6 M) b/ {0 \9 y  F8 w. x}  * h5 d; a  L7 H& Z
System.out.println("
0 ^- [' E" a2 b6 z编号"+" "+"内容");  6 @1 ~# y% ]) B+ @- w
for(int i=0;i<count;i++)//display all content after sorting  
& S8 E1 P1 e3 S$ y$ WSystem.out.println(" "+sortkey+" "+sortdata);  
  G, |/ i0 o- R. q# w! JSystem.out.print("
6 {* [- s9 l' z' ?6 s9 T8 {目前共有"+count+"笔数据");  * a6 }. l& N* o9 x/ D# C( s* c
System.out.print("
1 Z; Q; Z  E1 o5 F! ^5 u# ?: i4 C
按任意键继续...");  
6 X" D  r: P" U* Penter=bufin.readLine();  
- `* j5 {6 K" u; W9 q# Z# Q( Znew str14(htable);  
; k- f* `, b3 A( Cbreak;  
( L" c3 Y  T1 ^6 Z( b5 g4 Zdefault:  
2 Y2 D+ b5 U% s9 V% ^}  
/ a8 K  a8 w. }9 f0 o}  0 Z8 J7 w8 u3 P# j' f* l

/ c2 d+ W: o" Q: Ypublic int select() throws IOException//method of getting a function selector  7 m* Z# g% ]8 S% F
{  * U% e$ q- h2 C0 h6 C/ {$ L0 Y) ]
String input;  5 T$ s& q. M5 |2 m( x+ l1 f
int value=0;  
7 a1 I9 m/ @% c: Winput=bufin.readLine();//read a input from keyboard  
3 B6 L( w1 `5 X+ n' Rtry  
- w5 P$ |; I7 `  `. Z; @' a2 c{  " k8 E" O* K* J- A  X/ {& O$ f: O
value=Integer.parseInt(input);//convert a string to a int value  & P+ y2 @: W& e9 [
}catch(NumberFormatException e)//can’t vonverted  4 V& A/ E" a- e8 j* ^, E; G
{  
: V' F* v* E. N8 U6 QSystem.out.print("请输入选项1~6:");  
! c9 a7 G6 n9 f( Y/ Rvalue=select();  
5 V2 h3 v. E, d$ w; r, j& Q, v}  $ ?+ }4 t1 X1 W* w9 u4 h2 }9 D
if(value>6||value<1)//if exceed then print a message and reenter  & l4 Q6 a0 ^( F$ E
{  
- {: e4 t4 C9 r3 g. bSystem.out.print("请输入选项1~6:");  * O3 \2 U5 U, t- R4 H
value=select();  
8 {* ]+ L" V1 F}  
& W6 {: i' m, Mreturn value;//return a value  2 X& B3 x. P/ }+ R2 k( ?6 [: X
}  ( W$ l6 A: F* q: z! T

0 @/ X# p* w9 @; `' Cpublic int getid(int count)throws IOException//a method of return the number of data  
; K- X" @, O) m! ~$ @{  
. O. l. M' Y3 a& v& iString input;  
5 f( ~: T5 r% T' {* }; ^# p0 fint value=0;  
8 ~; C' y' [, }1 tinput=bufin.readLine();//read a user input string from keyboard  * r3 n3 i# H! c5 T
try  
; M; M0 z6 Q) F8 d{  
" s# u" N, E7 ~7 t, y; @  \4 Tvalue=Integer.parseInt(input);//convert the string to a int  
  q) M$ u( d& C2 c- n}catch(NumberFormatException e)//if can’t convert to a integer then reenter  & ]" Y/ E2 \' N5 q5 o4 C0 n7 z8 j
{  
7 T  e) L1 ?$ e. H8 a* VSystem.out.print("请输入数据编号:");  - ]' \3 k  K2 \, U1 W/ t
value=getid(count);  / u* \; Q' a: _# i! g5 [4 Y
}  - L7 _4 d4 ]5 J9 W8 [7 e
if(value>count)//the input value is out of bound  
0 c& `6 o; n) o7 Z3 n{    `5 N' U  w( G( f* q% u) m/ X
System.out.print("无此编号的数据,请重新输入:");  
- E4 k* h7 C" E: m& \" u* ~& X. ?getid(count);  
: W' M5 v6 f8 I. ?# C2 ^}  1 g0 e. J4 D. K' ]7 r+ m" S: o0 [
return value;//return a value  
, o2 ~8 \5 x& k}  
1 u8 D& a: b6 T+ r}
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-8-8 22:00 , Processed in 1.857516 second(s), 52 queries .

回顶部