import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Employee1 implements Serializable{
private static final long serialVersionUID =1891026849114142892L;
String EmpId; //编号
String EmpName; //姓名
String Gender; //性别
String BirthDate; //出生日期
String DeptId; //部门编号
public void setEmpId(String a) {
EmpId=a;
}
public String getEmpIdr() {
return EmpId;
}
public void setEmpName(String b) {
EmpName=b;
}
public String getempName() {
return EmpName;
}
public void setGender(String c) {
Gender=c;
}
public String getGender() {
return Gender;
}
public void setBirthDate(String d) {
BirthDate=d;
}
public String getBirthDate() {
return BirthDate;
}
public void setDeptId(String e) {
DeptId=e;
}
public String getDeptId() {
return DeptId;
}
public Employee1() {
}
public Employee1(String EmpId,String EmpName,String Gender,String BirthDate,String DeptId ) {
this.EmpId=EmpId;
this.EmpName=EmpName;
this.Gender=Gender;
this.BirthDate=BirthDate;
this.DeptId=DeptId;
}
}
public class Program1 implements Serializable{
Scanner sc=new Scanner(System.in);
List<Employee1> Emplist=new ArrayList<Employee1>();
//数据存储
public void StoreDate() {
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("E:\\java\\test.txt"));
if(Emplist.size()!=0)
oos.writeObject(Emplist);
oos.close();
if(Emplist.size()==0)
{
System.out.println("无保存记录");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
//数据读取
public void ReadDate(){
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\java\\test.txt"));
Emplist=(ArrayList<Employee1>) ois.readObject();
ois.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
//数据增加
public void AddEmployee(){
ReadDate();
System.out.println("请输入员工的编号:");
String EmpId=sc.next();
System.out.println("请输入员工姓名:");
String EmpName=sc.next();
System.out.println("请输入性别:");
String Gender = sc.next();
System.out.println("请输入出生日期:");
String BirthDate= sc.next();
System.out.println("请输入部门编号:");
String DeptId= sc.next();
Employee1 emp=new Employee1(EmpId,EmpName,Gender,BirthDate,DeptId);
Emplist.add(emp);
StoreDate();
}
//数据修改
public void Update() {
ReadDate();
System.out.println("请输入要修改的员工编号:");
String num=sc.next();
System.out.println("【1】编号");
System.out.println("【2】姓名");
System.out.println("【3】性别");
System.out.println("【4】出生日期");
System.out.println("【5】部门编号");
System.out.println("请选择要修改的内容:");
int k=sc.nextInt();
switch(k) {
case 1:{
System.out.println("请输入新的编号:");
String newEmpId=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.get(i).EmpId=newEmpId;
}
}
System.out.println("修改成功!");
break;
}
case 2:{
System.out.println("请输入新的姓名:");
String newEmpName=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.get(i).EmpName=newEmpName;
}
}
System.out.println("修改成功!");
break;
}
case 3:{
System.out.println("请输入新的姓别:");
String newGender=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.get(i).Gender=newGender;
}
}
System.out.println("修改成功!");
break;
}
case 4:{
System.out.println("请输入新的出生日期:");
String newBirthDate=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.get(i).BirthDate=newBirthDate;
}
}
System.out.println("修改成功!");
break;
}
case 5:{
System.out.println("请输入新的部门编号:");
String newDeptId=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.get(i).DeptId=newDeptId;
}
}
System.out.println("修改成功!");
break;
}
}
StoreDate();
}
//数据删除
public void Delect() {
ReadDate();
System.out.println("请输入要删除的记录的员工编号");
String num=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
Emplist.remove(i);
System.out.println("删除成功!");
}
}
if(Emplist.size()==0) {
System.out.println("无记录");
}
ShowallEmployee();
StoreDate();
}
//数据查询
public void Search() {
ReadDate();
System.out.println("请输入要查询的员工编号:");
String num=sc.next();
for(int i=0;i<Emplist.size();i++) {
if(Emplist.get(i).EmpId==num) {
System.out.println(Emplist.get(i).EmpId+" "+Emplist.get(i).EmpName+" "+Emplist.get(i).Gender+" "+Emplist.get(i).BirthDate+""+Emplist.get(i).DeptId);
}
}
if(Emplist.size()==0) {
System.out.println("无记录!");
}
}
//数据浏览
public void ShowallEmployee() {
ReadDate();
if(Emplist.size()==0) {
System.out.println("无记录!");
}
else {
for(int i=0;i<Emplist.size();i++)
System.out.println(Emplist.get(i).EmpId+" "+Emplist.get(i).EmpName+" "+Emplist.get(i).Gender+" "+Emplist.get(i).BirthDate+" "+Emplist.get(i).DeptId);
}
}
public void Mune() {
System.out.println("***************员工管理系统***************");
System.out.println(" 【1】数据增加 ");
System.out.println(" 【2】数据修改 ");
System.out.println(" 【3】数据删除 ");
System.out.println(" 【4】数据查询 ");
System.out.println(" 【5】数据浏览 ");
System.out.println(" 【0】退出 ");
System.out.println("******************************************");
do {
System.out.println("请选择:");
int t=sc.nextInt();
switch(t) {
case 1:
AddEmployee();
break;
case 2:
Update();
break;
case 3:
Delect();
break;
case 4:
Search();
break;
case 5:
ShowallEmployee();
break;
case 0:
System.exit(0);
}
}while(1!=0);
}
public static void main(String[] args) {
Program1 A=new Program1();
A.Mune();
}
}