数学建模社区-数学中国
标题:
[转帖]Hashtable简易数据库程序
[打印本页]
作者:
god
时间:
2005-3-31 01:45
标题:
[转帖]Hashtable简易数据库程序
import java.io.*;
2 y. U) I& A- \: u! y3 b
import java.util.*;
* I1 @& T1 v* C+ x) e
class str14
# m8 x$ z. K& ^4 s
{
, ~0 h2 n8 P: b: W. a" h
InputStreamReader stdin=new InputStreamReader(System.in);
# y. a8 P2 E q2 B. X2 [& p$ G
BufferedReader bufin=new BufferedReader(stdin);
6 [2 n3 u) |# O5 U
9 l: u* j# G9 f
public static void main(String args[]) throws IOException
6 S! K, W' _; U3 W, R% F/ C. Z5 c
{
2 R1 l2 Q* n0 q S* P
new str14();//run the application
0 t k4 |4 J9 y
}
. z; f' k! n+ A! J2 N
2 ?: ~0 V5 K' o5 d1 w# `
public str14() throws IOException//constructor
* M, {0 h# r; n; J
{
( z0 J; z6 \# S& O* Z. r
Hashtable htable=new Hashtable(20,0.75F);
7 T, _. d5 x- m* a
dataBase(htable);
% Q" C0 y. ^) U5 O X
}
$ w, D- h8 ^/ n- s6 y; M V
; z# ?0 W' {3 W: E
public str14(Hashtable htable) throws IOException //override the constructor
- b/ k# ^2 Z0 E$ t6 i* t& R c& j9 E
{
/ t* [ v5 V# p0 c( P
dataBase(htable);
; _$ N1 K3 h- E+ W0 t/ ~7 S B6 u
}
$ Y4 m. K: V1 Z$ K& D
* H8 f) L2 T3 Q" C) }
public void dataBase(Hashtable htable) throws IOException
5 z$ T1 Y: G" H: D# T5 m
{
5 T2 m; ^6 S6 |; A: p" K! Y
int count=htable.size();//get the ammount of the data in htable
8 ~! |5 \) d, h* Q
int value,id,num;
0 v* b% I( r" T+ W N2 ]
String key,enter,data;
) K. r8 }6 v G. ^* h4 L
Enumeration keys=htable.keys();//get all the key in htable
9 w. ~3 `' _3 I# o5 |
Enumeration elements=htable.elements();//get all the elements in htable
* K& v o/ l' W+ Y, j3 b# n/ b
System.out.println("
: N B1 z7 s9 k/ z$ B T! P' }
Hashtable 简易数据库程序
3 l6 b& D, k; d! ]& y* U$ c
");
5 a7 ]' [/ ~9 `7 n1 |3 v) I% ~6 J
System.out.println("(1)输入数据");
6 m; e) c1 E0 E& W0 ]$ \
System.out.println("(2)请除所有数据");
- C6 J" u4 d1 M) j) Y6 |) Q; |
System.out.println("(3)显示单笔数据");
7 u1 r- l: L: C7 y3 l+ R
System.out.println("(4)删除单笔数据");
6 `4 `. u4 t: S" A$ n
System.out.println("(5)显示所有数据");
( O9 b9 F7 P! z: d& V
System.out.println("(6)结束程序");
! X: B1 c( N1 ^' L
System.out.print("请输入您的选择:");
( P, Y! Y7 g0 @
value=select();//call select(),return the function number
' G" v n! ?6 i' B0 z/ ]
switch(value)//the function
- G6 l3 K7 v3 l6 k) w% V$ a9 L T
{
: B4 K( @+ |6 Y7 F& i. u
case 1:
0 {5 i. ?" G3 W: o
System.out.print("
0 | N0 D/ M+ U' G0 f
请输入一笔数据:");//need data input
- I9 x8 @7 w! W5 ?
data=bufin.readLine();
+ J" t7 l" T; X1 j
count++;
5 ^ Z) N/ {; z" l, S
key=String.valueOf(count);
7 O' J) _. ?+ I1 g) `6 q
htable.put(key,data);//store it to the htable
: h2 T! `9 T4 u
System.out.print("
: [) R' Z B; l) w" K% {
输入完成,按任意键继续...");
, w' i; x. m- z7 r
enter=bufin.readLine();
: D! o- H# r% e& o& A0 w( A
new str14(htable);//reenter
, K% @! N! }9 P$ A9 n. a
break;
3 @% y V: \: Z
case 2://clear all data from the htable
) r: S5 }% f1 O* z# ^
htable.clear();
/ _1 a; T# y( p5 `
System.out.print("
0 S- S; ~" G* H1 A4 k
已删除了所有数据... 按任意键继续...");
5 u# D$ U. v5 W, S; [
enter=bufin.readLine();
$ A9 N$ ~: q% i& R. V) A
new str14(htable);//reenter
1 A0 Y& K% J2 W1 f; r# s
break;
' e' X0 J8 M0 s5 G
case 3:
& E( o6 M2 y( u; L) a3 S
System.out.print("
2 l- @. j" I1 t
请输入要显示的数据编号:");
* s( o/ f6 o& W! b+ z) d% k/ r
id=getid(count);//call getid()
* J* n3 W8 G m- p7 M- }
key=String.valueOf(id);
& g' d4 {2 T+ {. [6 t+ r X
Object select=htable.get(key);//fetch the data from the htable
/ ?: ?6 |% K, B, w
data=select.toString();
6 {0 [; F8 V9 }/ \- E9 p' z
System.out.print("
: B4 |1 }8 S# |# X7 k
编号"+" "+"内容");
5 f/ U1 b7 w" i, M7 M( Q% P
System.out.println("
! M9 c# o- r: L& P' Z
"+key+" "+data);//display the data
) U+ G9 s2 t! ]8 x) f3 V: B
System.out.print("
) N0 {" H1 i$ M" l
按任意键继续...");
) M8 l8 H$ h; G( P6 R) T) @* g2 E* C
enter=bufin.readLine();
, W! z0 n1 a: B
new str14(htable);//reenter
: g" s( {" @' `% z! W
break;
' D7 Y% E0 F( w( [& \
case 4:
' f3 [) G, g0 {, E% Q7 x( T
System.out.print("请输入要删除的数据编号:");
1 I! D- V' V6 ~# t+ ?: ?
id=getid(count);
2 G9 d0 l' G9 h) M& V4 x" d8 x
key=String.valueOf(id);
6 t" S. {. Y) e C- j) p
htable.remove(key);//remove data
( b" l3 R" F+ x& B& w8 V2 @( Z
count--;
, x1 j# ~2 M& t8 k, q$ D- c
num=count;
4 o' N$ {: I$ P
System.out.print("已删除了所选择的数据...按任意见继续...");
$ Z: Z/ \+ j; h8 G- o, M! E
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
) d& t8 W c- J; ?7 W! Y4 z
elements=htable.elements();//fetch all the data from htable
, m8 C% \- ]+ _. W
while(elements.hasMoreElements())
- O( R8 k3 Q1 l/ z
{
8 x. G) {$ B; G* n, m/ W$ k# R
key=String.valueOf(num);//a new value
; q; h" I( m! o' Y% @
data=(String)elements.nextElement();//fetch content of the data
# e# o: o H& \: M1 F! ~- N* a
htable1.put(key,data);//store it to htable1
; r8 X+ i1 k: X+ B
num--;
* | j. P0 `7 w5 x9 |
}
( {# C! C" A3 `; C
htable.clear();
2 n$ `3 {- y( r
enter=bufin.readLine();
$ T6 }5 Z7 t9 L& }7 q& c
new str14(htable1);
) d7 O6 I5 ^9 a8 S y! @" v
break;
1 J% F; ?. J- C. C6 ^6 n g7 R
case 5:
" Z/ s$ W6 h" X: C
String[] sortkey=new String[count];//create a new sort array
* w* p& F( O: B, t
String[] sortdata=new String[count];
( o" Y6 E/ q) f' f* X% u6 o
num=count;
+ t8 L5 Z# X) `1 @1 A
elements=htable.elements();
& _7 p% Q9 v& K+ U* c) l$ \' {
keys=htable.keys();
) p5 {' [& ]) F8 i/ C# Z
while(elements.hasMoreElements())
& z% S. }# d+ [( Y9 C
{
2 ^& \# A! i7 ?! f. C, H
key=(String)keys.nextElement();//fetch value of key
5 Y: |$ G- k e( Q1 M
data=(String)elements.nextElement();//fetch value of data
; Q/ @0 v/ w ~. K6 q O0 O
sortkey[num-1]=key;//store the value of key to sortkey array
9 i8 t, }5 [7 n# b
sortdata[num-1]=data;//store the value of data to sortdata array
: T1 b# V& O5 ?. u" S! h- r, S
num--;
u; y, E3 b, ^0 C! Q) U
}
/ p8 W `& J0 O9 k9 v
System.out.println("
# v1 G3 q4 \2 m% \; F! L6 W* C
编号"+" "+"内容");
- K& }" U) U( g0 a2 i
for(int i=0;i<count;i++)//display all content after sorting
. f( m) t1 X8 O4 F7 L) i) A
System.out.println(" "+sortkey
+" "+sortdata
);
% U6 k) S4 V+ u8 F8 f
System.out.print("
# J( V2 b! l3 u; d& ~4 ]
目前共有"+count+"笔数据");
" C9 w) F; S% F! o7 H
System.out.print("
: Y; Z( v' ?" y
" y# {" B7 o: @) w4 w! O
按任意键继续...");
2 n: w- U, `$ v
enter=bufin.readLine();
4 x8 x8 a& I( m
new str14(htable);
% _ F% k" V% \; B
break;
1 R. S! W* l a/ t6 S8 ~
default:
9 l( W$ J6 O5 @, d+ H Z
}
. [% Y( B2 h: _& U
}
0 G6 O( p& `9 n/ e9 }, `
) a, ^& J% R; Q5 ~+ @2 L4 F5 [! Z
public int select() throws IOException//method of getting a function selector
# D! l6 r; [2 w l
{
0 S8 K/ E6 ?$ P0 k& }' P. n) w
String input;
- _- R" C$ n# ]4 t3 ~! W
int value=0;
. { G4 {" C6 t( ~8 B* n
input=bufin.readLine();//read a input from keyboard
' L+ |" t; E& ] u" }# }* j5 g
try
' c) k: C( ]) V" l2 J f/ b& k' P( W
{
( j l+ @2 C' t0 i
value=Integer.parseInt(input);//convert a string to a int value
. ^9 g) f3 M' m* i- |- I- j5 g
}catch(NumberFormatException e)//can’t vonverted
& o; }+ k d- i& q! }; _; @9 O
{
% p2 E" V& ?$ [+ V
System.out.print("请输入选项1~6:");
4 m4 N3 F; H9 |7 k" ]* Q8 q
value=select();
6 n8 Q: L' t5 h" _5 N
}
1 X0 S H2 E, g" g& q4 J
if(value>6||value<1)//if exceed then print a message and reenter
E, P; Y6 B9 X1 ^$ }3 U& L8 \
{
n$ ?# O0 Z$ T4 @$ }* t* ?
System.out.print("请输入选项1~6:");
1 c0 Z& c/ H# K& p5 C4 u7 R
value=select();
% l7 c! ^* s/ o
}
3 k+ u& M% k! ?3 |; [) i. Q
return value;//return a value
* \$ A# P5 m7 k
}
) z: k9 x! a1 d& V
3 {& @+ Y f( _
public int getid(int count)throws IOException//a method of return the number of data
* i7 m" _3 d3 a
{
$ p% A2 I k* }6 l/ P
String input;
7 w q3 u( z: }4 Z$ K" y
int value=0;
; b4 x' J6 Y. L9 e7 [1 i# W
input=bufin.readLine();//read a user input string from keyboard
6 J8 z. E3 s$ J6 r& Z
try
: Q* F% [" u0 p q
{
6 F8 q" T; ^3 u# k5 d( B
value=Integer.parseInt(input);//convert the string to a int
) g$ C4 K- J: c3 a9 W$ x
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
3 Y7 e) o7 h+ A6 b+ m
{
, t) N1 N6 m! T Y/ }# f2 @
System.out.print("请输入数据编号:");
) B$ m2 g* k& ?( J
value=getid(count);
6 B& g4 S/ E' o |" B, N
}
6 S( {; B' t a6 r
if(value>count)//the input value is out of bound
8 {/ m0 c" J/ u, b+ S* x; O4 D
{
. Y3 @ q& S: C& ~* k$ u! D7 J" z
System.out.print("无此编号的数据,请重新输入:");
. i+ x% J3 P6 I; z7 y* J
getid(count);
! [2 b o$ t+ O% W
}
1 g/ u K2 g/ c! l: F3 u" i0 Z( C: e
return value;//return a value
/ v$ ~1 _! B! ]1 Q6 c8 p( Y e' {
}
! G5 a" q% e9 [' I5 O9 K
}
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5