import java.io.*; import java.util.*;
class Employee implements Serializable { private static final long serialVersionUID = 1L; private String id; private String name; private String sex; private String education; private int age; private double salary; private String phone;
public Employee(String id, String name, String sex, String education, int age, double salary, String phone) { this.id = id; this.name = name; this.sex = sex; this.education = education; this.age = age; this.salary = salary; this.phone = phone; }
public String getId() { return id; }
public String getName() { return name; }
public String getSex() { return sex; }
public String getEducation() { return education; }
public int getAge() { return age; }
public double getSalary() { return salary; }
public String getPhone() { return phone; }
public void setId(String id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setSex(String sex) { this.sex = sex; }
public void setEducation(String education) { this.education = education; }
public void setAge(int age) { this.age = age; }
public void setSalary(double salary) { this.salary = salary; }
public void setPhone(String phone) { this.phone = phone; }
@Override public String toString() { return "工号: " + id + "\n" + "姓名: " + name + "\n" + "性别(m或f): " + sex + "\n" + "学历: " + education + "\n" + "年龄: " + age + "\n" + "工资: " + String.format("%.1f", salary) + "\n" + "电话: " + phone + "\n"; } }
class EmployeeManager { private List<Employee> employees = new ArrayList<>();
public void addWorker(Employee employee) { employees.add(employee); }
public void deleteWorker(String id) { for (Employee employee : employees) { if (employee.getId().equals(id)) { employees.remove(employee); break; } } }
public void modification(Employee employee) { for (Employee e : employees) { if (e.getId().equals(employee.getId())) { e.setName(employee.getName()); e.setSex(employee.getSex()); e.setEducation(employee.getEducation()); e.setAge(employee.getAge()); e.setSalary(employee.getSalary()); e.setPhone(employee.getPhone()); break; } } }
public List<Employee> getEmployees() { return employees; }
public void sortByIdAsc() { Collections.sort(employees, new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getId().compareTo(o2.getId()); } }); }
public void sortByIdDesc() { Collections.sort(employees, new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o2.getId().compareTo(o1.getId()); } }); }
public void sortBySalaryAsc() { Collections.sort(employees, new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return Double.compare(o1.getSalary(), o2.getSalary()); } }); }
public void sortBySalaryDesc() { Collections.sort(employees, new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return Double.compare(o2.getSalary(), o1.getSalary()); } }); }
public Employee findById(String id) { for (Employee employee : employees) { if (employee.getId().equals(id)) { return employee; } } return null; }
public List<Employee> findByName(String name) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (employee.getName().equals(name)) { result.add(employee); } } return result; }
public void writeFile() throws IOException { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employees.dat"))) { oos.writeObject(employees); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void loadFile() throws IOException, ClassNotFoundException { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employees.dat")); employees = (List<Employee>) ois.readObject(); ois.close(); } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
}
public class Main { private static Scanner scanner = new Scanner(System.in); private static EmployeeManager manager = new EmployeeManager(); public static void main(String[] args) throws IOException, ClassNotFoundException { manager.loadFile(); while (true) { 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("6. 所有职工信息显示"); System.out.println("7. 职工信息保存"); System.out.println("0. 退出程序");
int choice = scanner.nextInt(); scanner.nextLine();
switch (choice) { case 1: addWorker(); break; case 2: modification(); break; case 3: deleteWorker(); break; case 4: sortWorker(); break; case 5: findWorker(); break; case 6: showWorker(); break; case 7: saveEmployees(); break; case 0: System.exit(0); break; default: System.out.println("无效的选择,请重新输入。"); break; } } }
private static void addWorker() { System.out.println("请输入职工信息:"); System.out.print("工号: "); String id = scanner.nextLine(); System.out.print("姓名: "); String name = scanner.nextLine(); System.out.print("性别(m或f): "); String sex = scanner.nextLine(); System.out.print("学历: "); String education = scanner.nextLine(); System.out.print("年龄: "); int age = scanner.nextInt(); scanner.nextLine(); System.out.print("工资: "); double salary = scanner.nextDouble(); scanner.nextLine(); System.out.print("电话: "); String phone = scanner.nextLine();
Employee employee = new Employee(id, name, sex, education, age, salary, phone); manager.addWorker(employee); System.out.println("职工信息已添加。"); }
private static void modification() { System.out.print("请输入要修改的职工工号: "); String id = scanner.nextLine(); Employee employee = manager.findById(id); if (employee == null) { System.out.println("未找到该职工。"); return; } System.out.println("请输入新的职工信息:"); System.out.print("姓名: "); String name = scanner.nextLine(); System.out.print("性别(m或f): "); String sex = scanner.nextLine(); System.out.print("学历: "); String education = scanner.nextLine(); System.out.print("年龄: "); int age = scanner.nextInt(); scanner.nextLine(); System.out.print("工资: "); double salary = scanner.nextDouble(); scanner.nextLine(); System.out.print("电话: "); String phone = scanner.nextLine();
employee.setName(name); employee.setSex(sex); employee.setEducation(education); employee.setAge(age); employee.setSalary(salary); employee.setPhone(phone); manager.modification(employee); System.out.println("职工信息已更新。"); }
private static void deleteWorker() { System.out.print("请输入要删除的职工工号: "); String id = scanner.nextLine(); manager.deleteWorker(id); System.out.println("职工信息已删除。"); }
private static void sortWorker() { System.out.println("请选择排序方式:"); System.out.println("1. 按工号升序排序"); System.out.println("2. 按工号降序排序"); System.out.println("3. 按工资升序排序"); System.out.println("4. 按工资降序排序");
int choice = scanner.nextInt(); scanner.nextLine();
switch (choice) { case 1: manager.sortByIdAsc(); System.out.println("职工信息已按工号升序排序。"); break; case 2: manager.sortByIdDesc(); System.out.println("职工信息已按工号降序排序。"); break; case 3: manager.sortBySalaryAsc(); System.out.println("职工信息已按工资升序排序。"); break; case 4: manager.sortBySalaryDesc(); System.out.println("职工信息已按工资降序排序。"); break; default: System.out.println("无效的选择,请重新输入。"); break; } }
private static void findWorker() { System.out.println("请选择查找方式:"); System.out.println("1. 按工号查找"); System.out.println("2. 按姓名查找");
int choice = scanner.nextInt(); scanner.nextLine();
switch (choice) { case 1: System.out.print("请输入要查找的职工工号: "); String id = scanner.nextLine(); Employee employee = manager.findById(id); if (employee == null) { System.out.println("未找到该职工。"); } else { System.out.println(employee); } break; case 2: System.out.print("请输入要查找的职工姓名: "); String name = scanner.nextLine(); List<Employee> employees = manager.findByName(name); if (employees.isEmpty()) { System.out.println("未找到该职工。"); } else { for (Employee e : employees) { System.out.println(e); } } break; default: System.out.println("无效的选择,请重新输入。"); break; } }
private static void showWorker() { List<Employee> employees = manager.getEmployees(); if (employees.isEmpty()) { System.out.println("没有职工信息。"); } else { for (Employee employee : employees) { System.out.println(employee); } } }
private static void saveEmployees() { try { manager.writeFile(); System.out.println("职工信息已保存"); } catch (IOException e) { System.out.println("保存职工信息失败: " + e.getMessage()); } }
}
|