zoukankan      html  css  js  c++  java
  • 员工管理系统(集合与IO流的结合使用 beta2.0 ObjectInputStream/ ObjectOutputStream)

     1 package cn.employee;
     2 
     3 import java.io.Serializable;
     4 
     5 public class Employee implements Serializable{
     6     private static final long serialVersionUID = 1L;
     7     private int empNo;
     8     private String name;
     9     private String department;
    10 
    11     public Employee() {
    12         super();
    13     }
    14 
    15     public Employee(int empNo, String name, String department) {
    16         super();
    17         this.empNo = empNo;
    18         this.name = name;
    19         this.department = department;
    20     }
    21 
    22     public int getEmpNo() {
    23         return empNo;
    24     }
    25 
    26     public void setEmpNo(int empNo) {
    27         this.empNo = empNo;
    28     }
    29 
    30     public String getName() {
    31         return name;
    32     }
    33 
    34     public void setName(String name) {
    35         this.name = name;
    36     }
    37 
    38     public String getDepartment() {
    39         return department;
    40     }
    41 
    42     public void setDepartment(String department) {
    43         this.department = department;
    44     }
    45 
    46     public int hashCode() {
    47         final int prime = 31;
    48         int result = 1;
    49         result = prime * result
    50                 + ((department == null) ? 0 : department.hashCode());
    51         result = prime * result + empNo;
    52         result = prime * result + ((name == null) ? 0 : name.hashCode());
    53         return result;
    54     }
    55 
    56     public String toString() {
    57         return "Employee [empNo=" + empNo + ", name=" + name + ", department="
    58                 + department + "]";
    59     }
    60 
    61 }
    Employee
     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                 System.out.println("修改成功!");
    79                 break;
    80             }
    81         }
    82     }
    83 }
    EmpUtils
      1 package cn.employee;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.FileOutputStream;
      7 import java.io.IOException;
      8 import java.io.ObjectInputStream;
      9 import java.io.ObjectOutputStream;
     10 import java.io.OutputStream;
     11 import java.util.ArrayList;
     12 import java.util.List;
     13 import java.util.Scanner;
     14 
     15 public class MyEmployee {
     16     Scanner sc = new Scanner(System.in);
     17     List<Employee> list = null;
     18     int empNo;
     19     String name;
     20     String department;
     21     int num = 0;
     22     boolean flag = false;
     23     File file = null;
     24     FileOutputStream fos=null;
     25     ObjectInputStream ois = null;
     26     ObjectOutputStream oos = null;
     27 
     28     public MyEmployee() throws FileNotFoundException, IOException,
     29             ClassNotFoundException {
     30         list = new ArrayList<Employee>();
     31         fun(list);
     32     }
     33 
     34     public void fun(List<Employee> list) throws FileNotFoundException,
     35             IOException, ClassNotFoundException {
     36         ok: for (;;) {
     37             printOptions();
     38             num = sc.nextInt();
     39 
     40             if (num < 1 || num > 6) {
     41                 System.out.println("输入有误,将重新开始选择!");
     42                 break ok;
     43             }
     44 
     45             switch (num) {
     46             case 1:
     47                 printEmpNo();
     48                 if (!EmpUtils.copy(list, empNo)) {
     49                     printName();
     50                     if (EmpUtils.add(list,
     51                             new Employee(empNo, name, department))) {
     52                         addObject(list);//
     53                         System.out.println("添加成功!");
     54                     }
     55                 } else {
     56                     System.out.println("添加失败!");
     57                 }
     58                 break;
     59             case 2:
     60                 // EmpUtils.querys(list);
     61                 list=queryAllObject();
     62 //                System.out.println(list); 
     63                 break;
     64             case 3: 
     65 //                System.out.println("3333   "+list); 
     66                 printEmpNo();
     67                 EmpUtils.query(list, empNo);
     68                 break;
     69             case 4:
     70                 printEmpNo();
     71                 EmpUtils.delete(list, empNo);
     72 //                System.out.println(list); 
     73                 addObject(list);
     74                 break;
     75             case 5:
     76                 printEmpNo();
     77                 if (EmpUtils.copy(list, empNo)) {// 有该员工
     78                     printName();
     79                     EmpUtils.update(list, empNo, name, department);
     80                 }
     81                 addObject(list);
     82                 break;
     83             case 6:
     84                 flag = true;
     85             }
     86 
     87             if (flag) {// 退出
     88                 break;
     89             }
     90         }
     91     }
     92 
     93     public void printOptions() {
     94         System.out.println("***员工管理系统***");
     95         System.out.println("1.添加员工");
     96         System.out.println("2.查询所有员工");
     97         System.out.println("3.查询员工");
     98         System.out.println("4.删除员工");
     99         System.out.println("5.修改员工");
    100         System.out.println("6.退出");
    101         System.out.println("请输入你要进行的操作:");
    102     }
    103 
    104     public void printEmpNo() {
    105         System.out.println("请输入员工编号:");
    106         empNo = sc.nextInt();
    107     }
    108 
    109     public void printName() {
    110         System.out.println("请输入员工姓名:");
    111         name = sc.next();
    112         System.out.println("请输入员工部门:");
    113         department = sc.next();
    114     }
    115 
    116     /**
    117      * 在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)
    118      * 间接的构建了ObjectOutputStream流对象
    119      * ,在向外读数据的时候第一次运行的时候不会报错,在第二次就会报java.io.StreamCorruptedException: invalid
    120      * type code: AC错误
    121      * 
    122      * 在一个文件都有一个文件的头部和文件体。由于对多次使用FileOutputStream(文件名,true)
    123      * 构建的ObjectOutputStream对象向同一个文件写数据
    124      * ,在每次些数据的时候他都会向这个文件末尾先写入header在写入你要写的对象数据,在读取的时候遇到这个在文件体中的header就会报错
    125      * 。导致读出时,出现streamcorrput异常。
    126      * 
    127      * 解决办法:所以这里要判断是不是第一次写文件,若是则写入头部,否则不写入。
    128      * 
    129      * @param list
    130      * @throws FileNotFoundException
    131      * @throws IOException
    132      */
    133     public void addObject(List<Employee> list) throws FileNotFoundException, IOException {
    134         file = new File("src/cn/employee/emp.txt");
    135 //        fos=new FileOutputStream(file,true);
    136         fos=new FileOutputStream(file);
    137         if (file.length() < 1) {// 判断文件大小并调用不同的方法
    138             oos = new ObjectOutputStream(fos);
    139         } else {
    140             oos = new MyObjectOutputStream(fos);
    141         }
    142         oos.writeObject(list);
    143         oos.flush();
    144         oos.close();
    145     }
    146 
    147     @SuppressWarnings("unchecked")
    148     public List<Employee> queryAllObject() throws FileNotFoundException, IOException,
    149             ClassNotFoundException {
    150         ois = new ObjectInputStream(new FileInputStream(
    151                 "src/cn/employee/emp.txt"));
    152         List<Employee> list2=new ArrayList<Employee>();
    153         list2 = (ArrayList<Employee>) ois.readObject();
    154 
    155         for (int i = 0; i < list2.size(); i++) {
    156             System.out.println(list2.get(i));
    157         }
    158 
    159         ois.close();
    160         return list2;
    161     }
    162 }
    163 
    164 /*class MyObjectOutputStream extends ObjectOutputStream {
    165     public MyObjectOutputStream() throws IOException {
    166         super();
    167     }
    168 
    169     public MyObjectOutputStream(OutputStream out) throws IOException {
    170         super(out);
    171     }
    172 
    173     @Override
    174     protected void writeStreamHeader() throws IOException {
    175         return;
    176     }
    177 }*/
    MyEmployee
     1 package cn.employee;
     2 
     3 import java.io.IOException;
     4 import java.io.ObjectOutputStream;
     5 import java.io.OutputStream;
     6 
     7 public class MyObjectOutputStream extends ObjectOutputStream {
     8     public MyObjectOutputStream() throws IOException {
     9         super();
    10     }
    11 
    12     public MyObjectOutputStream(OutputStream out) throws IOException {
    13         super(out);
    14     }
    15 
    16     @Override
    17     protected void writeStreamHeader() throws IOException {
    18         return;
    19     }
    20 }
    MyObjectOutputStream
     1 package cn.employee;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 
     6 /**
     7  * 员工管理系统
     8  * @author 王恒
     9  * @time 2016年10月19日 下午8:54:14
    10  */
    11 public class TestEmp {
    12 
    13     @SuppressWarnings("unused")
    14     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException,NullPointerException {
    15         
    16         MyEmployee a=new MyEmployee();
    17         
    18     }
    19 }
    TestEmp
  • 相关阅读:
    大话设计模式C++实现-第1章-简单工厂模式
    mac下的git的安装与简单的配置
    Execute failed: java.io.IOException: Cannot run program &quot;sdk-linux/build-tools/22.0.0/aapt&quot;: error=2
    UIScrollView 循环滚动,代码超简单
    字符编码的前世今生
    Android 4.4 KitKat 支持 u 盘功能
    Java Tread多线程(1)实现Runnable接口
    (hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)
    每日算法之二十三:Reverse Nodes in k-Group
    Android4.0-4.4 加入实体按键振动支持的方法(java + smali版本号)
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/5995940.html
Copyright © 2011-2022 走看看