1 package cn.employee; 2 3 public class Employee { 4 private int empNo; 5 private String name; 6 private String department; 7 8 public Employee() { 9 super(); 10 } 11 12 public Employee(int empNo, String name, String department) { 13 super(); 14 this.empNo = empNo; 15 this.name = name; 16 this.department = department; 17 } 18 19 public int getEmpNo() { 20 return empNo; 21 } 22 23 public void setEmpNo(int empNo) { 24 this.empNo = empNo; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public String getDepartment() { 36 return department; 37 } 38 39 public void setDepartment(String department) { 40 this.department = department; 41 } 42 43 public int hashCode() { 44 final int prime = 31; 45 int result = 1; 46 result = prime * result 47 + ((department == null) ? 0 : department.hashCode()); 48 result = prime * result + empNo; 49 result = prime * result + ((name == null) ? 0 : name.hashCode()); 50 return result; 51 } 52 53 public String toString() { 54 return "Employee [empNo=" + empNo + ", name=" + name + ", department=" 55 + department + "]"; 56 } 57 58 }
1 package cn.employee; 2 3 import java.util.List; 4 5 public class EmpUtils { 6 7 /** 8 * 是否有相同的员工编号 9 */ 10 public static boolean copy(List<Employee> list,int empNo){ 11 boolean flag = false; 12 for (int i = 0; i < list.size(); i++) { 13 if(list.get(i).getEmpNo()==empNo){ 14 flag=true; 15 break; 16 } 17 } 18 return flag; 19 } 20 21 /** 22 * 添加员工 23 */ 24 public static boolean add(List<Employee> list, Employee e) { 25 if (list.contains(e)) { 26 System.out.println("有重复的员工"); 27 return false; 28 } 29 return list.add(e); 30 } 31 32 /** 33 * 查询所有员工 34 */ 35 public static void querys(List<Employee> list) { 36 for (int i = 0; i < list.size(); i++) { 37 System.out.println(list.get(i)); 38 } 39 } 40 41 /** 42 * 查询指定员工 43 */ 44 public static void query(List<Employee> list,int empNo){ 45 for(int i=0;i<list.size();i++){ 46 if(list.get(i).getEmpNo()==empNo){ 47 System.out.println(list.get(i)); 48 break; 49 } 50 if((i==list.size()-1)&&list.get(i).getEmpNo()!=empNo){ 51 System.out.println("不存在该员工!"); 52 } 53 } 54 } 55 /** 56 * 删除员工 57 */ 58 public static void delete(List<Employee> list,int empNo){ 59 for(int i=0;i<list.size();i++){ 60 if(list.get(i).getEmpNo()==empNo){ 61 list.remove(list.get(i)); 62 System.out.println("删除成功!"); 63 break; 64 } 65 if((i==list.size()-1)&&list.get(i).getEmpNo()!=empNo){ 66 System.out.println("删除失败!"); 67 } 68 } 69 } 70 /** 71 * 修改员工 72 */ 73 public static void update(List<Employee> list,int empNo,String name,String department){ 74 for (Employee e : list) { 75 if(e.getEmpNo()==empNo){ 76 e.setName(name); 77 e.setDepartment(department); 78 break; 79 } 80 } 81 } 82 }
1 package cn.employee; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 public class MyEmployee { 8 Scanner sc = new Scanner(System.in); 9 List<Employee> list=null; 10 int empNo; 11 String name; 12 String department; 13 int num = 0; 14 boolean flag = false; 15 16 public MyEmployee() { 17 18 list = new ArrayList<Employee>(); 19 fun(list); 20 } 21 22 public void fun(List<Employee> list){ 23 24 ok: for (;;) { 25 printOptions(); 26 num = sc.nextInt(); 27 28 if (num < 1 || num > 6) { 29 System.out.println("输入有误,将重新开始选择!"); 30 break ok; 31 } 32 33 switch (num) { 34 case 1: 35 printEmpNo(); 36 if (!EmpUtils.copy(list, empNo)) { 37 printName(); 38 if (EmpUtils.add(list,new Employee(empNo, name, department))) { 39 System.out.println("添加成功!"); 40 } 41 } else { 42 System.out.println("添加失败!"); 43 } 44 break; 45 case 2: 46 EmpUtils.querys(list); 47 break; 48 case 3: 49 printEmpNo(); 50 EmpUtils.query(list, empNo); 51 break; 52 case 4: 53 printEmpNo(); 54 EmpUtils.delete(list, empNo); 55 break; 56 case 5: 57 printEmpNo(); 58 if (EmpUtils.copy(list, empNo)) {// 有该员工 59 printName(); 60 EmpUtils.update(list, empNo, name, department); 61 } 62 break; 63 case 6: 64 flag = true; 65 } 66 67 if (flag) {// 退出 68 break; 69 } 70 } 71 } 72 73 public void printOptions(){ 74 System.out.println("***员工管理系统***"); 75 System.out.println("1.添加员工"); 76 System.out.println("2.查询所有员工"); 77 System.out.println("3.查询员工"); 78 System.out.println("4.删除员工"); 79 System.out.println("5.修改员工"); 80 System.out.println("6.退出"); 81 System.out.println("请输入你要进行的操作:"); 82 } 83 84 public void printEmpNo(){ 85 System.out.println("请输入员工编号:"); 86 empNo = sc.nextInt(); 87 } 88 89 public void printName(){ 90 System.out.println("请输入员工姓名:"); 91 name = sc.next(); 92 System.out.println("请输入员工部门:"); 93 department = sc.next(); 94 } 95 }
1 package cn.employee; 2 /** 3 * 员工管理系统 4 * @author 王恒 5 * @time 2016年10月19日 下午8:54:14 6 */ 7 public class TestEmp { 8 9 public static void main(String[] args) { 10 11 MyEmployee a=new MyEmployee(); 12 13 } 14 }