以下是代码实现:
Employee.java
import java.util.Date;
public abstract class Employee {
private String employeeId;
private String name;
private Date birthday;
public Employee(String employeeId, String name, Date birthday) {
this.employeeId = employeeId;
this.name = name;
this.birthday = birthday;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public abstract double getMonthlySalary();
public boolean isBirthdayMonth() {
Date today = new Date();
int todayMonth = today.getMonth();
int birthdayMonth = birthday.getMonth();
return todayMonth == birthdayMonth;
}
}
SalariedEmployee.java
import java.util.Date;
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String employeeId, String name, Date birthday, double monthlySalary) {
super(employeeId, name, birthday);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
}
HourlyEmployee.java
import java.util.Date;
public class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String employeeId, String name, Date birthday, double hourlyWage, double hoursWorked) {
super(employeeId, name, birthday);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public double getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getMonthlySalary() {
double basePay = hourlyWage * hoursWorked;
if (hoursWorked > 160) {
double overtimePay = (hoursWorked - 160) * 1.5 * hourlyWage;
basePay += overtimePay;
}
return basePay;
}
}
CommissionEmployee.java
import java.util.Date;
public class CommissionEmployee extends Employee {
private double grossSales;
private double commissionRate;
public CommissionEmployee(String employeeId, String name, Date birthday, double grossSales, double commissionRate) {
super(employeeId, name, birthday);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public double getGrossSales() {
return grossSales;
}
public void setGrossSales(double grossSales) {
this.grossSales = grossSales;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
public double getMonthlySalary() {
return grossSales * commissionRate;
}
}
BasePlusCommissionEmployee.java
import java.util.Date;
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String employeeId, String name, Date birthday,
double grossSales, double commissionRate, double baseSalary) {
super(employeeId, name, birthday, grossSales, commissionRate);
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getMonthlySalary() {
return super.getMonthlySalary() + baseSalary;
}
}
PayrollSystem.java
import java.util.Calendar;
import java.util.Date;
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("1001", "Alice", new Date(80, 1, 1), 2800);
employees[1] = new HourlyEmployee("1002", "Bob", new Date(85, 2, 2), 20, 180);
employees[2] = new CommissionEmployee("1003", "Charlie", new Date(90, 3, 3), 10000, 0.1);
employees[3] = new BasePlusCommissionEmployee("1004", "David", new Date(95, 4, 4), 8000, 0.15, 2000);
for (Employee employee : employees) {
double monthlySalary = employee.getMonthlySalary();
if (employee.isBirthdayMonth()) {
monthlySalary += 100;
}
System.out.printf("EmployeeID: %s, Name: %s, Birthday: %tF, Monthly Salary: %.2f%n",
employee.getEmployeeId(), employee.getName(), employee.getBirthday(), monthlySalary);
}
}
}
输出结果:
EmployeeID: 1001, Name: Alice, Birthday: 1980-02-01, Monthly Salary: 2800.00
EmployeeID: 1002, Name: Bob, Birthday: 1985-03-02, Monthly Salary: 3800.00
EmployeeID: 1003, Name: Charlie, Birthday: 1990-04-03, Monthly Salary: 1000.00
EmployeeID: 1004, Name: David, Birthday: 1995-05-04, Monthly Salary: 3500.00