QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  % y5 b% x2 d5 }
import java.util.*;  
9 H# {% v! h# M+ c; i" dclass str14  * h; Y* T( Z+ F8 ]) O
{  
& k2 P  P8 S% N' o. o5 R4 e+ LInputStreamReader stdin=new InputStreamReader(System.in);  + s5 f. H8 e) T. ]; \
BufferedReader bufin=new BufferedReader(stdin);  $ v' R2 v% F0 _, ^

2 l/ w  t0 h' B+ Opublic static void main(String args[]) throws IOException  
3 w$ s* W# d; v8 Z! s' P{  + [2 J- x% a. i2 k& m: y( X
new str14();//run the application  * \: u' Y( c' m4 n( j2 Z' u
}  6 W  y  \" e+ S$ k: ?- `
! P8 J  D( F* p" i
public str14() throws IOException//constructor  
0 A) G( ]' L9 @- R7 a1 H{  5 Z( T* B, M' z' T, Y! r+ e% c
Hashtable htable=new Hashtable(20,0.75F);    R7 H7 T& F1 `# z
dataBase(htable);  - B' z' L$ o+ ]0 A0 c
}  
/ o2 j. o6 q7 g- ]5 |. [" i- C: u0 R
public str14(Hashtable htable) throws IOException //override the constructor  
: O1 A  V0 Z2 l4 O# b7 g* [{  
" ]! V/ V7 `2 P! I6 f" |3 g' B+ P2 j0 vdataBase(htable);  
2 H  @9 C/ m) ^+ O! b}  
8 m+ J2 B- ?, j
* `& S/ {# J( y. @& i' Cpublic void dataBase(Hashtable htable) throws IOException  
5 ]# H- N4 X1 d+ v{  6 r) u8 _! y# Q. l# E# r; J* D/ g; r
int count=htable.size();//get the ammount of the data in htable  
8 w" C$ {4 ~  C3 @& g& ], s: t5 Tint value,id,num;  . k* X8 C& A  d
String key,enter,data;  . ?6 T  f9 W4 b3 _0 m! C
Enumeration keys=htable.keys();//get all the key in htable  
) K6 q( e" q, C. G  ~' uEnumeration elements=htable.elements();//get all the elements in htable  " ~! S, b6 `4 {. }, A9 y5 U
System.out.println("
* h* I1 `' i9 JHashtable 简易数据库程序# Y4 p# p% a  Y$ F( ^1 J- b
");  / p! e) E$ E# X3 q& ~
System.out.println("(1)输入数据");  & J8 x9 O7 S: \
System.out.println("(2)请除所有数据");    [, G# n* O: A* F" \6 L$ w. l
System.out.println("(3)显示单笔数据");  / |) a9 y& U. k$ W) W  a! K
System.out.println("(4)删除单笔数据");  
  x6 R; D1 m1 l6 C* D. ySystem.out.println("(5)显示所有数据");  1 V, N' P! d. _8 |, u5 J# N) r2 n& E
System.out.println("(6)结束程序");  
8 b4 `' v; K& s7 e0 OSystem.out.print("请输入您的选择:");  
) h: v- ]4 _" I% Bvalue=select();//call select(),return the function number  
; b' j; U0 b! Q9 e8 I: Kswitch(value)//the function  : F0 W2 {) G7 I4 H$ F: d$ w
{    d% A& Y2 B( [3 M+ X
case 1:  
! }3 T. Z% ?. R$ E: t5 F5 KSystem.out.print("2 [+ m0 J+ ^4 c' j. z/ G9 |9 U2 v
请输入一笔数据:");//need data input  
9 k0 w' }. R1 t3 [# c' Q, sdata=bufin.readLine();  ! y/ N+ O# {5 {2 G
count++;  
9 T- l/ K8 K# @! Kkey=String.valueOf(count);  $ `" m- H4 a( x. d" X
htable.put(key,data);//store it to the htable  
% o: U/ t6 n( z5 P% [2 F: _9 lSystem.out.print("
8 v9 {; b6 a4 M) v输入完成,按任意键继续...");  - @8 z) s3 J1 z% k) P' ?: U' S
enter=bufin.readLine();  9 s# r/ s: d, P! [
new str14(htable);//reenter  
) ~' t; A$ g5 x4 ?break;  ; s; j5 Q/ E- O- f( k1 b: S( k# t
case 2://clear all data from the htable  8 I" x' ~$ L8 G
htable.clear();  
$ l$ Y3 O% ~! Y6 p, M  C/ mSystem.out.print("6 H. O% K* H2 b9 W  a% C5 U
已删除了所有数据... 按任意键继续...");    i$ P) Y" E0 T' x3 n
enter=bufin.readLine();  / ~/ Z' F7 U% j6 y* |
new str14(htable);//reenter  ' b0 z5 _0 I7 `4 e
break;  # L! u2 t2 Q. v( B0 F/ ~3 N
case 3:  
" ~- L1 t5 ]4 U( J& K0 @, KSystem.out.print("
; y, T6 |0 c+ h! j请输入要显示的数据编号:");  . t! ]4 [) C' M8 p6 i! o
id=getid(count);//call getid()  + F0 N/ l3 d2 h( x
key=String.valueOf(id);  + [8 m7 k/ v" v
Object select=htable.get(key);//fetch the data from the htable  " T) n8 f$ |% a7 I! S
data=select.toString();  3 |2 d+ i0 K( J& U# A) D2 u3 X4 l
System.out.print("1 K9 l( g/ X1 v1 d0 X" V: H1 A
编号"+" "+"内容");  
- k6 {! x5 t8 O: mSystem.out.println("
0 K/ \  N9 B: I; u* g "+key+" "+data);//display the data  3 U3 L, Z7 s, R, o9 j
System.out.print("
! J- P: z  E9 P& U- k按任意键继续...");  . V2 r8 T8 P* z& `6 a( M8 s2 ~
enter=bufin.readLine();  
$ [: j0 d1 Q; H) Dnew str14(htable);//reenter  
$ m9 x* M7 |; p  p/ y4 Abreak;  
. m6 D9 ?1 y; Fcase 4:  : L8 k5 H; z# ], M  B: X
System.out.print("请输入要删除的数据编号:");  
$ Z4 U! K" f/ Y  z$ o) tid=getid(count);  
; K* j8 Z4 Z' X- }7 K6 |0 }key=String.valueOf(id);  % Q9 n+ S8 z1 b1 T9 ?! e+ o# s
htable.remove(key);//remove data  
+ s/ D! l) J  r& b) D# gcount--;  
$ ~( P# x& L7 ~' O% i  Z! qnum=count;  0 i! y- I! K; W' f
System.out.print("已删除了所选择的数据...按任意见继续...");  
& Z* I4 O, t7 T- ]Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  + i1 l7 u9 |- _& ?
elements=htable.elements();//fetch all the data from htable  3 v# v* G6 j5 p7 g8 ^
while(elements.hasMoreElements())  * r3 i, ], J8 w" S: }2 F! @  U
{  
' j6 _6 o6 Z  V- Jkey=String.valueOf(num);//a new value  
; w8 v1 }8 t3 d5 M9 R/ W* j. Z" Wdata=(String)elements.nextElement();//fetch content of the data  
8 ~1 d: U8 }4 Z/ N4 @; [2 H! fhtable1.put(key,data);//store it to htable1  
  ]4 ]' P# `7 p  @# fnum--;  3 j1 j9 w: f9 `0 ]: G/ A  U" z* ^  w
}  7 r( u/ H/ I. z
htable.clear();  ! ~, Q2 f  U1 U8 Y  c8 r
enter=bufin.readLine();  
- _1 w% ]( w8 C0 \" Z' vnew str14(htable1);  # S0 n. U; V+ Y& _; S; l
break;  ( y; S) i2 K9 _
case 5:  
: T' b0 t; {% j" ?7 y1 M, j9 MString[] sortkey=new String[count];//create a new sort array  
( q. H3 ?0 h0 X7 O9 Z/ o) M; bString[] sortdata=new String[count];  ! |# V# ~# k0 x1 H
num=count;  7 d/ h% ~% e4 M  `4 a  D/ _* z8 Q
elements=htable.elements();  
$ I( O2 G3 c0 Pkeys=htable.keys();  # H6 k9 @; L( x, M* e
while(elements.hasMoreElements())  
" w3 M: W5 y; J" ^' F& ~{  
1 m  G' Y' ^4 p9 A1 Ekey=(String)keys.nextElement();//fetch value of key  2 V: M6 G5 b. F/ A
data=(String)elements.nextElement();//fetch value of data  ' _4 V7 Z/ d" A. b7 M( Z" U
sortkey[num-1]=key;//store the value of key to sortkey array  
$ f4 R' q, U; I/ c" Y% q' Bsortdata[num-1]=data;//store the value of data to sortdata array  
0 l- H; ~0 z' a/ j; e- f2 P6 Cnum--;  
+ f. g4 k% q4 t$ }# k}  
. V& E) o0 T" H) {9 u! ?8 hSystem.out.println("
' @1 D9 z7 l( S2 k: |编号"+" "+"内容");  6 }8 F3 c4 X$ {( S2 Z
for(int i=0;i<count;i++)//display all content after sorting  . m# z. b1 J  G' M8 s
System.out.println(" "+sortkey+" "+sortdata);  
5 L# `' @: ?9 ?/ N; ESystem.out.print("
2 I# f4 v& p  ?目前共有"+count+"笔数据");  ( T9 U9 E, \) J7 m  m: ?
System.out.print("4 h$ x% v9 M7 s: P2 E

4 M$ N: D+ n/ G1 y- |5 |4 t8 Z3 t0 p4 \按任意键继续...");  
/ Y% R6 N: A, X' uenter=bufin.readLine();  
, [# n) |! U+ o% Wnew str14(htable);  6 e- s* _; R% G- S4 ^; P( v
break;  
! T$ f! n: o& M2 |default:  
; W1 M* J) I% g: r( _}  
) q0 U2 Y0 ?1 \3 w1 t}  2 o/ s7 a  O$ r6 T7 j: r0 r4 I* T

! f6 _+ R# E# i# z" wpublic int select() throws IOException//method of getting a function selector  
+ T7 X! J6 b+ O6 ~' M4 Y{  
% q9 a. t, o. r$ ~' Z9 u" q, {String input;  
3 Q: r! F6 a  p# v3 z# wint value=0;  
% z5 `6 p$ J# z/ V- w+ p' Cinput=bufin.readLine();//read a input from keyboard  6 @& z7 e9 T% `; k& T; ^
try  
$ a6 c3 {! A% S" o{  
" B8 Z4 P" b# Tvalue=Integer.parseInt(input);//convert a string to a int value  
8 S' c. d5 Y- |. B}catch(NumberFormatException e)//can’t vonverted  ' s6 S; z5 I, P7 P. b8 l9 U1 g. h. Y: S
{  
1 r. L( P3 r+ C1 k9 V' P4 d* M) ]) G% s. xSystem.out.print("请输入选项1~6:");  
/ j0 J% \/ q4 f4 {value=select();  
2 |& q  {! b6 C}  5 ~: }7 h5 G$ Q) G
if(value>6||value<1)//if exceed then print a message and reenter  3 ^8 F4 g" @2 m2 _9 T9 n9 M; x. S
{  3 \" f. s8 q% g0 x, O( x! Q) ]
System.out.print("请输入选项1~6:");  
3 m1 y) m. g! H$ g3 c/ H, o+ S$ @+ U- avalue=select();  & x8 E7 |4 k* Z) l" k. i
}  7 m+ j" o+ }9 K$ a5 c# d& T# T# Q
return value;//return a value  
4 s5 z/ r! c$ m$ }}  
* ]. z( \+ ^& D$ H
4 y6 y. A. J+ E8 w  z3 Q* [public int getid(int count)throws IOException//a method of return the number of data  
3 S% \& Y5 k9 _{  
0 q6 m2 K1 |5 t+ K- B: q' x2 zString input;  3 u5 E! a$ `  }3 S
int value=0;  
& \  N6 x/ D! d! @5 iinput=bufin.readLine();//read a user input string from keyboard  
9 x; L1 V7 f8 x) r  U; `try    ~/ z% _: O: {- G/ z
{  
7 K' e' t4 T5 J2 pvalue=Integer.parseInt(input);//convert the string to a int  
1 Y* s. D) o# N2 [. k% W; ~2 l}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
  U, u0 ^& e% U3 O4 L{  * W' J0 U2 H/ {7 J8 N
System.out.print("请输入数据编号:");    B- ], e+ }' B
value=getid(count);  
, K0 ]3 [/ T2 i7 b}  
' q4 N6 N/ j- ?' D0 L3 Vif(value>count)//the input value is out of bound  
9 h- l5 q2 N3 \2 |6 |0 d, ^{  
2 j3 L- Z5 j, @+ d( tSystem.out.print("无此编号的数据,请重新输入:");  
8 `+ W7 v0 z' t0 J' K0 H0 hgetid(count);  
/ z8 o$ F3 H# @}  $ H8 A$ S8 l. s3 ]3 K; [+ O
return value;//return a value  
" [% M/ Z0 \6 Q* Y- w+ L}  
: E5 [9 k4 K; A/ E. Q0 Z}
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:18 , Processed in 0.245195 second(s), 52 queries .

回顶部