QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  2 c5 S3 m! u! ?% v
import java.util.*;  
& H* S. b! t5 F4 E% Z4 [class str14  7 ^2 E% R" B) ?# M2 {- s
{  1 c; y( `% i+ R7 T* w
InputStreamReader stdin=new InputStreamReader(System.in);  
+ q- {& n: V; E! ?BufferedReader bufin=new BufferedReader(stdin);  : g: Y' e$ z# _% F; S# n  M) `0 |
3 S6 E2 X- L, _' s
public static void main(String args[]) throws IOException  
  |5 e; L; \/ n4 x" n9 n9 b; ?% i{  % B, n/ v5 q" C1 H4 S$ E% z) _
new str14();//run the application  ) h7 C7 R$ E" D6 s9 n& _  F
}  
4 r" B$ f8 D% k+ F7 x5 x# v7 G% ], D9 T3 W
public str14() throws IOException//constructor  
: b* e! y( D# ]{  
2 S$ l4 `; T- MHashtable htable=new Hashtable(20,0.75F);  
/ R  T, K$ w6 H1 T$ G8 l! YdataBase(htable);  
3 F) q( ~0 Z+ g* ^}  / p" f) c  s, G9 C5 x
' Q3 U/ m$ S4 j0 x. j
public str14(Hashtable htable) throws IOException //override the constructor  6 n! l& I) p! p8 J2 Q* i
{  
4 r/ D. E& k! v: d+ |- |" qdataBase(htable);  7 H, L5 C( L& A5 V9 P
}  ( C  {0 J; O* k( }
% b( ?7 V- @9 H. K
public void dataBase(Hashtable htable) throws IOException  
- ~" c8 J( n4 {$ \/ {+ S+ G{  8 f" N7 }! [7 c- g. K) p$ E( \
int count=htable.size();//get the ammount of the data in htable  1 L2 O) H# C8 `+ U0 U. b
int value,id,num;  
  E) X( n, ]1 P; wString key,enter,data;  7 z) B" ~) C0 p
