直接上代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.situ.demo2.Person;
public class Employee {
public List<Staff> staff = new ArrayList<>();
public static void main(String[] args) {
Employee user = new Employee();
user.system();
}
public void system() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("-------------------欢迎进入员工管理系统---------------------");
System.out.println("1.添加员工 2.开除员工 3.查询员工 4.修改员工信息 5.记录 6.查看");
int i = sc.nextInt();
switch (i) {
case 1:
add();
break;
case 2:
delate();
break;
case 3:
query();
break;
case 4:
modify();
break;
case 5:
output();
break;
case 6:
input();
break;
default:
System.exit(0);
}
}
}
public void add() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入员工的姓名,工号,年龄,性别:");
String name = sc.next();
int num = sc.nextInt();
int age = sc.nextInt();
String sex = sc.next();
staff.add(new Staff(name, num, age, sex));
System.out.println("添加成功!!!!");
}
public void delate() {
System.out.println("请输入您开除的员工工号:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < staff.size(); i++) {
if (num == staff.get(i).getNum())
staff.set(i, new Staff(null, i, i, null));
}
System.out.println("可恶的资本家,开除成功!");
}
public void query() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入员工工号:");
int num = sc.nextInt();
for (int i = 0; i < staff.size(); i++) {
if (num == staff.get(i).getNum()) {
System.out.println(staff.get(i));
return;
}
}
System.out.println("查无此人");
}
public void modify() {
System.out.println("请输入您想修改员工的工号:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < staff.size(); i++) {
if (num == staff.get(i).getNum()) {
System.out.println("请输入要修改的员工信息:");
staff.set(i, new Staff(sc.next(), sc.nextInt(), sc.nextInt(), sc.next()));
}
}
System.out.println("修改成功!!!");
}
public void output() {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("D:\\Staff.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(staff);
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("记录成功!!!");
}
public void input() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("D:\\Staff.txt");
ois = new ObjectInputStream(fis);
ArrayList<Staff> staff = (ArrayList<Staff>) ois.readObject();
for (int i = 0; i < staff.size(); i++) {
System.out.print("姓名:" + staff.get(i).getName() + " ");
System.out.print("工号:" + staff.get(i).getNum() + " ");
System.out.print("年龄:" + staff.get(i).getAge() + " ");
System.out.println("性别:" + staff.get(i).getSex() + " ");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-------------------------------------------------------------------------------------------------
Staff类
import java.io.Serializable;
public class Staff implements Serializable {
private String name;
private int num;
private int age;
private String sex;
public Staff(String name, int num, int age, String sex) {
super();
this.name = name;
this.num = num;
this.age = age;
this.sex = sex;
}
public String toString() {
return "员工信息 [姓名=" + name + ", 工号=" + num + ", 年龄=" + age + ", 性别=" + sex + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}