用Java实现为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数
时间: 2024-02-03 14:13:27 浏览: 77
据,分别存储不同类型的员工信息,并计算他们的月薪。
以下是Java代码实现:
```java
public abstract class Employee {
private String employeeId;
private String name;
private int birthYear;
public Employee(String employeeId, String name, int birthYear) {
this.employeeId = employeeId;
this.name = name;
this.birthYear = birthYear;
}
public String getEmployeeId() {
return employeeId;
}
public String getName() {
return name;
}
public int getBirthYear() {
return birthYear;
}
public abstract double getMonthlySalary();
}
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String employeeId, String name, int birthYear, double monthlySalary) {
super(employeeId, name, birthYear);
this.monthlySalary = monthlySalary;
}
@Override
public double getMonthlySalary() {
return monthlySalary;
}
}
public class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String employeeId, String name, int birthYear, double hourlyWage, double hoursWorked) {
super(employeeId, name, birthYear);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
@Override
public double getMonthlySalary() {
if (hoursWorked <= 160) {
return hourlyWage * hoursWorked;
} else {
return hourlyWage * 160 + hourlyWage * 1.5 * (hoursWorked - 160);
}
}
}
public class CommissionEmployee extends Employee {
private double commissionRate;
private double grossSales;
public CommissionEmployee(String employeeId, String name, int birthYear, double commissionRate, double grossSales) {
super(employeeId, name, birthYear);
this.commissionRate = commissionRate;
this.grossSales = grossSales;
}
@Override
public double getMonthlySalary() {
return commissionRate * grossSales;
}
}
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String employeeId, String name, int birthYear, double commissionRate, double grossSales, double baseSalary) {
super(employeeId, name, birthYear, commissionRate, grossSales);
this.baseSalary = baseSalary;
}
@Override
public double getMonthlySalary() {
return super.getMonthlySalary() + baseSalary;
}
}
public class PayrollSystem {
public static void main(String[] args) {
SalariedEmployee salariedEmployee = new SalariedEmployee("001", "John Doe", 1980, 5000);
HourlyEmployee hourlyEmployee = new HourlyEmployee("002", "Jane Smith", 1985, 20, 180);
CommissionEmployee commissionEmployee = new CommissionEmployee("003", "Bob Johnson", 1975, 0.1, 10000);
BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("004", "Mary Brown", 1990, 0.08, 12000, 3000);
Employee[] employees = {salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee};
for (Employee employee : employees) {
System.out.println("Employee ID: " + employee.getEmployeeId());
System.out.println("Name: " + employee.getName());
System.out.println("Birth Year: " + employee.getBirthYear());
System.out.println("Monthly Salary: " + employee.getMonthlySalary());
System.out.println();
}
}
}
```
在主函数中,我们创建了四个不同类型的员工,并将它们存储在一个数组中,然后使用循环遍历数组,并输出每个员工的信息和月薪。
阅读全文