1)创建部门枚举,对象有:JAVA、HTML、PYTHON 2)创建员工实体类,属性有:姓名、年龄、薪资、部门枚举 3) 实现员工的管理系统(底层数据结构不限),有添加员工、删除员工、修改员工、查询员工 4)根据姓名、年龄段、薪资、部门等各种情况查询学生 5)编写统计员工信息方法,统计最高工资的员工、最低薪资的员工、根据部门统计员工,通过Stream流统计 用代码实现
时间: 2023-05-26 09:02:29 浏览: 86
1)部门枚举类Department:
```
public enum Department {
JAVA,
HTML,
PYTHON
}
```
2)员工实体类Employee:
```
public class Employee {
private String name;
private int age;
private double salary;
private Department department;
// 构造方法、getter和setter方法略
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", department=" + department +
'}';
}
}
```
3) 员工管理类EmployeeManagement:
```
public class EmployeeManagement {
private List<Employee> employees;
public EmployeeManagement() {
employees = new ArrayList<>();
}
// 添加员工
public void addEmployee(Employee employee) {
employees.add(employee);
}
// 删除员工
public void deleteEmployee(Employee employee) {
employees.remove(employee);
}
// 修改员工
public void updateEmployee(Employee oldEmployee, Employee newEmployee) {
int index = employees.indexOf(oldEmployee);
employees.set(index, newEmployee);
}
// 查询员工
public List<Employee> queryEmployee(Predicate<Employee> predicate) {
return employees.stream()
.filter(predicate)
.collect(Collectors.toList());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("EmployeeManagement{\n");
for (Employee employee : employees) {
sb.append("\t").append(employee).append("\n");
}
sb.append("}");
return sb.toString();
}
}
```
4) 查询员工示例:
```
// 根据姓名查询
List<Employee> employeesByName = employeeManagement.queryEmployee(e -> e.getName().equals("张三"));
// 根据年龄段查询
List<Employee> employeesByAgeRange = employeeManagement.queryEmployee(e -> e.getAge() >= 20 && e.getAge() <= 30);
// 根据薪资查询
List<Employee> employeesBySalary = employeeManagement.queryEmployee(e -> e.getSalary() > 5000);
// 根据部门查询
List<Employee> employeesByDepartment = employeeManagement.queryEmployee(e -> e.getDepartment() == Department.JAVA);
```
5) 统计员工信息示例:
```
// 统计最高工资的员工
Optional<Employee> maxSalaryEmployeeOp = employeeManagement.queryEmployee(e -> true)
.stream()
.max(Comparator.comparingDouble(Employee::getSalary));
if (maxSalaryEmployeeOp.isPresent()) {
Employee maxSalaryEmployee = maxSalaryEmployeeOp.get();
System.out.println("最高工资的员工:" + maxSalaryEmployee);
}
// 统计最低薪资的员工
Optional<Employee> minSalaryEmployeeOp = employeeManagement.queryEmployee(e -> true)
.stream()
.min(Comparator.comparingDouble(Employee::getSalary));
if (minSalaryEmployeeOp.isPresent()) {
Employee minSalaryEmployee = minSalaryEmployeeOp.get();
System.out.println("最低薪资的员工:" + minSalaryEmployee);
}
// 根据部门统计员工
Map<Department, Long> departmentEmployeeCount = employeeManagement.queryEmployee(e -> true)
.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
System.out.println("各部门员工数量:" + departmentEmployeeCount);
```
完整代码如下:
```
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class EmployeeManagement {
private List<Employee> employees;
public EmployeeManagement() {
employees = new ArrayList<>();
}
// 添加员工
public void addEmployee(Employee employee) {
employees.add(employee);
}
// 删除员工
public void deleteEmployee(Employee employee) {
employees.remove(employee);
}
// 修改员工
public void updateEmployee(Employee oldEmployee, Employee newEmployee) {
int index = employees.indexOf(oldEmployee);
employees.set(index, newEmployee);
}
// 查询员工
public List<Employee> queryEmployee(Predicate<Employee> predicate) {
return employees.stream()
.filter(predicate)
.collect(Collectors.toList());
}
// 统计最高工资的员工、最低薪资的员工、根据部门统计员工
public void statisticsEmployee() {
// 统计最高工资的员工
Optional<Employee> maxSalaryEmployeeOp = employeeManagement.queryEmployee(e -> true)
.stream()
.max(Comparator.comparingDouble(Employee::getSalary));
if (maxSalaryEmployeeOp.isPresent()) {
Employee maxSalaryEmployee = maxSalaryEmployeeOp.get();
System.out.println("最高工资的员工:" + maxSalaryEmployee);
}
// 统计最低薪资的员工
Optional<Employee> minSalaryEmployeeOp = employeeManagement.queryEmployee(e -> true)
.stream()
.min(Comparator.comparingDouble(Employee::getSalary));
if (minSalaryEmployeeOp.isPresent()) {
Employee minSalaryEmployee = minSalaryEmployeeOp.get();
System.out.println("最低薪资的员工:" + minSalaryEmployee);
}
// 根据部门统计员工
Map<Department, Long> departmentEmployeeCount = employeeManagement.queryEmployee(e -> true)
.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
System.out.println("各部门员工数量:" + departmentEmployeeCount);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("EmployeeManagement{\n");
for (Employee employee : employees) {
sb.append("\t").append(employee).append("\n");
}
sb.append("}");
return sb.toString();
}
public static void main(String[] args) {
EmployeeManagement employeeManagement = new EmployeeManagement();
// 添加员工
employeeManagement.addEmployee(new Employee("张三", 25, 6000, Department.JAVA));
employeeManagement.addEmployee(new Employee("李四", 30, 8000, Department.HTML));
employeeManagement.addEmployee(new Employee("王五", 20, 5000, Department.JAVA));
System.out.println(employeeManagement);
// 删除员工
Employee employee = new Employee("张三", 25, 6000, Department.JAVA);
employeeManagement.deleteEmployee(employee);
System.out.println(employeeManagement);
// 修改员工
Employee oldEmployee = new Employee("李四", 30, 8000, Department.HTML);
Employee newEmployee = new Employee("李四", 30, 9000, Department.PYTHON);
employeeManagement.updateEmployee(oldEmployee, newEmployee);
System.out.println(employeeManagement);
// 查询员工
List<Employee> employeesByName = employeeManagement.queryEmployee(e -> e.getName().equals("李四"));
System.out.println("姓名为李四的员工:" + employeesByName);
List<Employee> employeesByAgeRange = employeeManagement.queryEmployee(e -> e.getAge() >= 20 && e.getAge() <= 30);
System.out.println("年龄在20~30岁之间的员工:" + employeesByAgeRange);
List<Employee> employeesBySalary = employeeManagement.queryEmployee(e -> e.getSalary() > 5000);
System.out.println("薪资大于5000的员工:" + employeesBySalary);
List<Employee> employeesByDepartment = employeeManagement.queryEmployee(e -> e.getDepartment() == Department.JAVA);
System.out.println("JAVA部门的员工:" + employeesByDepartment);
// 统计员工信息
employeeManagement.statisticsEmployee();
}
}
enum Department {
JAVA,
HTML,
PYTHON
}
class Employee {
private String name;
private int age;
private double salary;
private Department department;
public Employee(String name, int age, double salary, Department department) {
this.name = name;
this.age = age;
this.salary = salary;
this.department = department;
}
// getter和setter方法略
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", department=" + department +
'}';
}
}
```
阅读全文