java编程:定义一个员工类,包含属性“姓名、性别、年龄、月基本工资、月绩效工资”,方法“构造方法、获得姓名、获得性别、获得年龄、获得月基本工资、获得月绩效工资、打印属性全部信息”。 定义一个统计类,包含一个静态方法“工资排序”,方法接收一个员工类数组,对数组中的所有员工按照月基本工资从小到大排序,如果基本工资相同,则按照绩效工资从小到大排序,最后返回排好序的员工数组。 定义一个测试类,在测试类中,创建包含不少于5个员工对象的数组,并且至少有2个员工基本工资相同。利用统计类对基本员工数组进行排序,最后将排好序的数组中员工姓名输出。
时间: 2023-02-14 17:48:30 浏览: 140
员工类:
```java
public class Employee {
private String name;
private String gender;
private int age;
private double basicSalary;
private double performanceSalary;
public Employee(String name, String gender, int age, double basicSalary, double performanceSalary) {
this.name = name;
this.gender = gender;
this.age = age;
this.basicSalary = basicSalary;
this.performanceSalary = performanceSalary;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public double getBasicSalary() {
return basicSalary;
}
public double getPerformanceSalary() {
return performanceSalary;
}
public void printInfo() {
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
System.out.println("月基本工资:" + basicSalary);
System.out.println("月绩效工资:" + performanceSalary);
}
}
```
统计类:
```java
import java.util.Arrays;
public class Statistics {
public static Employee[] salarySort(Employee[] employees) {
Arrays.sort(employees, (e1, e2) -> {
if (e1.getBasicSalary() != e2.getBasicSalary()) {
return Double.compare(e1.getBasicSalary(), e2.getBasicSalary());
} else {
return Double.compare(e1.getPerformanceSalary(), e2.getPerformanceSalary());
}
});
return employees;
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[] {
new Employee("张三", "男", 25, 3000, 2000),
new Employee("李四", "女", 22, 2500, 1500),
new Employee("王五", "男", 28, 2500, 2500),
new Employee("赵六", "女", 23, 3500, 1800),
new Employee("孙七", "男", 30, 2500, 1900)
};
employees = Statistics.salarySort(employees);
for (Employee employee : employees) {
System.out.println(employee.getName());
}
}
}
```
阅读全文