员工类
public abstract class Employee
{
//Class Constants
public static final double MINIMUM_HOURLY_WAGE = 8.00;
public static final int MAX_HOURS_WORK_WEEK = 40;
public static final int MAX_HOURS_WORK_DAY = 8;
//Instance Variables
private final String number; //ID number
private double payRate; //Pay-rate
//Constructor
public Employee(double payRate)
{
this.number = newNumber();
this.payRate = payRate;
}
//Parameter-less constructor **** TEMPORARY ****
//TO BE DELETED when subclass constructors completed
public Employee()
{
this(0.0);
}
//Accessor
public String getNumber()
{
return this.number;
}
//Accessor
public double getPayRate()
{
return this.payRate;
}
//For implementation in subclasses
public abstract double grossPay();
//Override
public String toString()
{
String category = this.getClass().getName();
category = category.substring(0, category.indexOf("Employee"));
return category + " " + super.toString();
}
//Override
public boolean equals(Object other)
{
return super.equals(other);
}
//Helper: Create unique Employee number in the form xxx-xx-xx
private static String newNumber()
{
return "xxx-xx-xx";
}
}
SalariedEmployee Class
public abstract class SalariedEmployee extends Employee
{
public static final double MINIMUM_SALARY =
Employee.MINIMUM_HOURLY_WAGE *
Employee.MAX_HOURS_WORK_WEEK ;
public SalariedEmployee()
{
payRate = MINIMUM_SALARY ;
}
public abstract double grossPay();
}
我对这个类唯一的问题是它的构造函数,因为指令说要通过它的构造函数强制执行最低工资约束但我不是那个意思(我只是有人为我澄清)
HourlyWageEmployee Class
public abstract class HourlyWageEmployee extends Employee
{
//Instance Variable
private TimeCard hours;
public HourlyWageEmployee()
{
super(MINIMUM_HOURLY_WAGE);
hours = new TimeCard();
}
public TimeCard getHours()
{
return hours;
}
public void setHours(TimeCard hours)
{
this.hours = hours;
}
public abstract double overtime();
public double grossPay()
{
return (this.getPayRate()) + overtime();
}
}
我的类问题是TimeCard类型的实例变量“hours”我无法确定如何使用从TimeCard类中检索小时数,因为在该类课时是一个数组。因为小时数是一个数组我无法将其与支付比较或将其包含在加班和支付方式的任何计算中,因为它们是两种不同的类型
注意:这两个子类每个都有两个子类,我会编辑这个问题,以便在必要时包含那些和TimeCard
答案 0 :(得分:0)
在SalariedEmployee
中使用
public SalariedEmployee(double payRate)
{
super(payRate < MINIMUM_SALARY ? MINIMUM_SALARY : payRate);
}
这样,如果payRate
小于最低工资,则将其替换为最低工资。
答案 1 :(得分:0)
SalariedEmployee无法访问payRate,因为payRate在Employee上是私有的。
你应该将payRate更改为protected modifer