📄 work.java
字号:
import java.lang.*;
import java.io.*;
import java.util.*;
//surname,given_name,email,date_of_birth,telephone
public class work
{
public static Hashtable command = new Hashtable();
public static Hashtable studTable = new Hashtable();
public static Hashtable empTable = new Hashtable();
public static Hashtable subjectTable = new Hashtable();
public static void main(String[] args)
{
if(!processData())
{
System.out.println("create File failed!");
return;
}
work oWork = new work();
String ans = "";
while(!ans.equals("q"))
{
System.out.println("#");
try{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
ans = trim(br1.readLine());
}catch(IOException ex)
{
System.out.println(ex.toString() );
}
int CMD = getCommIndex(oWork.getCommand(ans));
switch(CMD)
{
case 0:oWork.addStudent(ans);
break;
case 1:oWork.addEmployee(ans);
break;
case 2: oWork.editStudent(ans);
break;
case 3: oWork.editEmployee(ans);
break;
case 4:oWork.del(ans);
break;
case 5:oWork.delAll(ans);
break;
case 6:oWork.listbyID(ans);
break;
case 7: oWork.listStaff(ans);
break;
case 8: oWork.listAcad(ans);
break;
case 9:oWork.listStud(ans);
break;
case 10:oWork.listAll(ans);
break;
case 11:oWork.save();
break;
case 12:oWork.create_subject(ans);
break;
case 13:oWork.enrol(ans);
break;
case 14:oWork.unenrol(ans);
break;
case 15:oWork.assign(ans);
break;
case 16:oWork.unassign(ans);
break;
case 17:oWork.set_prereq(ans);
break;
case 18:oWork.get_prereqs(ans);
break;
case 19:oWork.count_staff();
break;
case 20:oWork.count_acad();
break;
case 21:oWork.count_student();
break;
case 22:oWork.count_employee();
break;
case 23:oWork.count_all();
break;
case 24:
break;
default:
System.out.println("bad command for " +ans);
}
}
}
/*
> COUNT_STAFF
> COUNT_ACADEMICS
> COUNT_STUDENTS
> COUNT_EMPLOYEES
> COUNT_ALL
CREATE_SUBJECT subject_code,subject_title
ENROL student_id:subject_code
UNENROL student_id:subject_code
ASSIGN academic_id:subject_code
UNASSIGN academic_id:subject_code
SET_PREREQ subject_code:subject_code
GET_PREREQS subject_code
*/
//add a student
public void addStudent(String strInfo)
{
String strTemp = strInfo;
int pos = strTemp.indexOf(" ");
strTemp = trim(strTemp.substring(pos+1,strTemp.length()));
StringTokenizer st = new StringTokenizer(strTemp,",");
if(st.countTokens()<6)
{
System.out.println("bad input student information! ");
System.out.println(" -- ADD_STUDENT idnum,surname,given_name,email,date_of_birth,telephone");
return;
}
String studNum = st.nextToken();
if(!studNum.startsWith("stud"))
{
System.out.println("error input student id! please input start with \"stud\" ") ;
return;
}
studInfo si = new studInfo();
si.setSurname(st.nextToken());
si.setGiven_name( st.nextToken());
si.setEmail( st.nextToken());
si.setDate_of_birth ( st.nextToken());
si.setTelephone( st.nextToken());
si.setSubject_code("");
studTable.put(studNum,si);
}
// add a employee;
public void addEmployee(String strInfo)
{
String strTemp = strInfo;
int pos = strTemp.indexOf(" ");
strTemp = trim(strTemp.substring(pos+1,strTemp.length()));
StringTokenizer st = new StringTokenizer(strTemp,",");
if(st.countTokens()<7)
{
System.out.println("bad input student information! ");
return;
}
String empNum = st.nextToken();
if(!empNum.startsWith("acad")&&!empNum.startsWith("staff"))
{
System.out.println("error input employee id!" ) ;
return;
}
empInfo info = new empInfo();
if(empNum.startsWith("acad"))
{
info = new acadInfo();
}
info.setSurname(st.nextToken());
info.setGiven_name( st.nextToken());
info.setEmail( st.nextToken());
info.setDate_of_birth ( st.nextToken());
info.setTelephone( st.nextToken());
info.setSalary( st.nextToken());
empTable.put(empNum,info);
}
//Edit a student information;
public void editStudent(String strInfo)
{
String strTemp = strInfo;
int pos = strTemp.indexOf(" ");
strTemp = trim(strTemp.substring(pos+1,strTemp.length()));
int subPos = strTemp.indexOf(":");
String studNum = strTemp.substring(0,subPos);
String strContent = this.trim(strTemp.substring(subPos+1,strTemp.length() ));
StringTokenizer st = new StringTokenizer(strContent,",");
if(st.countTokens()<5)
{
System.out.println("bad input student information! ");
return;
}
//String studNum = st.nextToken();
if(!studNum.startsWith("stud"))
{
System.out.println("error input student id! please input start with \"stud\" ") ;
return;
}
if(studTable.size()==0)
{
System.out.println("no records!");
return;
}
studInfo si =(studInfo) studTable.get(studNum);
if(si!=null)
{
si.surname = st.nextToken();
si.given_name = st.nextToken();
si.email = st.nextToken();
si.date_of_birth = st.nextToken();
si.telephone = st.nextToken();
}else
{
System.out.println("No record match the student id number:"+studNum);
return;
}
}
// edit employee
public void editEmployee(String strInfo)
{
String strTemp = strInfo;
int pos = strTemp.indexOf(" ");
strTemp = trim(strTemp.substring(pos+1,strTemp.length()));
int subPos = strTemp.indexOf(":");
String studNum = strTemp.substring(0,subPos);
String strContent = this.trim(strTemp.substring(subPos+1,strTemp.length() ));
StringTokenizer st = new StringTokenizer(strContent,",");
if(st.countTokens()<6)
{
System.out.println("bad input student information! ");
return;
}
if(!studNum.startsWith("acad")&&!studNum.startsWith("staff"))
{
System.out.println("error input employee id") ;
return;
}
if(empTable.size()==0)
{
System.out.println("no records!");
return;
}
empInfo ei =(empInfo) studTable.get(studNum);
if(ei!=null)
{
ei.surname = st.nextToken();
ei.given_name = st.nextToken();
ei.email = st.nextToken();
ei.date_of_birth = st.nextToken();
ei.telephone = st.nextToken();
ei.salary = st.nextToken();
}else
{
System.out.println("No record match the student id number:"+studNum);
return;
}
}
//save Hashtable to persist data
public void save()
{
if(!saveTableToFile("studInfo",work.studTable )) return;
if(!saveTableToFile("empInfo",work.empTable )) return;
if(!saveTableToFile("subjectInfo",work.subjectTable )) return;
}
// save hashtable to persist data file
private boolean saveTableToFile(String file_name,Hashtable table)
{
if(table!=null)
{
System.out.println("please given hashtable!");
return false;
}
if(file_name.equals("")||file_name==null)
{
System.out.println("please input filename !");
return false;
}
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file_name,false));
out.writeObject(table);
out.flush();
out.close();
}catch(Exception ex)
{
System.out.println("write file err: "+ ex +"\n\r please check filename!");
return false;
}
return true;
}
//del
public void del(String strInfo)
{
String strTemp = strInfo;
int pos = strTemp.indexOf(" ");
strTemp = trim(strTemp.substring(pos+1,strTemp.length()));
if(strTemp.equals("")||strTemp==null)
{
System.out.println("please input id number! ");
return;
}
String studNum = strTemp;
/* if(studTable.size()==0)
{
System.out.println("no records!");
return;
}
*/
if(studTable.containsKey(studNum))
{
studTable.remove(studNum);
}else if(empTable.containsKey(studNum))
{
empTable.remove(studNum);
}else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -