显示数组列表中的所有元素<superclass>随工资增加



我有一个抽象类Staff有一个属性系数(工资)。我有两个类Employee有自己的属性是overTimes和Manager有一个属性是title从Staff扩展和实现接口ICalculate有一个函数calculateSalary()。所有对象从员工和经理创建,我添加到数组列表listofstaff。那么在我计算完工资之后,我该如何安排员工随工资上升呢?谢谢你!这是我的Staff类

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;
}
}

这是我的Employee类

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;
}
}

这是我的Manager类

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

现在,由于Employee类和Manager类是Staff的子类,它们也实现了接口ICaculator,并且由于它们不是抽象类,它们必须实现接口方法calculateSalary(它们在您的问题中的代码中这样做)。

现在您可以创建一个包含Employee对象和Manager对象的ICaculator对象列表。排序列表的一种方法是实现接口Comparator。下面的代码演示了如何创建ListComparator,并对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);
}
}

相关内容

  • 没有找到相关文章

最新更新