搜档网
当前位置:搜档网 › 小型人事管理系统(java)

小型人事管理系统(java)

import java.util.*;
import java.io.*;

//员工类
class Employee implements Serializable{
long num; //员工编号
String name; //员工姓名
String depart; //员工所属部门
int grade; //员工职位
String grades;
float pay; //员工工资
int worktime; //员工工龄

public void enterContinue() {
System.out.println("\n按回车键继续");
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {System.out.println(e);}
}

public void showOneInfo(Employee e,ArrayList a) { //显示单个员工信息
Scanner scan = new Scanner(System.in);
if(a.size()!=0) {
System.out.println("现有员工"+a.size()+"人");
}else{
System.out.println("档案中无信息,请添加!\n");
}
boolean flag=true;
while(flag && a.size() != 0) {
int enumber;
System.out.println("请输入你要查看的员工编号(输入0返回):");
try{enumber = scan.nextInt();
}catch(InputMismatchException input) {System.out.println("输入无效!");break;}
if (enumber == 0) {break;}
int i = 0;
while (i != a.size()) {
e = a.get(i);
if(e.num == enumber){
System.out.println("你要查看的员工信息如下:\n");
System.out.println("编号:"+e.num+
"\t姓名:"+https://www.sodocs.net/doc/b812005576.html,+
"\t部门:"+e.depart+
"\t职务:"+e.grades+
"\t工资:"+e.pay+
"\t工龄:"+e.worktime);
e.enterContinue();
flag = false;
break;
}
else i++;
if(i == a.size()){System.out.println("你所查询的员工不存在,请重新输入\n");}
}
}

}

public void showAllInfo(Employee e,ArrayList a) { //显示所有员工信息
if(a.size()!=0) {
System.out.println("现有员工"+a.size()+"人,信息如下:");
}else{
System.out.println("档案中无信息,请添加!\n");
}
for(int i=0;ie = a.get(i);
System.out.println("编号:"+e.num+
"\t姓名:"+https://www.sodocs.net/doc/b812005576.html,+
"\t部门:"+e.depart+
"\t职务:"+e.grades+
"\t工资:"+e.pay+
"\t工龄:"+e.worktime);
if((i+1) % 16 ==0){
System.out.println("\n按回车显示下一页...");
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException ioe) {System.out.println(ioe);}
}
}
System.out.println("\n按回车返回到主菜单...");
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException ioe) {Syste

m.out.println(ioe);}
}

public void newInfo(Employee e,ArrayList a) { //添加员工信息
Scanner scan = new Scanner(System.in);
System.out.println("请输入新员工编号:");
while(true){
try{
this.num = scan.nextLong();
boolean noExistNum = true;
for(int i =0;iif(e.num==a.get(i).num) {
System.out.println("添加失败!你输入的新编号已重复,请重新添加!\n");
noExistNum = false;
break;
}
}
if(noExistNum == false) break;
if(this.num<1){
System.out.println("输入编号无效!返回主菜单...");
break;
}
}catch(InputMismatchException input) {
System.out.println("输入非法!返回主菜单...");
break;
}

System.out.println("请输入新员工姓名:");
https://www.sodocs.net/doc/b812005576.html, = scan.next();
System.out.println("请输入新员工所属部门:");
this.depart = scan.next();
try{
while (true) {
System.out.println("请输入新员工的职务序号(1->职工;2->主任;3->经理;4->董事长):");
String st = scan.next();
byte[] by = st.getBytes();
this.grade = by[0]-48;
switch(this.grade) {
case 1:this.grades = "职工";break;
case 2:this.grades = "主任";break;
case 3:this.grades = "经理";break;
case 4:this.grades = "董事长";break;
default:System.out.println("对不起没有该职务!");
}
if(this.grades != null) {break;}
}
}
catch(InputMismatchException input) {
System.out.println("输入非法!返回主菜单...");
break;
}
System.out.println("请输入新员工的工龄:");
try{
this.worktime = scan.nextInt();
if(this.worktime<1){
System.out.println("输入编号无效!返回主菜单...");
break;
}
}
catch(InputMismatchException input) {
System.out.println("输入非法!返回主菜单...");
break;
}
this.pay = 1000+(this.grade * this.grade)*400+this.worktime*300;
break;
}

if(e.num>0 && e.worktime>0) {
a.add(e);
System.out.println("添加成功!");
}

}

