QQ登录

只需要一步,快速开始

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

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

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

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:45 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import java.io.*;  - |) }' C$ H7 N* |/ B  X+ k
import java.util.*;  # y+ t1 j2 x4 b, S! y7 E2 d
class str14  ' w0 u5 A, k: T! y% x
{  1 y$ l; m4 @# g+ A; F
InputStreamReader stdin=new InputStreamReader(System.in);  
+ y- a: L( U; a8 f) d' MBufferedReader bufin=new BufferedReader(stdin);  8 B: y* L8 v4 w' M# o7 D7 Y3 `

, f7 J9 x- @$ w4 A6 ~% R( n) w2 Tpublic static void main(String args[]) throws IOException  & U5 w4 G' u: Z7 |2 \/ e! Q
{  
& s  X- X6 ?: Q# J: ]: Vnew str14();//run the application  
: b( i" V+ e3 U9 E8 O}  
' m. v* \$ }1 d  X$ ]2 L$ L. \: [* `5 N( ]8 c* e
public str14() throws IOException//constructor  ( Y' Z. ~! E  C; w" N
{  
7 Z7 _5 X  S) v! c6 ~% S  ?Hashtable htable=new Hashtable(20,0.75F);  ; s8 G, x) B6 A( ^8 u
dataBase(htable);  ( V2 \: Z9 [/ d  q
}  % r/ E5 ~0 E! Z
: m8 \# M0 {) D1 V5 J! \
public str14(Hashtable htable) throws IOException //override the constructor  - t6 G& W! w% z
{  ( X, s' l; \+ x
dataBase(htable);  ' H: O* P! o4 @4 s. a/ K% m: ?
}  $ L; W8 A, B# j

( B- \0 T& h: z  `5 vpublic void dataBase(Hashtable htable) throws IOException  $ b' P2 x# f& X( i8 l$ i
{  ; A; H1 G- r4 f" e( g$ o
int count=htable.size();//get the ammount of the data in htable  
% n9 G7 p; n, \( eint value,id,num;  & \2 U) B; p6 j$ ^, m" U
String key,enter,data;  
; Z" E  G: J( C- P* M% ?' X% |Enumeration keys=htable.keys();//get all the key in htable  % U3 Y: J2 F$ d% X  K- P
Enumeration elements=htable.elements();//get all the elements in htable  - {. B, C% C4 S* l' n5 G3 V% _' |
System.out.println("3 U  m% W- ?: s% i- O
Hashtable 简易数据库程序
5 ?- ^3 p& |3 z% q6 m");  - W, `" A  W/ ?+ ^9 P) _# T9 s: H
System.out.println("(1)输入数据");  
5 o% \% G; R6 j$ ]& b  ^System.out.println("(2)请除所有数据");  ; n% Z8 O, l) f! P+ i2 K
System.out.println("(3)显示单笔数据");  
' R2 q- q4 ~: m/ i- e- gSystem.out.println("(4)删除单笔数据");  
8 r1 S  K- C! s1 L+ ?1 pSystem.out.println("(5)显示所有数据");  9 q0 T4 ^/ |: c+ }# V7 Z
System.out.println("(6)结束程序");  
+ Z, r9 q* Y. @4 {System.out.print("请输入您的选择:");  
9 |3 c3 C  `% b: L* z7 Lvalue=select();//call select(),return the function number  1 N6 x4 |0 `) P8 Y. ?6 c5 s! Z
switch(value)//the function  
. h& A/ Y3 U$ m5 C8 j5 s) q{  
' p' Y( D  c) [: I5 W5 t" ]2 Icase 1:  
$ P$ {" u" r7 aSystem.out.print("4 t5 k( K5 W8 ^6 u4 Y
请输入一笔数据:");//need data input  
6 Q* [* Z8 P1 c; u( }data=bufin.readLine();  
; {5 v. a9 J( x" r% icount++;  
8 d& O7 q. L- C6 mkey=String.valueOf(count);  
' F1 V- |& @' k( g$ q; L0 Ohtable.put(key,data);//store it to the htable  
% H9 {( {# h  ^7 KSystem.out.print(". l$ ?. V1 _6 y
输入完成,按任意键继续...");  7 I4 K/ B7 ~9 m9 t
enter=bufin.readLine();  ( e2 Z6 @5 z* a: @
new str14(htable);//reenter  + Q/ {9 h( k; O* x. J
break;  
8 ]" D2 h. q8 m" F) X  Ecase 2://clear all data from the htable  + x, V( \5 u9 [9 ~
htable.clear();  
- ?7 ?% Z0 K' b  D9 _System.out.print("2 @  _( Z" u% K2 F/ z
已删除了所有数据... 按任意键继续...");  
. R1 I8 B" Q0 E3 tenter=bufin.readLine();  
% x3 i/ F) M, X0 bnew str14(htable);//reenter    {' p; S, e9 @4 U  H, T- s
break;  / l" Z6 n* ]( j0 d7 L6 ^
case 3:  
& n7 F$ e# g* a& [9 WSystem.out.print("  {! Y7 i0 U0 z( W9 S
请输入要显示的数据编号:");  
* c7 I' [4 b! M" r3 h* V1 Wid=getid(count);//call getid()  1 _3 @7 \2 g/ M. J! j& c
key=String.valueOf(id);  9 U- ]5 g- E6 X- O; O5 E* Z
Object select=htable.get(key);//fetch the data from the htable  0 m4 I# N, P4 [
data=select.toString();  , Q- w4 o9 X4 ^' F, D; t! c% O
System.out.print("
5 {" {8 C5 x3 O) a7 S) |编号"+" "+"内容");  
5 _' e6 t% ?- rSystem.out.println("9 s8 v- J; ]+ d# a
"+key+" "+data);//display the data  
4 _; w7 T! J7 R0 mSystem.out.print("
: F* w2 u3 g# }3 K按任意键继续...");  - G( H+ y( z8 `* t/ ?5 [$ [0 t8 X
enter=bufin.readLine();  
/ Z. N8 ~. J% [1 x( K8 O+ h8 k0 znew str14(htable);//reenter  9 \' K6 s6 x6 c/ H. h; C; s7 r8 D
break;  # u8 A& D7 I+ j0 z% q% N
case 4:  7 ?4 [& g4 H0 c3 l, g/ v& m
System.out.print("请输入要删除的数据编号:");  : K0 m# ^$ j& V: H2 ~  A. G
id=getid(count);  1 L/ O: E, c* E5 A) o
key=String.valueOf(id);  
$ `3 i2 O% z. ]8 N. _htable.remove(key);//remove data  9 y4 [5 @) Q1 T+ j3 P
count--;  
9 J; m# y, n# k: c/ {num=count;  
2 ]% X/ I# I0 BSystem.out.print("已删除了所选择的数据...按任意见继续...");  5 D5 H; l" k1 ]/ E
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1  & m! G, r' B2 f6 @
elements=htable.elements();//fetch all the data from htable  : g- @* y" ~5 A( L4 }
while(elements.hasMoreElements())  
: |9 {8 U: P# ^  j: k" D5 j* p{  
( R0 _6 a! b0 e% Pkey=String.valueOf(num);//a new value  
# z$ I3 W0 i$ [! S, Z& `data=(String)elements.nextElement();//fetch content of the data  ' H2 R* \' ]7 Z" \
htable1.put(key,data);//store it to htable1  
: F2 e) Y; j3 X; h3 ]: snum--;  . H' M+ y% c$ G  Z2 O* W
}  
! z5 W& R& b" K/ jhtable.clear();  ! V3 Z7 m4 _7 e+ Y
enter=bufin.readLine();  
4 j: j" N6 Q6 e% Anew str14(htable1);  
+ A+ `9 _0 }. F6 xbreak;  
" ~, i. B; H6 M& h. z1 u4 c1 gcase 5:  
8 D5 [. D' p! Q. r+ \1 X$ V4 ZString[] sortkey=new String[count];//create a new sort array  
: G5 @3 I1 W/ ?' oString[] sortdata=new String[count];  - x' s! u9 m. c1 F% U8 n, Q, ^
num=count;  / Y' k  P& z' i+ l7 A* m) {
elements=htable.elements();  
+ q& B# H# V( J0 j' i5 d, ?6 rkeys=htable.keys();  
; A+ i% x  |6 vwhile(elements.hasMoreElements())    X* F+ [7 c& b+ E# y
{  
* g3 P; N1 k* Qkey=(String)keys.nextElement();//fetch value of key  
8 k6 Q* ?% g+ v* X  }6 {data=(String)elements.nextElement();//fetch value of data  
7 Y. H: @7 K& P' x" _% msortkey[num-1]=key;//store the value of key to sortkey array  
. O2 [! J) k$ J% @9 xsortdata[num-1]=data;//store the value of data to sortdata array  
" @% N0 y& m6 H+ _# u, ~  ~) Hnum--;  
# }# Q% j* D# U" S" u}  
4 P/ G& j2 _5 ?2 H; l5 n; P' rSystem.out.println("1 c9 n. s2 p! Z8 p! F) Z  j7 @
编号"+" "+"内容");  9 H; [' P% C# O) _4 n8 r
for(int i=0;i<count;i++)//display all content after sorting  8 W- S7 u' j4 v7 F1 _( F
System.out.println(" "+sortkey+" "+sortdata);  * {- a6 f8 G* N8 m  _* I7 ^
System.out.print("1 v5 D* F; o& J- U
目前共有"+count+"笔数据");  
; ?5 _8 T8 i4 p: k" X" I$ n6 P, w( ASystem.out.print("& P! R5 i( z- m. b9 z5 M5 |
9 H7 r; O  ], o/ E0 f! J3 j5 u
按任意键继续...");  7 c6 l( C) _. S% L. b* T
enter=bufin.readLine();  4 ^/ N7 X5 s) h6 W0 ]4 S  Z3 S4 R
new str14(htable);  4 R; K8 {& j4 |
break;  
6 {. ?1 k( j- n6 H9 X# ldefault:  
" w6 c0 s, E2 d! l& O}  
( W) b7 d; @& }7 i' D}  
$ N: _& q8 ^3 a( |; \
/ J. \9 W7 d" F2 u) rpublic int select() throws IOException//method of getting a function selector  8 X1 l/ ~+ L+ @
{  
8 a) z1 w* f4 A& V1 lString input;  
- c- D6 e7 y8 k2 c, U$ x: Zint value=0;  
/ Y" y, R6 o7 Cinput=bufin.readLine();//read a input from keyboard  , A# c& s9 C% m$ @& Y3 ^
try  
# N' @" Q5 ~9 U' m+ A+ w+ G{  
" ~: l6 u% p( I: C& N4 F3 ~value=Integer.parseInt(input);//convert a string to a int value  ' t+ P6 g6 f$ ^7 Z$ V6 k" G
}catch(NumberFormatException e)//can’t vonverted  
$ M& d4 j: K  ?- d: L, Y- z{  8 \3 M3 c/ B- [0 E! W
System.out.print("请输入选项1~6:");  + g0 ^, H/ h9 A# q* J
value=select();  / ~1 c+ n  A8 ^/ V6 Z
}  
6 u# I/ _$ @: o8 P; o- wif(value>6||value<1)//if exceed then print a message and reenter  : c3 ^5 @  w# }* i# ?$ {) W, ~! A6 s
{  
( M% q; _! c% M1 ^3 ^  o* b/ k2 q9 ESystem.out.print("请输入选项1~6:");  
- H9 q& k) R2 y2 `- ivalue=select();  ! Z1 }' v. R: r- Y8 n1 y4 E7 w
}  
# L& R& b- p% u3 ~2 {3 Qreturn value;//return a value  
% h2 |6 `1 j' `/ i# t: y4 r; }}  
- c8 ^- ?$ s5 n
8 d+ E6 p  D' S2 Mpublic int getid(int count)throws IOException//a method of return the number of data  
8 g: f% b2 D& @; r# A{  
0 B1 p( e# m2 t- Q% nString input;  ( [# D9 E+ }% r8 s
int value=0;  ) k' a& ^( w1 S0 o. e" g
input=bufin.readLine();//read a user input string from keyboard  
/ `# d' I/ p+ v& `4 ztry  + O3 F( O8 K/ c' J# Q8 V
{  
* }9 `) F9 N( Z5 x: Y3 {5 @! Fvalue=Integer.parseInt(input);//convert the string to a int  7 r9 Z% u2 h" A0 b
}catch(NumberFormatException e)//if can’t convert to a integer then reenter  
! O2 y2 }8 [4 E{  % \3 Y/ ^9 S  J- U+ R4 v6 ?
System.out.print("请输入数据编号:");  
% e% p( H7 s0 u1 Z: b$ H! `value=getid(count);  
3 c) G2 V  ]8 V$ C4 S}  2 q1 r5 ~: b0 r
if(value>count)//the input value is out of bound  
* a8 U, b1 e' O9 s5 N% Q6 P{  1 z- M) B1 `$ D3 g  D
System.out.print("无此编号的数据,请重新输入:");  
! y  r+ S9 w6 Z# Vgetid(count);    ^( p1 L! S$ r# N# v
}  
" m3 i# x( l" v' G9 P- X2 kreturn value;//return a value  
1 h, f+ o( l2 q& l. |& [# b$ ^4 A}  
3 B2 x! U; s1 ~# L6 y& V$ o}
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 20:55 , Processed in 0.399856 second(s), 52 queries .

回顶部