import Java.io.*;
import java.util.*;
class str14
{
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader bufin=new BufferedReader(stdin);
public static void main(String args[]) throws IOException
{
new str14();//run the application
}
public str14() throws IOException//constrUCtor
{
Hashtable htable=new Hashtable(20,0.75F);
dataBase(htable);
}
public str14(Hashtable htable) throws IOException //override the constructor
{
dataBase(htable);
}
public void dataBase(Hashtable htable) throws IOException
{
int count=htable.size();//get the ammount of the data in htable
int value,id,num;
String key,enter,data;
Enumeration keys=htable.keys();//get all the key in htable
Enumeration elements=htable.elements();//get all the elements in htable
System.out.println("
Hashtable 简易数据库程序
");
System.out.println("(1)输入数据");
System.out.println("(2)请除所有数据");
System.out.println("(3)显示单笔数据");
System.out.println("(4)删除单笔数据");
System.out.println("(5)显示所有数据");
System.out.println("(6)结束程序");
System.out.print("请输入您的选择:");
value=select();//call select(),return the function number
switch(value)//the function
{
case 1:
System.out.print("
请输入一笔数据:");//need data input
data=bufin.readLine();
count++;
key=String.valueOf(count);
htable.put(key,data);//store it to the htable
System.out.print("
输入完成,按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 2://clear all data from the htable
htable.clear();
System.out.print("
已删除了所有数据... 按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 3:
System.out.print("
请输入要显示的数据编号:");
id=getid(count);//call getid()
key=String.valueOf(id);
Object select=htable.get(key);//fetch the data from the htable
data=select.toString();
System.out.print("
编号"+" "+"内容");
System.out.println("
"+key+" "+data);//display the data
System.out.print("
按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 4:
System.out.print("请输入要删除的数据编号:");
id=getid(count);
key=String.valueOf(id);
htable.remove(key);//remove data
count--;
num=count;
System.out.print("已删除了所选择的数据...按任意见继续...");
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
elements=htable.elements();//fetch all the data from htable
while(elements.hasMoreElements())
{
key=String.valueOf(num);//a new value
data=(String)elements.nextElement();//fetch content of the data
htable1.put(key,data);//store it to htable1
num--;
}
htable.clear();
enter=bufin.readLine();
new str14(htable1);
break;
case 5:
String[] sortkey=new String[count];//create a new sort array
String[] sortdata=new String[count];
num=count;
elements=htable.elements();
keys=htable.keys();
while(elements.hasMoreElements())
{
key=(String)keys.nextElement();//fetch value of key
data=(String)elements.nextElement();//fetch value of data
sortkey[num-1]=key;//store the value of key to sortkey array
sortdata[num-1]=data;//store the value of data to sortdata array
num--;
}
System.out.println("
编号"+" "+"内容");
for(int i=0;i<count;i++)//display all content after sorting
System.out.println(" "+sortkey[i]+" "+sortdata[i]);
System.out.print("
目前共有"+count+"笔数据");
System.out.print("
按任意键继续...");
enter=bufin.readLine();
new str14(htable);
break;
default:
}
}
public int select() throws IOException//method of getting a function selector
{
String input;
int value=0;
input=bufin.readLine();//read a input from keyboard
try
{
value=Integer.parseInt(input);//convert a string to a int value
}catch(NumberFormatException e)//can’t vonverted
{
System.out.print("请输入选项1~6:");
value=select();
}
if(value>6value<1)//if exceed then print a message and reenter
{
System.out.print("请输入选项1~6:");
value=select();
}
return value;//return a value
}
public int getid(int count)throws IOException//a method of return the number of data
{
String input;
int value=0;
input=bufin.readLine();//read a user input string from keyboard
try
{
value=Integer.parseInt(input);//convert the string to a int
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
{
System.out.print("请输入数据编号:");
value=getid(count);
}
if(value>count)//the input value is out of bound
{
System.out.print("无此编号的数据,请重新输入:");
getid(count);
}
return value;//return a value
}
}