QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  $ s7 K7 d0 |/ x4 f0 _  t7 b0 h
import java.util.*;  . \3 E2 u. F8 ~
class str14  - a4 r: ], F) j8 J
{  
7 C4 H4 r+ T( [InputStreamReader stdin=new InputStreamReader(System.in);  4 S7 @1 v1 k. X* T: ~3 F/ h
BufferedReader bufin=new BufferedReader(stdin);  
: L. a1 t: i8 l* T0 K+ a
. s4 F6 Z2 x" [9 i" Wpublic static void main(String args[]) throws IOException  
" s5 k4 o2 t3 x; z1 J) l: V0 [$ Q; Q4 ]{  % Z7 ^) u" I2 g: Q' d. v
new str14();//run the application  / d5 z; |- q5 {6 v, M3 d- m+ R
}  
3 j; R1 h- H# c% \. J; g$ v' d7 G
5 _/ [  q' U+ m/ o7 }& l6 apublic str14() throws IOException//constructor  
8 U1 [0 w" @. Q- w{  
0 b8 a8 p# n. e) H6 f8 r" THashtable htable=new Hashtable(20,0.75F);  
. O: x/ ~- }8 L- F' `dataBase(htable);  
  u; |9 M8 J1 n* q}  ! Q5 |) K! k, v! i$ }+ w  F, g

; E) u% _1 d2 C0 }1 M) t3 e; V5 Qpublic str14(Hashtable htable) throws IOException //override the constructor  ' H0 a3 K: H0 z5 l: u: {2 a' b- J
{  
+ ^3 |* h3 S# @# {dataBase(htable);  1 \1 \2 O* f/ V' ^* R0 c, k- K
}  3 u3 g1 \+ ]% p; h9 l, A/ M

