1. 程式人生 > >員工管理系統

員工管理系統

import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap;

/**

  • Created by Administrator on 2018-10-29 .

  • Created by Administrator on 10:22. */ //初始化員工資訊,存放MAP public class Init { public static HashMap<Integer,Employee> hmEmployee = new HashMap<>(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm;ss”);

    static { Employee employee1 = new Employee(“北大青鳥”,“1989-06-06”,“高中”,“寒冰”,1,“實習生”,“13322221111”); Employee employee2 = new Employee(“北大青鳥”,“1989-07-06”,“高中”,“馬達”,2,“實習生”,“13322221111”); Employee employee3 = new Employee(“北大青鳥”,“1989-05-06”,“研究生”,“陳怡”,3,“專案經理”,“13322221111”); Employee employee4 = new Employee(“北大青鳥”,“1989-04-06”,“本科”,“王浩”,4,“程式設計師”,“13322221111”); Employee employee5 = new Employee(“北大青鳥”,“1989-03-06”,“本科”,“王瓊”,5,“程式設計師”,“13322221111”); hmEmployee.put(1,employee1); hmEmployee.put(2,employee2); hmEmployee.put(3,employee3); hmEmployee.put(4,employee4); hmEmployee.put(5,employee5);

    } } import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date;

/**

  • Created by Administrator on 2018-10-29 .

  • Created by Administrator on 10:14. */ //建立員工類 public class Employee implements Serializable{ private int number; private String name; private String birthDay; private String education; private String position; private String address; private String tel;

    public Employee(String address, String birthDay, String education, String name, int number, String position, String tel) { this.address = address; this.birthDay = birthDay; this.education = education; this.name = name; this.number = number; this.position = position; this.tel = tel; }

    public String getAddress() { return address; }

    public void setAddress(String address) { this.address = address; }

    public String getBirthDay() { return birthDay; }

    public void setBirthDay(String birthDay) { this.birthDay = birthDay; }

    public String getEducation() { return education; }

    public void setEducation(String education) { this.education = education; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public int getNumber() { return number; }

    public void setNumber(int number) { this.number = number; }

    public String getPosition() { return position; }

    public void setPosition(String position) { this.position = position; }

    public String getTel() { return tel; }

    public void setTel(String tel) { this.tel = tel; } } import java.io.*; import java.util.Collection; import java.util.HashMap; import java.util.Scanner;

