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();
}
}