1 package cn.employee_io; 2 3 import java.io.Serializable; 4 5 public class Employee implements Serializable{ 6 /** 7 * 8 */ 9 private static final long serialVersionUID = 1L; 10 private String empId; 11 private String name; 12 private int age; 13 private double salary; 14 15 public Employee() { 16 super(); 17 } 18 19 public Employee(String empId, String name, int age, double salary) { 20 super(); 21 this.empId = empId; 22 this.name = name; 23 this.age = age; 24 this.salary = salary; 25 } 26 27 public String getEmpId() { 28 return empId; 29 } 30 31 public void setEmpId(String empId) { 32 this.empId = empId; 33 } 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public int getAge() { 44 return age; 45 } 46 47 public void setAge(int age) { 48 this.age = age; 49 } 50 51 public double getSalary() { 52 return salary; 53 } 54 55 public void setSalary(double salary) { 56 this.salary = salary; 57 } 58 59 @Override 60 public int hashCode() { 61 final int prime = 31; 62 int result = 1; 63 result = prime * result + ((empId == null) ? 0 : empId.hashCode()); 64 return result; 65 } 66 67 @Override 68 public boolean equals(Object obj) { 69 if (this == obj) 70 return true; 71 if (obj == null) 72 return false; 73 if (getClass() != obj.getClass()) 74 return false; 75 Employee other = (Employee) obj; 76 if (empId == null) { 77 if (other.empId != null) 78 return false; 79 } else if (!empId.equals(other.empId)) 80 return false; 81 return true; 82 } 83 84 public String toString() { 85 return empId + "," + name + "," + age+ "," + salary; 86 } 87 88 } Employee.java
1 package cn.employee_io; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.ObjectInputStream; 8 import java.io.ObjectOutputStream; 9 import java.util.LinkedList; 10 import java.util.List; 11 12 public class Service { 13 List<Employee> list = new LinkedList<Employee>(); 14 File file = null; 15 ObjectInputStream ois = null; 16 ObjectOutputStream oos = null; 17 boolean flag=false; 18 19 public Service() throws IOException, ClassNotFoundException { 20 reader(); 21 } 22 23 /** 24 * 读文件 25 * @throws IOException 26 * @throws ClassNotFoundException 27 */ 28 @SuppressWarnings("unchecked") 29 public void reader() throws IOException, ClassNotFoundException {//ObjectInputStream先写后读 30 file = new File("src/cn/employee_io/emp.txt"); 31 if (!file.exists()) { 32 writer(); 33 }else{ 34 ois = new ObjectInputStream(new FileInputStream(file)); 35 list = (List<Employee>) ois.readObject(); // 给list赋值 36 ois.close(); 37 } 38 } 39 40 /** 41 * 写文件 42 * @throws IOException 43 * @throws ClassNotFoundException 44 */ 45 public void writer() throws IOException, ClassNotFoundException { 46 file = new File("src/cn/employee_io/emp.txt"); 47 oos = new ObjectOutputStream(new FileOutputStream(file)); 48 oos.writeObject(list); 49 oos.flush(); 50 oos.close(); 51 } 52 53 /** 54 * 添加员工 55 * @param e 56 * @throws ClassNotFoundException 57 * @throws IOException 58 */ 59 public void add(Employee e) throws ClassNotFoundException, IOException { 60 if (!list.contains(e)) { 61 list.add(e); 62 writer(); 63 System.out.println("添加成功!"); 64 } else { 65 System.out.println("添加失败!"); 66 } 67 } 68 69 /** 70 * 查询所有员工 71 */ 72 public void queryAll(){ 73 System.out.println("编号 "+"姓名 "+"年龄 "+"薪资"); 74 for(Employee e:list){ 75 System.out.println(e.getEmpId()+" "+e.getName()+" "+e.getAge()+" "+e.getSalary()); 76 } 77 } 78 79 /** 80 * 查询单个员工 81 * @param empId 82 */ 83 public void query(String empId){ 84 for(int i=0;i<list.size();i++){ 85 if(list.get(i).getEmpId().equals(empId)){ 86 System.out.println("编号 "+"姓名 "+"年龄 "+"薪资"); 87 System.out.println(list.get(i).getEmpId()+" "+list.get(i).getName()+" "+list.get(i).getAge()+" "+list.get(i).getSalary()); 88 } 89 } 90 } 91 92 /** 93 * 删除员工 94 * @throws IOException 95 * @throws ClassNotFoundException 96 */ 97 public void delete(String empId) throws ClassNotFoundException, IOException{ 98 for(int i=0;i<list.size();i++){ 99 if(list.get(i).getEmpId().equals(empId)){ 100 list.remove(list.get(i)); 101 writer(); 102 System.out.println("删除成功!"); 103 break; 104 } 105 if((i==list.size()-1)&&(!list.get(i).getEmpId().equals(empId))){ 106 System.out.println("删除失败!"); 107 } 108 } 109 } 110 111 /** 112 * 修改员工 113 * @throws IOException 114 * @throws ClassNotFoundException 115 */ 116 public void update(Employee emp) throws ClassNotFoundException, IOException{ 117 for(int i=0;i<list.size();i++){ 118 if(list.get(i).getEmpId().equals(emp.getEmpId())){ 119 list.get(i).setName(emp.getName()); 120 list.get(i).setAge(emp.getAge()); 121 list.get(i).setSalary(emp.getSalary()); 122 writer(); 123 System.out.println("修改成功!"); 124 } 125 if((i==list.size()-1)&&(!list.get(i).getEmpId().equals(emp.getEmpId()))){ 126 System.out.println("修改失败!"); 127 } 128 } 129 } 130 } Service.java
1 package cn.employee_io; 2 3 import java.io.IOException; 4 import java.util.Scanner; 5 6 public class TestEmp { 7 static Scanner sc = new Scanner(System.in); 8 9 static String empId; 10 static String name; 11 static int age; 12 static double salary; 13 static int num; 14 15 public static void main(String[] args) throws ClassNotFoundException, IOException { 16 Service s=new Service(); 17 18 ok: for (;;) { 19 printOptions(); 20 num = sc.nextInt(); 21 22 if (num < 1 || num > 6) { 23 System.out.println("输入有误,将重新开始选择!"); 24 break ok; 25 } 26 27 switch (num) { 28 case 1: 29 printEmpNo(); 30 printName(); 31 s.add(new Employee(empId,name,age,salary)); 32 break; 33 case 2: 34 s.queryAll(); 35 break; 36 case 3: 37 printEmpNo(); 38 s.query(empId); 39 break; 40 case 4: 41 printEmpNo(); 42 s.delete(empId); 43 break; 44 case 5: 45 printEmpNo(); 46 printName(); 47 s.update(new Employee(empId,name,age,salary)); 48 break; 49 case 6: 50 return; 51 } 52 } 53 } 54 55 public static void printOptions() { 56 System.out.println("***员工管理系统***"); 57 System.out.println("1.添加员工"); 58 System.out.println("2.查询所有员工"); 59 System.out.println("3.查询员工"); 60 System.out.println("4.删除员工"); 61 System.out.println("5.修改员工"); 62 System.out.println("6.退出"); 63 System.out.println("请输入你要进行的操作:"); 64 } 65 66 public static void printEmpNo() { 67 System.out.println("请输入员工编号:"); 68 empId = sc.next(); 69 } 70 71 public static void printName() { 72 System.out.println("请输入员工姓名:"); 73 name = sc.next(); 74 System.out.println("请输入员工年龄:"); 75 age = sc.nextInt(); 76 System.out.println("请输入员工薪资:"); 77 salary=sc.nextDouble(); 78 } 79 } TestEmp.java
原文链接:https://www.cnblogs.com/1020182600HENG/p/5995706.html