员工管理系统(集合与IO流的结合使用 beta3.0 BufferedReader / ObjectOutputStream)

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