帮我用java实现下列需求 1.项目目标 (1)掌握集合框架包含的内容; (2)掌握ArrayList和LinkedList的使用; (3)掌握HashMap的使用; (4)掌握Iterator的使用; (5)掌握泛型集合的使用。 2.项目任务 请做一个公司职员薪水管理系统,要求完成如下功能:当有新员工时,将该员工加入到管理系统; (1)可以根据员工号,显示该员工的信息; (2)可以显示所有员工信息; (3)可以修改员工薪水 (4)当员工离职时,将该员工从管理系统中删除。
时间: 2023-06-24 10:09:03 浏览: 112
好的,以下是一个简单的实现。注意,这只是示例代码,实际使用时需要考虑更多的细节和边界情况。
```
import java.util.*;
public class EmployeeManagementSystem {
private List<Employee> employees;
private Map<Integer, Employee> employeeMap;
public EmployeeManagementSystem() {
employees = new ArrayList<>();
employeeMap = new HashMap<>();
}
public void addEmployee(Employee employee) {
employees.add(employee);
employeeMap.put(employee.getEmployeeID(), employee);
}
public Employee getEmployee(int employeeID) {
return employeeMap.get(employeeID);
}
public List<Employee> getAllEmployees() {
return employees;
}
public void updateSalary(int employeeID, double salary) {
Employee employee = employeeMap.get(employeeID);
if (employee != null) {
employee.setSalary(salary);
}
}
public void removeEmployee(int employeeID) {
Employee employee = employeeMap.get(employeeID);
if (employee != null) {
employees.remove(employee);
employeeMap.remove(employeeID);
}
}
public static void main(String[] args) {
EmployeeManagementSystem system = new EmployeeManagementSystem();
// 添加员工
Employee employee1 = new Employee(1, "张三", 5000);
Employee employee2 = new Employee(2, "李四", 6000);
system.addEmployee(employee1);
system.addEmployee(employee2);
// 显示所有员工信息
List<Employee> allEmployees = system.getAllEmployees();
for (Employee employee : allEmployees) {
System.out.println(employee);
}
// 修改员工薪水
system.updateSalary(1, 5500);
System.out.println(system.getEmployee(1));
// 删除员工
system.removeEmployee(2);
allEmployees = system.getAllEmployees();
for (Employee employee : allEmployees) {
System.out.println(employee);
}
}
}
class Employee {
private int employeeID;
private String name;
private double salary;
public Employee(int employeeID, String name, double salary) {
this.employeeID = employeeID;
this.name = name;
this.salary = salary;
}
public int getEmployeeID() {
return employeeID;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "员工ID:" + employeeID + ",姓名:" + name + ",薪水:" + salary;
}
}
```
阅读全文