1)创建部门枚举,对象有:JAVA、HTML、PYTHON 2)创建员工实体类,属性有:姓名、年龄、薪资、部门枚举 3) 实现员工的管理系统(底层数据结构不限),有添加员工、删除员工、修改员工、查询员工 4)根据姓名、年龄段、薪资、部门等各种情况查询学生 5)编写统计员工信息方法,统计最高工资的员工、最低薪资的员工、根据部门统计员工,通过Stream流统计
时间: 2023-05-26 14:02:28 浏览: 91
java 枚举类总结
1) 部门枚举的创建:
public enum Department {
JAVA,
HTML,
PYTHON
}
2) 员工实体类的创建:
public class Employee {
private String name;
private int age;
private int salary;
private Department department;
public Employee(String name, int age, int salary, Department department) {
this.name = name;
this.age = age;
this.salary = salary;
this.department = department;
}
// getter and setter
}
3) 员工管理系统的实现:
public class EmployeeManagementSystem {
private List<Employee> employeeList;
public EmployeeManagementSystem() {
employeeList = new ArrayList<>();
}
// 添加员工
public void addEmployee(Employee employee) {
employeeList.add(employee);
}
// 删除员工
public void deleteEmployee(Employee employee) {
employeeList.remove(employee);
}
// 修改员工
public void updateEmployee(Employee employee) {
for (int i = 0; i < employeeList.size(); i++) {
if (employeeList.get(i).getName().equals(employee.getName())) {
employeeList.set(i, employee);
break;
}
}
}
// 查询员工
public List<Employee> queryEmployee(String name, int age, int salary, Department department) {
List<Employee> result = new ArrayList<>();
for (Employee employee : employeeList) {
if ((name == null || employee.getName().equals(name))
&& (age <= 0 || employee.getAge() == age)
&& (salary <= 0 || employee.getSalary() == salary)
&& (department == null || employee.getDepartment() == department)) {
result.add(employee);
}
}
return result;
}
}
4) 查询员工的方法:
List<Employee> employeeList = employeeManagementSystem.queryEmployee("张三", 20, 0, Department.JAVA);
5) 统计员工信息方法的实现:
// 统计最高工资的员工
Optional<Employee> maxSalaryEmployee = employeeList.stream().max(Comparator.comparing(Employee::getSalary));
// 统计最低薪资的员工
Optional<Employee> minSalaryEmployee = employeeList.stream().min(Comparator.comparing(Employee::getSalary));
// 根据部门统计员工
Map<Department, Long> departmentCount = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
阅读全文