为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100月作为红包。 Java
时间: 2024-02-03 09:14:37 浏览: 64
以下是一个针对该需求的Java代码示例:
```java
import java.time.LocalDate;
public class PayrollSystem {
public static void main(String[] args) {
// 创建员工数组,包含不同类型的员工对象
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("123", "Tom", LocalDate.of(1990, 3, 10), 5000);
employees[1] = new HourlyEmployee("456", "Jerry", LocalDate.of(1995, 5, 15), 35, 100);
employees[2] = new CommissionEmployee("789", "Lucy", LocalDate.of(1985, 8, 20), 10000, 0.1);
employees[3] = new BasePlusCommissionEmployee("101", "Bob", LocalDate.of(1992, 10, 1), 8000, 0.15, 2000);
// 遍历员工数组,显示每个员工的信息和月收入
for (Employee employee : employees) {
System.out.println("Employee ID: " + employee.getId());
System.out.println("Employee Name: " + employee.getName());
System.out.println("Employee Birthday: " + employee.getBirthDate());
double earnings = employee.earnings();
System.out.println("Employee Monthly Income: " + earnings);
if (employee.isBirthdayMonth()) {
System.out.println("Happy Birthday! You got a 100 yuan red envelope!");
earnings += 100;
System.out.println("Employee Monthly Income with Red Envelope: " + earnings);
}
System.out.println();
}
}
}
abstract class Employee {
private String id;
private String name;
private LocalDate birthDate;
public Employee(String id, String name, LocalDate birthDate) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public LocalDate getBirthDate() {
return birthDate;
}
// 判断当月是否是员工生日所在月份
public boolean isBirthdayMonth() {
LocalDate now = LocalDate.now();
return now.getMonthValue() == birthDate.getMonthValue();
}
// 计算员工月收入,具体实现在各个子类中
public abstract double earnings();
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String id, String name, LocalDate birthDate, double monthlySalary) {
super(id, name, birthDate);
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String id, String name, LocalDate birthDate, double hourlyWage, double hoursWorked) {
super(id, name, birthDate);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
@Override
public double earnings() {
double basePay = hourlyWage * hoursWorked;
if (hoursWorked > 160) {
double overtimePay = (hoursWorked - 160) * hourlyWage * 0.5;
return basePay + overtimePay;
} else {
return basePay;
}
}
}
class CommissionEmployee extends Employee {
private double grossSales;
private double commissionRate;
public CommissionEmployee(String id, String name, LocalDate birthDate, double grossSales, double commissionRate) {
super(id, name, birthDate);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
@Override
public double earnings() {
return grossSales * commissionRate;
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String id, String name, LocalDate birthDate, double grossSales, double commissionRate, double baseSalary) {
super(id, name, birthDate, grossSales, commissionRate);
this.baseSalary = baseSalary;
}
@Override
public double earnings() {
return super.earnings() + baseSalary;
}
}
```
该程序中,Employee类是所有员工类型的抽象父类,其中定义了员工的基本信息和方法。四个具体员工类型分别继承自Employee,并实现了各自特有的earnings方法,用于计算员工月收入。
PayrollSystem类是程序的入口,创建了一个Employee数组,包含了不同类型的员工对象,然后遍历数组,显示每个员工的信息和月收入,如果当月是员工的生日所在的月份,则还另发给员工100元作为红包。
阅读全文