public void refreshInfo(Employee e,ArrayList a) { //更新员工信息
Scanner scan = new Scanner(System.in);
boolean canchange = true;
boolean flag = true;
while(canchange && a.size() != 0) {
if(a.size()==0){System.out.println("档案中无信息,请添加!\n");break;}
System.out.println("请输入你要修改的员工编号(输入0返回):");
int enu

mber;
try{enumber = scan.nextInt();
}catch(InputMismatchException input) {System.out.println("输入无效!");break;}
if (enumber == 0) {
canchange = false;
break;
}
int i = 0;
while (i != a.size()) {
e = a.get(i);
if(e.num == enumber){
System.out.println("你要修改的员工信息如下,确定要修改么?(1->确定,0->取消)");
System.out.println("编号:"+e.num+
"\t姓名:"+https://www.sodocs.net/doc/b812005576.html,+
"\t部门:"+e.depart+
"\t职务:"+e.grades+
"\t工资:"+e.pay+
"\t工龄:"+e.worktime);
String sti = scan.next();
byte[] byt = sti.getBytes();
int ifok = byt[0]-48;
if(ifok == 0) {
break;
} else if(ifok == 1) {
System.out.println("请输入修改后员工姓名:");
https://www.sodocs.net/doc/b812005576.html, = scan.next();
System.out.println("请输入修改后员工所属部门:");
this.depart = scan.next();
try{
while (true) {
System.out.println("请输入修改后员工职务序号(1->职工;2->主任;3->经理;4->董事长):");
sti = scan.next();
byt = sti.getBytes();
this.grade = byt[0]-48;

String oldgrades = this.grades;

switch(this.grade) {
case 1:this.grades = "职工";break;
case 2:this.grades = "主任";break;
case 3:this.grades = "经理";break;
case 4:this.grades = "董事长";break;
default:System.out.println("对不起没有该职务!");break;
}
if(this.grade>0 &&this.grade<5) {break;}
}
}
catch(InputMismatchException input) {
System.out.println("输入非法!返回主菜单...");
break;
}
System.out.println("请输入修改后员工的工龄:");
try{
this.worktime = scan.nextInt();
if(this.worktime<1){
System.out.println("输入编号无效!返回主菜单...");
break;
}
}
catch(InputMismatchException input) {
System.out.println("输入非法!返回主菜单...");
break;
}
this.pay = 1000+(this.grade * this.grade)*400+this.worktime*300;
canchange = true;

if(canchange) { e=a.set(i,e);System.out.println("修改成功!");
canchange = false;
break;
} else {
System.out.print

ln("修改失败!");
break;
}
}
break;
}
else i++;
if(i == a.size()){System.out.println("所要修改的员工不存在,请重新输入\n");}
}

}

}

public void deleteInfo(Employee e,ArrayList a){
boolean flag=true;
while(flag && a.size() != 0) {
if(a.size()==0){System.out.println("档案中无信息,请添加!\n");break;}
System.out.println("请输入你要删除的员工编号(输入0返回):");
Scanner scan = new Scanner(System.in);
int enumber = scan.nextInt();
if (enumber == 0) {break;}
int i = 0;
while (i != a.size()) {
e = a.get(i);
if(e.num == enumber){
System.out.println("你要删除的员工信息如下,确定要删除么?(1->确定,0->取消)");
System.out.println("编号:"+e.num+
"\t姓名:"+https://www.sodocs.net/doc/b812005576.html,+
"\t部门:"+e.depart+
"\t职务:"+e.grades+
"\t工资:"+e.pay+
"\t工龄:"+e.worktime);
String sti = scan.next();
byte[] byt = sti.getBytes();
int ifok = byt[0]-48;
if(ifok ==0) {
System.out.println("取消删除操作,返回...");
break;
}
else if(ifok == 1) {
a.remove(e);
flag = false;
System.out.println("删除成功!");
}
break;
}
else i++;
if(i == a.size()){System.out.println("你要删除的员工不存在,请重新输入\n");}
}
}
}

public void numSort(ArrayList a) {
if(a.size()>=1) {
Comparator comp = new NumComparator();
Collections.sort(a,comp);
System.out.println("编号由小到大排序成功! 请输入'2'查看结果!");
} else{
System.out.println("排序失败,档案中没有员工信息!");
}
}

public void gradeSort(ArrayList a) {
if(a.size()>=1) {
Comparator comp = new GradeComparator();
Collections.sort(a,comp);
System.out.println("职务高低排序成功! 请输入'2'查看结果!");
} else{
System.out.println("排序失败,档案中没有员工信息!");
}
}