Enumeration keys=htable.keys();//get all the key in htable  1 U# p+ s8 ^/ [; f0 p
Enumeration elements=htable.elements();//get all the elements in htable  
: b3 G0 `4 Z8 D# mSystem.out.println("
. u4 N; i/ y& E7 B4 \9 x8 ]9 A; d$ SHashtable 简易数据库程序
; v0 n. m+ K4 O; P6 S");  
* P1 d$ u1 o  [. ySystem.out.println("(1)输入数据");  9 |+ G1 Q4 |1 O8 t+ m" ^
System.out.println("(2)请除所有数据");  
0 }. [8 I! M1 S5 ESystem.out.println("(3)显示单笔数据");  
" J4 p  P: G8 F+ A$ XSystem.out.println("(4)删除单笔数据");  5 h; N+ A! U! N) l2 w; {
System.out.println("(5)显示所有数据");  
8 H: Q3 G* Y# h+ SSystem.out.println("(6)结束程序");  ) v: J4 V4 H* z/ t0 M
System.out.print("请输入您的选择:");    i  E. z! T' d6 ]  P: P! D
value=select();//call select(),return the function number  0 T/ @9 M; h' m
switch(value)//the function  1 g; H) O3 \: G" n
{  
2 W9 f$ K) o' I5 `5 u$ E' kcase 1:  
+ M2 ^# O$ N; r  d4 c  T* ASystem.out.print("
( m1 o/ k( c+ c请输入一笔数据:");//need data input  , r) }  x6 _1 c- H( T
data=bufin.readLine();  
7 _: U2 W, N& N/ f4 j: {; Bcount++;  
* o* i* _4 M3 b! gkey=String.valueOf(count);  
2 ~* X2 A7 q' r* Y$ K/ Rhtable.put(key,data);//store it to the htable  : j  Q0 U1 Z9 g; ^
System.out.print("
, m/ |: ?) v2 u! w" I4 d输入完成,按任意键继续...");  + H. n1 ]3 E9 R1 e
enter=bufin.readLine();  6 i. C' r4 b- P: j) B  r
new str14(htable);//reenter  ) p1 X- W! g9 @+ b: d- i- i  A
break;    L7 V! h7 f- V; K, t1 ]% K& Z  X) ?
case 2://clear all data from the htable  $ {. Z. j: X! H
htable.clear();  , m' V' o$ x4 W+ g+ F0 ?
System.out.print("( _: E- j7 r, a* J0 K# o7 L) [
已删除了所有数据... 按任意键继续...");  
4 x$ I0 `/ _0 U3 Y) O8 fenter=bufin.readLine();  
* J, d# W# d7 M2 n% G/ O) O. @1 lnew str14(htable);//reenter  
8 m% i) z( w$ T6 sbreak;  % u7 j1 B6 ^. c  R( R- ^0 _  S
case 3:  
1 ^6 z& ?6 N: y$ b- l: C' h% _System.out.print("  `5 Q- {7 t% ~# G- x: P
请输入要显示的数据编号:");  9 u1 l" j: o+ y$ V
id=getid(count);//call getid()  5 |$ P) u* C9 h* V
key=String.valueOf(id);  
& M# \& @  ?- a( o' cObject select=htable.get(key);//fetch the data from the htable  + t' w2 n0 M1 x/ H) o  w  A( r
data=select.toString();  
, W+ n+ \: u+ f: tSystem.out.print(", f& B' I9 [' x" m
编号"+" "+"内容");  2 s* [. e8 |3 m% X
System.out.println("
: E* x* r" X6 W* Z3 m "+key+" "+data);//display the data  ( a. C" L. N& g& D0 q0 b: b
System.out.print(") |4 g' n4 S/ T
按任意键继续...");  2 Y+ f: C( X; Q4 l
enter=bufin.readLine();  
1 O  ]0 h/ Y8 ~( P6 c' {) Bnew str14(htable);//reenter  
1 m8 M6 r8 C) L+ N) S0 Lbreak;  , R8 [* K$ }8 A. ]
case 4:  7 r6 z/ ]. m  A2 e
System.out.print("请输入要删除的数据编号:");  1 S- D9 R. I. c; \4 J: x
id=getid(count);  
) y5 p" K( q$ |, z# H) |key=String.valueOf(id);  
6 Z8 V  x0 w0 [/ l/ e( _htable.remove(key);//remove data  
- J* P6 o8 u+ N2 w6 U7 Q: `% bcount--;  
* `# Y# W' |5 i# Anum=count;  ( k# S4 n- e0 k% g$ G. \9 |
System.out.print("已删除了所选择的数据...按任意见继续...");  8 l& g! ~% L1 o7 I, g& g; F3 O  C+ @$ m
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  
- s4 ], t. i2 g0 i- jelements=htable.elements();//fetch all the data from htable  / T& t' \& Y: j- O! x, c
while(elements.hasMoreElements())  6 a# K' e2 m" i) d  e4 t) h4 Z. Y
{  1 U4 c+ @8 W- `" c
key=String.valueOf(num);//a new value  / S4 {% S2 y2 R
data=(String)elements.nextElement();//fetch content of the data  3 h0 F! f* z3 J1 F7 H
htable1.put(key,data);//store it to htable1  $ l7 J  H; H! h0 o
num--;  3 a3 R( j/ N# q, i$ D( Y$ p9 r! C) r
}  ' Z0 N& o: e' N( R
htable.clear();  
; f7 k1 v2 S: }# x% d7 B0 h- {enter=bufin.readLine();  
, R, g+ y! n/ @2 Fnew str14(htable1);  + h- t) {3 f; k% v( D+ q# g9 D
break;  + K! m+ ?( j8 \& E- p
case 5:  9 L5 a9 I4 k. |- L
String[] sortkey=new String[count];//create a new sort array  0 ^8 Z  U6 N: ?; N
String[] sortdata=new String[count];  
8 L+ L  z9 u) z- _num=count;  1 T( R/ v4 V( ]1 G0 t
elements=htable.elements();  
0 f* E8 U2 x; r9 j& _% i5 vkeys=htable.keys();  
( u+ i& O2 z  N2 P3 Iwhile(elements.hasMoreElements())  
# C( _% L3 O4 b! ^  V$ B- _{  
$ K% d  J4 `1 h* _2 d4 Nkey=(String)keys.nextElement();//fetch value of key  
) u. p- Q, Z& Mdata=(String)elements.nextElement();//fetch value of data  
/ ]. h, e& h1 @. c, g+ }sortkey[num-1]=key;//store the value of key to sortkey array  " ^$ I$ u: T% }. Z
sortdata[num-1]=data;//store the value of data to sortdata array  + R- T+ X! Y. R# K0 F9 _
num--;  
, J; j6 g8 N+ G1 j}  # Y# `* ?2 X7 Y3 M3 E* {  C
System.out.println("8 [* ?! O: b3 {9 U8 |
编号"+" "+"内容");  4 C8 ]' H; s4 b: Z* B. ?/ Q% {
for(int i=0;i<count;i++)//display all content after sorting  
# I$ n0 v% z4 U5 sSystem.out.println(" "+sortkey+" "+sortdata);  
5 c3 I2 t6 T4 Q! USystem.out.print("* J, `0 z) r) D; w9 m( _
目前共有"+count+"笔数据");  
( [- |1 p& D2 T0 z7 V2 eSystem.out.print("/ l' D$ i! x& ?3 R9 t' T3 g

9 X2 n9 ^: c' P+ v/ @" B按任意键继续...");  & k! _# z6 m- ~" c0 O
enter=bufin.readLine();  
" _& b; E( S- M) W" Ynew str14(htable);  + G6 {. k  P4 q0 K9 n6 L
break;  
/ N5 t: X* J; c+ m3 A2 O/ G. Zdefault:  
( o* ]% |; n, t3 g/ i6 w! ^* \}  # A. V) M3 @9 [0 ]7 z
}  5 @( E1 z/ q& }7 v5 e; K
8 ?! z( H0 h* f9 i
public int select() throws IOException//method of getting a function selector  * o# F* V( z3 C; i! W2 k
{  0 E0 T! M8 k8 H
String input;  # W5 ^; C+ [$ i( m: A
int value=0;  
# x9 L8 i6 b  |( ^# yinput=bufin.readLine();//read a input from keyboard  
  E& @0 T- g  K+ T" t# btry  
3 [# Y- g8 J! v" O! V9 P$ j{  ) h; A1 w! d/ c7 O& k2 B( c
value=Integer.parseInt(input);//convert a string to a int value  
' Z! f/ e" A6 ?+ N4 u2 b}catch(NumberFormatException e)//can’t vonverted  % u* a" s3 p8 J
{  + a; e- E" d' d' D
System.out.print("请输入选项1~6:");  
, H7 \' s5 A7 d& P$ {: ^2 P& {value=select();  4 t/ A1 d8 s- y& {
}  
6 D0 d$ q4 o, {) x& Rif(value>6||value<1)//if exceed then print a message and reenter  
5 [7 v) S* K" ]- g( Y) v: s{  
; x: c# H7 [' V8 S. L, b8 m+ P8 BSystem.out.print("请输入选项1~6:");  
5 i  K  O1 t+ q; K4 U  bvalue=select();  % O: F9 ]6 {: M  @
}  
5 |( p7 H% S0 b5 b6 {5 Sreturn value;//return a value  
& Y5 h- x- p. y5 C& `% ]! {}  % {7 H# l4 o( X4 S' l/ T4 }3 n
! w! f9 G$ M" V9 N" N% h
public int getid(int count)throws IOException//a method of return the number of data  8 R6 Z7 Y; A. R( K6 S6 J
{  
- O+ v5 a& o8 s0 uString input;  
9 ^$ L! S/ w4 F  u; p8 r' mint value=0;  
0 b/ J; P+ p# ~6 |4 x" W. f% a3 uinput=bufin.readLine();//read a user input string from keyboard  
+ x9 \, A2 R+ a& Wtry  
7 o+ |; |" Q$ Y{  / _5 `* J5 F$ U' ~
value=Integer.parseInt(input);//convert the string to a int  , }- s9 {, d' f
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  6 b8 k/ _5 H" e! n  o
{  1 O% z( F3 T) G- [. [8 y( O
System.out.print("请输入数据编号:");  + c( t; D5 i1 V: j9 a- }" n
value=getid(count);  
) m4 h6 p9 t+ ]0 c2 R5 C' r4 _}  9 S0 O6 x$ {5 L7 R
if(value>count)//the input value is out of bound  # c, z4 R5 T6 S/ c' ?
{  
- O( Z# `% L- n# A) w" iSystem.out.print("无此编号的数据,请重新输入:");  
' E1 I( J5 ^% egetid(count);  
+ ^0 A+ ?) M' X! `}  
8 [% ~( H6 _, Oreturn value;//return a value  9 `3 ]& G- e0 V* c* X5 O9 O
}  # n7 u% X- _. I( E1 n% n) [
}
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-10-8 07:27 , Processed in 0.377228 second(s), 52 queries .

回顶部