📄 employeesystem.java
字号:
import java.io.*;
import java.util.*;
class EmployeeSystem
{
static boolean isDelete = true;
static boolean isFind = true;
public static void main(String [] args)
throws IOException,NumberNotFoundException
{
int choice=-1;
do{
EmployeeSystem lp = new EmployeeSystem();
System.out.println();
System.out.println("\t####################################");
System.out.println();
System.out.println("\t\t employee system");
System.out.println("\t####################################\n");
System.out.print("1.Add one employee\n"+ "2.Inquiry one employee\n"+ "3.Delete one employee\n"+ "4.About writer\n"+
"5.Print to screen\n"+"6.Change data\n"+ "7.Count all \n"+ "8.Clear all imformation\n"+ "9.Tuichu\n");
System.out.print("Choose which one you want to do:");
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String inputLine = in.readLine();
choice= Integer.valueOf(inputLine).intValue();
switch(choice)
{
case 1: {
String str = lp.inputData();
lp.addData(str);
System.out.println("Add success!");
timeOut(1);
}break;
case 2: {
long find = 0;
System.out.print("Please input the ID of the employee which you want to find:");
BufferedReader inn = new BufferedReader(
new InputStreamReader(System.in));
String inputLi = inn.readLine();
find = Integer.valueOf(inputLi).longValue();
lp.findData(find);
timeOut(2);
}break;
case 3: {
long deleteNumber = 0;
System.out.print("Please input the ID of the employee which you want to find:");
BufferedReader bf =
new BufferedReader (
new InputStreamReader(System.in));
String inputL = bf.readLine();
deleteNumber = Integer.valueOf(inputL).longValue();
lp.deleteData(deleteNumber);
if(isDelete)
System.out.println("Delete success!");
timeOut(1);
}break;
case 4: {
System.out.print("\t\t\n"+ "\t\t040421205 geguangzuo\n"+
"\t\tQQ:398413291\n");
timeOut(4);
}break;
case 5: {
print();
timeOut(2);
}break;
case 6: {
lp.rewrite();
timeOut(2);
}break;
case 7: {
int count = lp.count();
System.out.println("There are "+count+" employees");
timeOut(2);
}break;
case 8: {
lp.clearData();
timeOut(1);
}break;
case 9: {
System.out.println("exit!");
System.exit(0);
}break;
}
}while (choice != 0);
}
public String inputData()
throws IOException,NumberFormatException
{
System.out.print("Input the imformation of the employee: ID Name Sex Saloary\n" +
"divided by Kongge:");
String all = "";
try{
BufferedReader in =
new BufferedReader (new InputStreamReader(System.in));
String inputLine = in.readLine();
StringTokenizer str =
new StringTokenizer(inputLine," ");
long ID = Integer.valueOf(str.nextToken()).longValue();
String name = (String)str.nextToken();
String sex = (String)str.nextToken();
double salary = Integer.valueOf(str.nextToken()).doubleValue();
all = String.valueOf(ID) +" , "+ name +" , "+ sex +" , "+ String.valueOf(salary);
}catch (IOException e){}
catch (NumberFormatException e){}
return all;
}
public void addData(String str)
throws IOException
{
String s1 ="",s2="" ,s3= "";
File file = new File("imformation.txt");
if (file.exists())
{
try{
BufferedReader in =
new BufferedReader(
new FileReader("imformation.txt"));
while ((s1=in.readLine())!=null)
s2+=s1+"\n";
s2+=str+"\n";
BufferedReader in2 =
new BufferedReader(
new StringReader(s2));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("imformation.txt")));
while ((s3=in2.readLine())!= null)
{
out.println(s3);
}
out.close();
}catch (IOException e){}
}
else{
System.err.println("File \"data\" Missing!");
}
}
public void deleteData(long deleteNumber)
throws IOException,FileNotFoundException
{
isDelete = true;
try{
DataMap mp = new DataMap();
long j=0;
String s1="",s2="",s3="";
BufferedReader in =
new BufferedReader(
new FileReader("imformation.txt"));
while ((s1=in.readLine())!=null)
{
j=numberTokenizer(s1);
mp.put(j,s1);
}
try{
if(mp.containsKey( String.valueOf(deleteNumber).toString()))
{
mp.remove(deleteNumber);
}
else throw new NumberNotFoundException();
Collection c = mp.values();
Iterator iter = c.iterator();
while(iter.hasNext())
{
s1 = (String)iter.next();
s3 +=s1+"\n";
}
BufferedReader in2 =
new BufferedReader(
new StringReader(s3));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("imformation.txt")));
while( (s1 = in2.readLine())!=null)
{
out.println(s1);
}
out.close();
}catch (NumberNotFoundException e)
{
isDelete = false;
System.out.println(deleteNumber+" no found :(");
}
}catch(IOException e){}
}
public void clearData()
throws IOException
{
File file = new File("imformation.txt");
if(file.exists())
{
try{
PrintWriter out =
new PrintWriter( new BufferedWriter( new FileWriter(file)));
out.print("");
out.close();
System.out.println("clear all!");
}catch(IOException e){}
}
else{
System.err.println("File \"data\" Missing!");
}
}
public long numberTokenizer(String s)
throws IOException
{
StringTokenizer st =
new StringTokenizer(s," ");
return Integer.valueOf((st.nextToken())).longValue();
}
public static void print()
throws IOException
{
try{
BufferedReader in =
new BufferedReader( new FileReader("imformation.txt"));
String read = "";
while ((read = in.readLine())!=null)
System.out.println(read);
}catch(IOException e){}
}
public static void timeOut(double sec)
{
double seconds = sec;
long t = System.currentTimeMillis()+(int)(seconds*1000);
while ((System.currentTimeMillis())<t);
}
public void findData(long find)//
throws IOException,NumberNotFoundException
{
isFind = true;
String s = "",findString ="";
long i;
DataMap dm = new DataMap();
BufferedReader in =
new BufferedReader(
new FileReader("imformation.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
try{
if(dm.containsKey( String.valueOf(find).toString()))
{
findString = dm.get(find);
System.out.println("ID"+find+"The imformation of the employee:");
System.out.println(findString);
}else
throw new NumberNotFoundException();
}catch (NumberNotFoundException e){
System.out.println(find+" no found :(");
isFind = false;
}
}
public void rewrite()
throws IOException,NumberNotFoundException
{
try{
System.out.print("Input the ID of the employee which you want to change:");
BufferedReader in = new BufferedReader ( new InputStreamReader(System.in));
String inputLine = in.readLine();
long ID = Integer.valueOf(inputLine).longValue();
findData(ID);
if(isFind)
{
deleteData(ID);
System.out.print("Input the new imformation of the employee which you want to change:");
String str = inputData();
addData(str);
System.out.println("rewrite true!");
}
}catch(IOException e){}
catch(NumberNotFoundException e){}
}
public int count()
throws IOException
{
DataArrayList dal = new DataArrayList();
try{
String s = "";
long i =0;
BufferedReader in = new BufferedReader( new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
}catch(IOException e){}
return dal.size();
}
}
class DataMap extends HashMap
{
public void put(long i,String str)
{
put(String.valueOf(i).toString(),str);
}
public void remove(long i)
{
remove(String.valueOf(i).toString().toString());
}
public String get(long i)
{
String s = String.valueOf(i).toString();
if (!containsKey(s))
{
System.err.println("Not found Key: "+s);
}
return (String)get(s);
}
}
class DataArrayList extends ArrayList
{
public void add(long num)
{
String numToString = String.valueOf(num).toString();
add(numToString);
}
}
class NumberNotFoundException extends Exception
{
public NumberNotFoundException() {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -