用java为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100月作为红包。
时间: 2024-02-17 13:02:23 浏览: 172
好的,这是用Java实现的工资支付系统:
```java
import java.time.LocalDate;
abstract class Employee {
private int empId;
private String name;
private LocalDate birthdate;
public Employee(int empId, String name, LocalDate birthdate) {
this.empId = empId;
this.name = name;
this.birthdate = birthdate;
}
public int getEmpId() {
return empId;
}
public String getName() {
return name;
}
public LocalDate getBirthdate() {
return birthdate;
}
public abstract double getMonthlySalary();
public double getBonus() {
LocalDate today = LocalDate.now();
if (today.getMonth() == birthdate.getMonth()) {
return 100;
} else {
return 0;
}
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(int empId, String name, LocalDate birthdate, double monthlySalary) {
super(empId, name, birthdate);
this.monthlySalary = monthlySalary;
}
@Override
public double getMonthlySalary() {
return monthlySalary + getBonus();
}
}
class HourlyEmployee extends Employee {
private double hourlyRate;
private double hoursWorked;
public HourlyEmployee(int empId, String name, LocalDate birthdate, double hourlyRate, double hoursWorked) {
super(empId, name, birthdate);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
@Override
public double getMonthlySalary() {
if (hoursWorked > 160) {
double overtimePay = (hoursWorked - 160) * (hourlyRate * 1.5);
double monthlySalary = (160 * hourlyRate) + overtimePay;
return monthlySalary + getBonus();
} else {
return hoursWorked * hourlyRate + getBonus();
}
}
}
class CommissionEmployee extends Employee {
private double commissionRate;
private double sales;
public CommissionEmployee(int empId, String name, LocalDate birthdate, double commissionRate, double sales) {
super(empId, name, birthdate);
this.commissionRate = commissionRate;
this.sales = sales;
}
@Override
public double getMonthlySalary() {
return sales * commissionRate + getBonus();
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(int empId, String name, LocalDate birthdate, double commissionRate, double sales, double baseSalary) {
super(empId, name, birthdate, commissionRate, sales);
this.baseSalary = baseSalary;
}
@Override
public double getMonthlySalary() {
return baseSalary + super.getMonthlySalary();
}
}
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = {
new SalariedEmployee(1, "张三", LocalDate.of(1980, 1, 1), 10000),
new HourlyEmployee(2, "李四", LocalDate.of(1990, 2, 2), 50, 180),
new CommissionEmployee(3, "王五", LocalDate.of(1985, 3, 3), 0.1, 50000),
new BasePlusCommissionEmployee(4, "赵六", LocalDate.of(1995, 4, 4), 0.1, 40000, 5000)
};
for (Employee emp : employees) {
System.out.println("工号:" + emp.getEmpId());
System.out.println("姓名:" + emp.getName());
System.out.println("出生年月:" + emp.getBirthdate());
System.out.println("月收入:" + emp.getMonthlySalary());
System.out.println("红包:" + emp.getBonus());
System.out.println("----------------------");
}
}
}
```
这段代码中,我们定义了一个抽象类 `Employee`,它有一个抽象方法 `getMonthlySalary` 用于计算员工的月薪,以及一个 `getBonus` 方法用于计算生日红包。然后我们定义了四个具体类,分别对应于四种不同类型的员工,它们都继承自 `Employee`,并实现了 `getMonthlySalary` 方法。最后我们创建了一个 `employees` 数组,保存了所有员工的信息,遍历这个数组,并依次调用每个员工的方法来显示工号、姓名、出生年月、月收入和红包。
阅读全文