0 \! S' P/ N" U' Xpublic void dataBase(Hashtable htable) throws IOException  . q/ D& H3 c( i- |1 E# A
{  
6 Y. P* |+ }) }! V# `& Nint count=htable.size();//get the ammount of the data in htable  1 h* i+ |9 u& K: o! \4 u& t* Y
int value,id,num;  4 q) C! E, u  l5 i; q( P
String key,enter,data;  
5 v; r5 @- f9 E/ i' vEnumeration keys=htable.keys();//get all the key in htable  $ \& V7 k- a% w4 i% [  b/ \' @
Enumeration elements=htable.elements();//get all the elements in htable  
" o/ t2 T4 N, M; R, _System.out.println("0 |; f  D" J  j+ C4 N
Hashtable 简易数据库程序
* H7 U4 q( c- e# ?- Z, `. [( x");  $ c7 b8 J, C/ i* W+ E% v4 F8 @, j
System.out.println("(1)输入数据");  
0 e% s' @8 |+ X1 b( V' u- GSystem.out.println("(2)请除所有数据");  
" w' p% {/ s. T; t, jSystem.out.println("(3)显示单笔数据");  
! G4 B+ g. I! O/ ^* ]& jSystem.out.println("(4)删除单笔数据");  
3 w! U! B! u+ e. {8 I6 m/ SSystem.out.println("(5)显示所有数据");  9 @8 |, V" ?$ z' ~
System.out.println("(6)结束程序");  ) Y6 C# P5 d, X7 R! ]9 a
System.out.print("请输入您的选择:");  / n8 {$ }& T/ `& q+ r" x4 r
value=select();//call select(),return the function number  ; a3 S* ^. c, Q7 A! C8 r
switch(value)//the function  ( O% d9 x, F3 z* U/ u" B- n
{  
; q' ?6 T' P3 ^3 t+ ~- Q- \) G' Mcase 1:  . t- f+ S2 p( Y9 T( @8 s6 T
System.out.print("
% v5 z9 Z" A% r1 L- b请输入一笔数据:");//need data input  # o  m9 P7 X, }; z, C0 W8 P1 C& Q% F
data=bufin.readLine();  
$ ?- h, v4 y/ l  Xcount++;  
9 ^; t& f* ]7 [0 Dkey=String.valueOf(count);  0 t+ z% u: e& ?9 b! Z: H
htable.put(key,data);//store it to the htable  
7 p, S& H4 }, T9 d9 L* c6 hSystem.out.print("
0 z/ c2 C8 Y- E! B- J7 H输入完成,按任意键继续...");  
1 \+ b) d9 }( n* W- qenter=bufin.readLine();  , x( I4 R7 b3 S9 m
new str14(htable);//reenter  
% I$ `; _$ R5 t  J# R+ ebreak;  
8 l; T  [2 _7 _0 w) {/ x4 ]case 2://clear all data from the htable  % s+ n6 }7 j+ R. \) F
htable.clear();  2 y# U& _: K/ \; \7 T! D" x
System.out.print("
3 I9 @% x# x: R% I  g已删除了所有数据... 按任意键继续...");  
' S$ E1 i- [  Y" r2 C) V( a. l2 center=bufin.readLine();  
# z9 x2 L. T5 {" }. ^9 Xnew str14(htable);//reenter  
" [# A* a- ]7 m2 `& h# F4 Pbreak;  - C+ O# ]: Z' g! S
case 3:  2 g7 n; O. Q* \9 [$ ]1 x$ {  j# w
System.out.print("
& Q2 K! F( x- L9 f3 n( M请输入要显示的数据编号:");  
: W& l- [8 E' i4 Aid=getid(count);//call getid()  
' I8 \; Y1 t. I2 Bkey=String.valueOf(id);  0 O; `' c* @  p# [- y
Object select=htable.get(key);//fetch the data from the htable  
* ^, T$ D: U& [+ U5 |data=select.toString();  
8 V: C. F1 L  ^System.out.print("
' v. s, }2 P% A7 L- \4 a编号"+" "+"内容");  4 q; b/ D# f7 I8 ]7 E
System.out.println("' o' a7 n5 U) n& [0 Z8 t' l+ c
"+key+" "+data);//display the data  
! [# G+ c3 k1 ^1 Q/ H; mSystem.out.print("
$ K' H! A+ [6 M! J7 V7 P# U. \按任意键继续...");  
1 Z. x9 k  h, @+ k4 w2 [9 j( |. genter=bufin.readLine();  # {! b; @5 c; l; P+ a0 K5 X8 j
new str14(htable);//reenter  
% B/ b/ K* b6 _  R9 G: Jbreak;  9 N9 m/ B  t. {
case 4:  ( O9 k( e3 O1 Y% F0 T
System.out.print("请输入要删除的数据编号:");  4 v  S1 d# m( L( ^
id=getid(count);  
3 x  j' b' `; `5 Q7 ikey=String.valueOf(id);  
) p" I' `. ^( l! Z3 `& khtable.remove(key);//remove data  & u. }* m8 o3 k- M' [* W- e% s
count--;  
) P0 I  B3 T6 O3 B1 Xnum=count;  " r* R1 I/ |; l4 Z2 r( a! ^
System.out.print("已删除了所选择的数据...按任意见继续...");  
* Q6 _+ V2 ^/ mHashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  " P2 b6 K% I- v! L1 |
elements=htable.elements();//fetch all the data from htable  9 G& H0 l* y% y0 Y1 y9 q5 ~+ `: _: I' S7 n
while(elements.hasMoreElements())  5 C" T. E9 {6 P# p
{  5 u6 I  ~4 W1 h# s! D
key=String.valueOf(num);//a new value  - [: q- @& b' d) }2 i# y2 T
data=(String)elements.nextElement();//fetch content of the data  
& `# E9 d7 e, d- h% N: e5 mhtable1.put(key,data);//store it to htable1  0 F/ R) D0 b. u. y: @% V1 c
num--;  
0 u5 o9 |4 [  m6 E: ~1 Z}  ( F# h7 G% A9 Y2 `) ~6 R
htable.clear();  , l, D$ m) W* B" Y
enter=bufin.readLine();  
. u4 r3 D# ~( r) enew str14(htable1);  
. P8 @1 z! a1 z* Z2 X- ^( Fbreak;  
/ K" o% ~' n  V# L' ~- K* w6 _case 5:  
% N" Q( L, P- hString[] sortkey=new String[count];//create a new sort array  ; D3 g+ Q- t$ p+ B
String[] sortdata=new String[count];  * w$ G" t9 M( M/ r, m. V0 y7 u
num=count;  8 {, ^' q* \, @
elements=htable.elements();  6 b4 r( S' S; {+ @
keys=htable.keys();  4 t0 p! ]  |4 F6 |8 M
while(elements.hasMoreElements())  $ n8 ?+ n5 {4 t
{    ~4 y2 r9 A7 x/ A1 Y/ |0 q! N
key=(String)keys.nextElement();//fetch value of key  # c9 r5 ?, s- z0 X$ r+ P
data=(String)elements.nextElement();//fetch value of data  
& _+ y$ _+ O. v0 |! r* [7 C2 }sortkey[num-1]=key;//store the value of key to sortkey array  
2 Q( o0 x) W; @/ H% O" L% ?sortdata[num-1]=data;//store the value of data to sortdata array  1 n" }2 q' j: R2 n1 t0 H
num--;  ! _7 Z2 t9 O. s1 ]
}  
. w0 `: O3 V! W" I6 DSystem.out.println(". G3 f( p+ r% k
编号"+" "+"内容");  7 L, A6 G( c# v
for(int i=0;i<count;i++)//display all content after sorting  0 I( B  H& f5 s" }. Q, r
System.out.println(" "+sortkey+" "+sortdata);  - x  a8 ?  v* f
System.out.print(". R1 \) y( w$ y4 c& z4 @+ K4 G
目前共有"+count+"笔数据");  
- T- o, n8 C' ^System.out.print("6 l* |0 P, ~2 f6 r0 R1 S
; K% V( F0 E& a+ ~; }0 A2 z
按任意键继续...");  
  c7 I. p! L/ s9 ]enter=bufin.readLine();  
, `! N+ f, [7 |8 g3 U8 p) S8 }; tnew str14(htable);  
) m0 i* r# p+ L/ _break;  
  a. F- y6 Z+ w" E7 z$ A& gdefault:  
" F$ }# d. l3 X. `5 ^! l1 k}  
' T$ r$ P- K* Z' d) {1 z( \}  
" ?2 D$ D9 I+ h* _1 s$ g1 \# h. h3 K' ^( q! k  o1 ?3 ^
public int select() throws IOException//method of getting a function selector  7 |& h, C; [) _* Y3 m7 T1 m
{  
4 [# n1 N! ~+ d- @. w. ?; r* |8 XString input;  1 a. I7 W* g& T- `) O- e& G
int value=0;  & Z  q9 b% N* B. L7 h  J4 h: I
input=bufin.readLine();//read a input from keyboard  
& ?" f$ I/ M; }try  ! x9 Z" K' \* z/ w) J
{  5 @$ a3 z/ ]2 _2 B  R
value=Integer.parseInt(input);//convert a string to a int value  5 m" S3 N- g: X) x' J: g
}catch(NumberFormatException e)//can’t vonverted  
7 {% O& v& J5 k5 n( s" R8 k# `& k{  
/ q' G. U6 c5 bSystem.out.print("请输入选项1~6:");  1 d' [  i7 U# c4 p  I
value=select();  
& S  L7 @& |1 Q6 F2 R}  . H( D3 Z* F; t3 x; f3 \
if(value>6||value<1)//if exceed then print a message and reenter  4 S1 ~: x, f' B' `  o
{  / u' |6 L; n& _+ e
System.out.print("请输入选项1~6:");  
% C; h  L1 |( tvalue=select();  
8 f' K) M% f/ e: T# W0 y}    ?4 I# E7 w7 u' Y; c# s
return value;//return a value  
, s! k3 j$ X" ?+ |. V+ a* e}  
: _5 g7 d1 n" r, Q9 {% f
7 D+ b3 N( r% S5 qpublic int getid(int count)throws IOException//a method of return the number of data    d% e# R' b* g9 H+ g5 c
{  
: @3 r( x/ I# s$ r, \String input;  " F+ O. |4 j. S
int value=0;    a. V3 A8 l1 N
input=bufin.readLine();//read a user input string from keyboard  4 I4 j6 I9 M$ e. e, W8 G+ C: }9 E5 ~
try  # q+ |: e: [9 V7 {' }  a' U
{  % \* B2 G7 P& @9 P$ Y
value=Integer.parseInt(input);//convert the string to a int  ! K7 x& y, H9 Z; G
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  : X0 I) b7 @& j' _' O+ i
{  1 q) \' T" U1 C& ~( b1 I; R
System.out.print("请输入数据编号:");  
) s5 Q1 ~# R3 i; u( w2 fvalue=getid(count);  
. A2 p& a1 N; y: w( R- S/ e+ ?}  
* G! T/ w1 X4 m  }& D. Gif(value>count)//the input value is out of bound  ) o% E/ b4 x, g% k+ r: q' j( G
{  + \- V3 k/ E  M2 D% e! J1 G  p: i
System.out.print("无此编号的数据,请重新输入:");  
( j& I1 r9 C) W: y/ H5 x: D9 C& R! Agetid(count);  
4 G+ T1 L' X% e9 e( r& ~1 ?}  
0 Q& B& @/ c1 J- o% [; d) a! W; yreturn value;//return a value  ( t& i% ~. u$ K7 u
}  7 f) I/ n* j6 H6 I# v* K+ H0 \: s
}
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-23 05:59 , Processed in 0.551491 second(s), 52 queries .

回顶部