public void paySort(ArrayList a) {
if(a.size()>=1) {
Comparator comp = new PayComparator();
Collections.sort(a,comp);
System.out.println("工资由低到高排序成功! 请输入'2'查看结果!");
} else{
System.out.println("排序失败,档案中没有员工信息!");
}
}

class NumComparator implements Comparator{ //对员工编号进行比较的类

public int compare(Object o1,Object o2) {

Employee e1=(Employee)o1;
Employee e2=(Employee)o2;
if(e1.numreturn 0;
else
return 1;
}

}

class GradeComparator implements Comparator{ //对员工职位进行比较的类

public int compare(Object o1,Object o2) {
Employee e1=(Employee)o1;
Employee e2=(Employee)o2;
if(e1.grade>e2.grade)
return 0;
else
return 1;
}

}

class PayComparator implements Comparator{ //对员工工资进行比较的类

public int compare(Object o1,Object o2) {
Employee e1=(Employee)o1;
Employee e2=(Employee)o2;
if(e1.payreturn 0;
else
return 1;
}
}
}

//员工数据类
class EmployeeData {
public ArrayList loadEmployeeData() {
ArrayList a = new ArrayList();
try { //若有保存好的数据文件则写入到容器列表
//否则提示无信息,请添加
File f = new File("Employee_Data.dat");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("Employee_Data.dat"));
a=(ArrayList)in.readObject();
if(f.exists() && a.size()==0) {
System.out.println("档案中没有任何信息,请添加员工信息并正常保存退出!");
}
f.delete();
}catch (ClassNotFoundException exc) {
System.out.println(exc);
}
catch (FileNotFoundException fex) {
System.out.println("档案中没有任何信息,请添加员工信息并正常保存退出!");
}
catch (IOException ex) {
System.out.println(ex);
}
return a;
}

public void saveEmployeeData(ArrayList a) {
try { //将最终信息写入文件保存,供下次程序读取继续使用
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Employee_Data.dat"));
out.writeObject(a);
}
catch (IOException ex) {
System.out.println(ex);
}
}
}

//主方法类
public class EmployeeSystem {
public static void main(String args[]) { //主方法
Employee e = new Employee(); //建立一个员工对象
ArrayList a = new ArrayList(); //建立一个存放员工类的容器列表
EmployeeData ed = new EmployeeData();
int choice = 0; //表示用户的功能选择变量

a=ed.loadEmployeeData();

while (true) { //主循环体
System.out.println(" __________________________________\n"+
" | 小型人事信息管理系统 |\n"+
" |________________________________|\n"+
" | 1->查看单个员工信息 |\n"+

" | 2->查看所有员工信息 |\n"+
" | 3->添加新员工信息 |\n"+
" | 4->更新员工信息 |\n"+
" | 5->删除员工信息 |\n"+
" | 6->按编号大小对员工排序 |\n"+
" | 7->按职务高低对员工排序 |\n"+
" | 8->按工资高低对员工排序 |\n"+
" | 9->删除所有信息 |\n"+
" |________________________________|\n"+
" | 0->保存并退出 |\n"+
" ----------------------------------");
System.out.println("请输入你要选择的功能序号并按回车确认: ");

Scanner scan = new Scanner(System.in);
String st = scan.next(); //等待用户输入指令
byte[] by = st.getBytes();
choice = by[0]-48;

switch(choice) { //根据用户输入判断执行哪一个功能,或者报错提示重新输入
case 1: //查看个人员工信息模块
e.showOneInfo(e,a);
break;
case 2: //查看所有员工信息模块
e.showAllInfo(e,a);
break;
case 3: //添加员工信息模块
e = new Employee();
e.newInfo(e,a);
e.enterContinue();
break;
case 4: //更新员工信息模块
e.refreshInfo(e,a);
e.enterContinue();
break;
case 5: //删除员工信息
e.deleteInfo(e,a);
e.enterContinue();
break;
case 6: //编号排序模块
e.numSort(a);
e.enterContinue();
break;
case 7: //职位排序模块
e.gradeSort(a);
e.enterContinue();
break;
case 8: //工资排序模块
e.paySort(a);
e.enterContinue();
break;
case 9: //删除所有信息模块
a.clear();
ed.saveEmployeeData(a);
System.out.println("删除成功!");
break;
case 0: //保存退出模块
ed.saveEmployeeData(a);
System.exit(0);
default:
System.out.println("输入错误,请重新输入!\n");
}
}
}
}

相关主题