【问题标题】:display all element in ArrayList<superclass> increase with salary显示 ArrayList<superclass> 中的所有元素随薪水增加
【发布时间】:2021-11-29 23:50:51
【问题描述】:

我有一个抽象类 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;
}
}

这是我的 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;
}
}

这是我的经理类

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

我可以显示所有员工的工资,但我不知道如何安排他们。请帮我。非常感谢。

【问题讨论】:

    标签: java oop inheritance arraylist


    【解决方案1】:

    超类,即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 对象的List。对列表进行排序的一种方法是实现接口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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多