为不同类型的员工实施不同的谓词

时间:2018-12-04 21:11:53

标签: java

我不熟悉谓词,不知道自己是否正确理解谓词。我有一个抽象的employee类,其中每小时和薪水员工的薪水分别创建。我的问题取决于我的EmployeePredicate.java类,在该类中,我不确定如何检查它是否是一个小时工并返回true或false。

我需要为以下所有条件创建一个不同的谓词:

  

所有员工,仅按小时,仅薪水和仅全职。

到目前为止,我只想让“仅小时”谓词首先正常工作,然后认为我可以弄清楚其余部分。我不确定在“ p”后面放什么以检查它是哪种类型的员工。我目前拥有的是:

public static Predicate<Employee> isHourlyEmployee() {
        return p -> p.
    }

我也有语句double avgSalary = calculateAveragePay(employees, null);,并且不确定用null替换什么,因为它应该是基于我上面的calculateAveragePay函数的谓词。

Main.java

package homework04;

import java.util.function.Predicate;

public class Main {

public static double calculateAveragePay(Employee[] employees, Predicate<Employee> pred) {
    double sum = 0.0;
    int count = 0;
    for(Employee e : employees) {
        if(!pred.test(e)) {             
            continue;
        }
        sum += e.calculatePay();
        count++;
    }
    return sum / count;
}

public static void main(String[] args) {
    //The list of employees to calculate.
    Employee[] employees = {
        new HourlyEmployee("John Smith", 80, 18.00),
        new HourlyEmployee("Jane Doe", 77, 20.00),
        new SalaryEmployee("Bob Miller", 85, 40000.00),
        new HourlyEmployee("Alice Davis", 40, 12.50),
        new SalaryEmployee("Frank Frink", 70, 35000.00),
        new HourlyEmployee("Chris Williams", 95, 25.00)
    };

    //The average pay for both types of employee.
    double avgSalary = calculateAveragePay(employees, null);
    double avgHourly = calculateAveragePay(employees, null);

    //The bonus to be added to employee pay.
    //double bonus = Math.abs(avgSalary - avgHourly);

    //Print the average pay
    System.out.println("===== Average Pay =====");

}

}

Employee.java

package homework04;

import java.util.function.Predicate;

abstract class Employee {

    private String name;
    private int hoursWorked;

    public Employee(String name, int hoursWorked) {
        this.name = name;
        this.hoursWorked = hoursWorked;
    }

    public int getHoursWorked() {
        return hoursWorked;
    }

    public String getName() {
        return name;
    }

    public abstract double calculatePay();

}

HourlyEmployee.java

package homework04;

public class HourlyEmployee extends Employee {

    private double hourlyPay;

    public HourlyEmployee(String name, int hoursWorked, double hourlyPay) {
        super(name, hoursWorked);
        this.hourlyPay = hourlyPay;
    }

    @Override
    public double calculatePay() {
        return getHoursWorked() * hourlyPay;
    }

}

SalaryEmployee.java

package homework04;

public class SalaryEmployee extends Employee {

    private static final int NUM_PAY_PERIODS = 26;

    private double salary;

    public SalaryEmployee(String name, int hoursWorked, double salary) {
        super(name, hoursWorked);
        this.salary = salary;
    }

    @Override
    public double calculatePay() {
        return salary / NUM_PAY_PERIODS;
    }

}

EmployeePredicate.java

package homework04;

import java.util.function.Predicate;

public class EmployeePredicate {

    public static Predicate<Employee> isHourlyEmployee() {
        return p -> p.
    }

}

1 个答案:

答案 0 :(得分:2)

您正在寻找:

 return p -> p instanceof HourlyEmployee;

但是我不建议在Employee工厂类中为每种EmployeePredicate类型创建谓词的方法,而只是在调用calculateAveragePay方法时传递行为,即/ p>

double avgSalary = calculateAveragePay(employees, p -> p instanceof SalaryEmployee);
double avgHourly = calculateAveragePay(employees, p -> p instanceof HourlyEmployee);

但是,如果您希望继续使用工厂类的Predicate方法,因为您认为它提供了更好的可读性,则可以执行以下操作:

public class EmployeePredicate {
    public static Predicate<Employee> isHourlyEmployee() {
        return p -> p instanceof HourlyEmployee;
    }
}

然后该方法对calculateAveragePay的调用变为:

double avgSalary = calculateAveragePay(employees, EmployeePredicate.isSalaryEmployee()); // create the isSalaryEmployee method
double avgHourly = calculateAveragePay(employees, EmployeePredicate.isHourlyEmployee());

顺便说一句,您可以使用流API执行calculateAveragePay,使其更具可读性。

public static double calculateAveragePay(Employee[] employees, Predicate<Employee> pred) {
    return Arrays.stream(employees)
                 .filter(pred)
                 .mapToDouble(e -> e.calculatePay()) 
                 .average().orElse(0);
}