java - 显示 ArrayList 中的所有元素随工资增长
问题描述
我有一个抽象类 Staff 有一个属性系数(工资)。我有两个类 Employee 有自己的属性是 overTimes,Manager 有一个属性是从 Staff 扩展的 title 并实现接口 ICalculate 有一个函数 calculateSalary()。从员工和经理创建的所有对象,我添加到 ArrayList listOfStaffs。那么在我计算工资后,我该如何安排员工随着工资上升呢?谢谢这是我的员工课
public abstract class Staff {
private double staffCoefficient;
public Staff(double staffCoefficient) {
super();
this.staffCoefficient = staffCoefficient;
}
public double getStaffCoefficient() {
return staffCoefficient;
}
public void setStaffCoefficient(double staffCoefficient) {
this.staffCoefficient = staffCoefficient;
}
}
这是我的员工班
public class Employee extends Staff implements ICaculator {
private int overTimes;
public Employee(double staffCoefficient) {
super(staffCoefficient);
// TODO Auto-generated constructor stub
}
public Employee(double staffCoefficient, int overTimes) {
super(staffCoefficient);
this.overTimes = overTimes;
}
public int getOverTimes() {
return overTimes;
}
public void setOverTimes(int overTimes) {
this.overTimes = overTimes;
}
@Override
public BigDecimal calculateSalary() {
double salary = super.getStaffCoefficient() * 3000000 + getOverTimes() * 200000;
BigDecimal bigSalary = new BigDecimal(salary);
return bigSalary;
}
}
这是我的经理课
public class Manager extends Staff implements ICaculator {
private String title;
public Manager(double staffCoefficient) {
super(staffCoefficient);
}
public Manager(double staffCoefficient, String title) {
super(staffCoefficient);
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public BigDecimal calculateSalary() {
Double salary = super.getStaffCoefficient() * 5000000;
if (getTitle().contentEquals("Business Leader")) {
salary += 8000000;
BigDecimal bigSalary = new BigDecimal(salary);
return bigSalary;
} else if (getTitle().contentEquals("Project Leader")) {
salary += 5000000;
BigDecimal bigSalary = new BigDecimal(salary);
return bigSalary;
} else if (getTitle().contentEquals("Technical Leader")) {
salary += 6000000;
BigDecimal bigSalary = new BigDecimal(salary);
return bigSalary;
} else {
BigDecimal bigSalary = new BigDecimal(salary);
return bigSalary;
}
}
}
我可以显示所有员工的工资,但我不知道如何安排他们。请帮我。太感谢了。
解决方案
超类,即Staff
是抽象的。因此它可以声明它实现了一个接口而不包含实现接口方法的方法。换句话说,更改Staff
为以下内容:
public abstract class Staff implements ICaculator {
private double staffCoefficient;
public Staff(double staffCoefficient) {
super();
this.staffCoefficient = staffCoefficient;
}
public double getStaffCoefficient() {
return staffCoefficient;
}
public void setStaffCoefficient(double staffCoefficient) {
this.staffCoefficient = staffCoefficient;
}
}
请注意,我对您的代码所做的唯一更改是在这一行:
public abstract class Staff implements ICaculator
我补充说implements ICaculator
。
现在,由于 classEmployee
和 classManager
是的子类Staff
,它们也实现了接口ICaculator
,并且由于它们不是抽象类,它们必须实现接口方法calculateSalary
(它们在您问题的代码中执行)。
现在您可以创建一个包含对象和对象的对象列表。对列表进行排序的一种方法是实现接口Comparator。下面的代码演示了创建和排序。ICaculator
Employee
Manager
List
Comparator
List
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortStaffBySalaryTest {
public static void main(String[] args) {
Employee emp1 = new Employee(0.5, 10);
Employee emp2 = new Employee(0.75, 2);
Manager mgr1 = new Manager(0.8, "Boss");
Manager mgr2 = new Manager(0.9, "Big Boss");
List<ICaculator> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(mgr1);
list.add(mgr2);
Comparator<ICaculator> c = (s1, s2) -> {
BigDecimal salary1 = s1.calculateSalary();
BigDecimal salary2 = s2.calculateSalary();
return salary1.compareTo(salary2);
};
list.sort(c);
}
}
推荐阅读
- scala - VS Code 中的 Scala 和集成终端
- python - 通过 Flask 上传多个文件或整个文件夹
- css - 在 Angular 中处理 CSS
- php - Docker-compose 构建内存不足
- sql - SQL Server - 按外观替换字符串
- kubernetes - Kubernetes 部署失败,在 GKE 中出现“Failed create pod sandbox”
- spring - 如何在 Spring Boot 应用程序中为带有 @Configuration 注释的类编写单元测试用例
- php - 读取目录不在文件系统顺序中
- java - 使用 @SpringBootTest 时,@Autowired 对象为空
- windows - Flutter over企业代理