用jiana为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100月作为红包。
时间: 2024-02-17 17:02:44 浏览: 78
c++实现一个万年历
5星 · 资源好评率100%
好的,我理解了您的问题。下面是一个用 Java 语言实现的工资支付系统,该系统包含了 SalariedEmployee、HourlyEmployee、CommissionEmployee 和 BasePlusCommissionEmployee 四个类,并且实现了您的要求:
```java
import java.util.Calendar;
import java.util.GregorianCalendar;
abstract class Employee {
private String employeeId;
private String name;
private int birthYear;
private int birthMonth;
private int birthDay;
public Employee(String employeeId, String name, int birthYear, int birthMonth, int birthDay) {
this.employeeId = employeeId;
this.name = name;
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
}
public String getEmployeeId() {
return employeeId;
}
public String getName() {
return name;
}
public int getBirthYear() {
return birthYear;
}
public int getBirthMonth() {
return birthMonth;
}
public int getBirthDay() {
return birthDay;
}
public abstract double getMonthlySalary();
public void printEmployeeInfo() {
System.out.println("Employee ID: " + employeeId);
System.out.println("Name: " + name);
System.out.println("Birth Date: " + birthYear + "/" + birthMonth + "/" + birthDay);
System.out.println("Monthly Salary: " + getMonthlySalary());
if (isBirthday()) {
System.out.println("Happy Birthday! You get a red envelope of $100.");
}
System.out.println();
}
private boolean isBirthday() {
Calendar today = new GregorianCalendar();
int todayMonth = today.get(Calendar.MONTH) + 1;
return todayMonth == birthMonth;
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String employeeId, String name, int birthYear, int birthMonth, int birthDay, double monthlySalary) {
super(employeeId, name, birthYear, birthMonth, birthDay);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String employeeId, String name, int birthYear, int birthMonth, int birthDay, double hourlyWage, double hoursWorked) {
super(employeeId, name, birthYear, birthMonth, birthDay);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
public double getMonthlySalary() {
double overtimePay = 0;
if (hoursWorked > 160) {
overtimePay = (hoursWorked - 160) * hourlyWage * 1.5;
}
return hoursWorked * hourlyWage + overtimePay;
}
}
class CommissionEmployee extends Employee {
private double commissionRate;
private double grossSales;
public CommissionEmployee(String employeeId, String name, int birthYear, int birthMonth, int birthDay, double commissionRate, double grossSales) {
super(employeeId, name, birthYear, birthMonth, birthDay);
this.commissionRate = commissionRate;
this.grossSales = grossSales;
}
public double getMonthlySalary() {
return commissionRate * grossSales;
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String employeeId, String name, int birthYear, int birthMonth, int birthDay, double commissionRate, double grossSales, double baseSalary) {
super(employeeId, name, birthYear, birthMonth, birthDay, commissionRate, grossSales);
this.baseSalary = baseSalary;
}
public double getMonthlySalary() {
return super.getMonthlySalary() + baseSalary;
}
}
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("001", "Alice", 1980, 6, 1, 3000);
employees[1] = new HourlyEmployee("002", "Bob", 1985, 10, 15, 20, 180);
employees[2] = new CommissionEmployee("003", "Charlie", 1990, 5, 20, 0.15, 10000);
employees[3] = new BasePlusCommissionEmployee("004", "David", 1995, 8, 8, 0.1, 8000, 1500);
for (Employee employee : employees) {
employee.printEmployeeInfo();
}
}
}
```
在这个系统中,抽象类 `Employee` 定义了员工的基本属性和方法,其中 `getMonthlySalary()` 方法是抽象的,需要在具体子类中实现。四个具体子类分别实现了不同类型员工的月薪计算方法。
在 `main()` 方法中,我们创建了一个 `Employee` 类型的数组,存放了四个具体类型的员工对象。然后遍历这个数组,对每个员工调用 `printEmployeeInfo()` 方法,输出员工的基本信息和月收入,并判断是否为员工的生日月份,如果是则额外发放 $100 的红包。
阅读全文