/**

  • Created by Administrator on 2018-10-29 .

  • Created by Administrator on 10:49. */ public class Manage { HashMap<Integer,Employee> hm = Init.hmEmployee; Scanner input = new Scanner(System.in); File file = new File(“employee.txt”);

    //操作選單方法 public void menu(){ unserializable(); System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); Collection collection = hm.values(); for (Employee e :collection){ System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } do { System.out.print(“請選擇操作 1.新增 2.修改 3.刪除 4.查詢”); int choose = input.nextInt(); switch (choose){ case 1: System.out.println(“請輸入員工姓名:”); String addName = input.next(); System.out.println(“請輸入員工編號”); int num = input.nextInt(); System.out.println(“請輸入出生日期:”); String birthday = input.next();

                 System.out.println("請輸入學歷:");
                 String edu = input.next();
                 System.out.println("請輸入職位");
                 String positions = input.next();
                 System.out.println("請輸入地址");
                 String addresses = input.next();
                 System.out.println("請輸入電話");
                 String tele = input.next();
                 Employee e = new Employee(addresses,birthday,edu,addName,num,positions,tele);
                 add(num,e);
                 break;
             case 2:
                 System.out.println("請輸入要修改的員工編號!");
                 int updateNum = input.nextInt();
                 System.out.println("請輸入員工姓名:");
                 String updateName = input.next();
                 System.out.println("請輸入出生日期:");
                 String updatebirthday = input.next();
                 System.out.println("請輸入學歷:");
                 String updateedu = input.next();
                 System.out.println("請輸入職位");
                 String updatePosition = input.next();
                 System.out.println("請輸入地址");
                 String updateAddresses = input.next();
                 System.out.println("請輸入電話");
                 String updateTele = input.next();
                 Employee employee = new Employee(updateAddresses,updatebirthday,updateedu,updateName,updateNum,updatePosition,updateTele);
                 update(updateNum,employee);
                 break;
             case 3:
                 System.out.println("請輸入要刪除的員工編號:");
                 int deleteNum = input.nextInt();
                 delete(deleteNum);
                 break;
             case 4:
                 System.out.println("--------------根據條件查詢-------------");
                 System.out.println("1.學歷  2.職位");
                 int choice = input.nextInt();
                 if (choice ==1){
                     System.out.println("請輸入學歷:");
                     String edus = input.next();
                     serchByEdu(edus);
                 }else if(choice == 2){
                     System.out.println("請輸入職位:");
                     String posi = input.next();
                     searchByPosition(posi);
                 }else {
                     System.out.println("輸入有誤!");
                 }
                 break;
             default:
                 System.out.println("輸入有誤,請重新選擇!");
                 break;
         }
     }while (true);
    

    }

    //反序列化 public void unserializable(){ ObjectInputStream ois = null; try { if (file.exists()){ if (file.length()>0){ ois = new ObjectInputStream(new FileInputStream(file)); hm = (HashMap<Integer, Employee>) ois.readObject(); } }

     } catch (IOException e) {
         e.printStackTrace();
     } catch (ClassNotFoundException e) {
         e.printStackTrace();
     }
     finally {
         try {
             if (ois!= null){
                 ois.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    

    }

    //序列化 public void serializable(){ ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(hm);//上面反序列化的時候把資料讀到了記憶體,這裡序列化把更新後的學生資料從記憶體讀到檔案 oos.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } }

    //新增員工 public void add(int nums, Employee employee){ unserializable(); if (hm.containsKey(nums)){ System.out.println(“該編號已有員工,新增失敗!”); }else { hm.put(nums,employee); System.out.println(“新增成功!”); System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); Collection collection = hm.values(); for (Employee e :collection){ System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } } serializable(); }

    //修改員工 public void update(int num,Employee employee){ unserializable(); if (hm.containsKey(num)){ hm.get(num).setName(employee.getName()); hm.get(num).setBirthDay(employee.getBirthDay()); hm.get(num).setEducation(employee.getEducation()); hm.get(num).setPosition(employee.getPosition()); hm.get(num).setAddress(employee.getAddress()); hm.get(num).setTel(employee.getTel()); System.out.println(“修改成功!”); System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); Collection collection = hm.values(); for (Employee e :collection){ System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } }else { System.out.println(“編號不存在!”); } serializable(); }

    //根據員工編號刪除員工資訊,顯示員工列表 public void delete(int num){ unserializable(); if (hm.containsKey(num)){ hm.remove(num); Collection collection = hm.values(); System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); for (Employee e :collection){ System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } } serializable(); }

    //根據條件查詢員工資訊,顯示員工列表 //根據學歷查詢 public void serchByEdu(String edu){ unserializable(); Collection collection = hm.values(); for (Employee e:collection){ if (e.getEducation().contains(edu)){ System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } } }

    //根據職位查詢 public void searchByPosition(String position){ unserializable(); Collection collection = hm.values(); for (Employee e:collection){ if (e.getPosition().contains(position)){ System.out.println(“員工編號 員工姓名 出生年月 學歷 職位 地址”); System.out.println(" “+e.getNumber()+” “+e.getName()+” " + " “+e.getBirthDay()+” “+e.getEducation()+” “+e.getPosition()+” "+e.getAddress()); } } } } //test類 /**

  • Created by Administrator on 2018-10-29 .

  • Created by Administrator on 10:56. */ public class Test { public static void main(String[] args) { Manage manage = new Manage(); manage.menu(); } }