在Java中实现List: 员工管理系统 员工属性:员工编号、员工姓名、性别、年龄、所在部门 1—员工添加 员工信息修改 4—根据员工编号查询员工信息 5—查询所有员工信息要求1.员工信息存储在TreeSet中2.按照员工编号进行升序排序
时间: 2024-02-19 17:02:42 浏览: 115
java代码-定义一个员工信息类Employee,使用ArrayList对员工信息进行添加和显示。 运行效果为
好的,这是一个很实用的问题,下面是一个简单的Java代码示例,可以实现这个员工管理系统:
```java
import java.util.*;
public class EmployeeManagementSystem {
private TreeSet<Employee> employees;
public EmployeeManagementSystem() {
employees = new TreeSet<>(Comparator.comparingInt(Employee::getId));
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void modifyEmployee(Employee employee) {
employees.remove(employee);
employees.add(employee);
}
public Employee getEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
public List<Employee> getAllEmployees() {
return new ArrayList<>(employees);
}
}
class Employee {
private int id;
private String name;
private String gender;
private int age;
private String department;
public Employee(int id, String name, String gender, int age, String department) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", department='" + department + '\'' +
'}';
}
}
```
这个代码示例中,我们首先定义了一个员工类 `Employee`,它包含了员工的属性。然后我们定义了一个员工管理系统 `EmployeeManagementSystem`,它包含了添加员工、修改员工、根据员工编号查询员工信息、查询所有员工信息等方法。注意,我们使用了 `TreeSet` 来存储员工信息,并且在构造函数中指定了按照员工编号进行升序排序的比较器。最后,我们在 `main` 函数中演示了如何使用这个员工管理系统。
阅读全文