为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100月作为红包。用java实现
时间: 2024-02-27 20:55:37 浏览: 62
好的,以下是用 Java 实现的工资支付系统:
```java
import java.util.Calendar;
public abstract class Employee {
private String id;
private String name;
private int birthMonth;
public Employee(String id, String name, int birthMonth) {
this.id = id;
this.name = name;
this.birthMonth = birthMonth;
}
public abstract double calculateMonthlySalary();
public void printEmployeeInfo() {
System.out.println("工号:" + id);
System.out.println("姓名:" + name);
System.out.println("出生年月:" + birthMonth);
System.out.println("月薪:" + calculateMonthlySalary());
}
public boolean isBirthdayMonth(int currentMonth) {
return birthMonth == currentMonth;
}
public double getBonus() {
if (isBirthdayMonth(Calendar.getInstance().get(Calendar.MONTH) + 1)) {
return 100.0;
} else {
return 0.0;
}
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String id, String name, int birthMonth, double monthlySalary) {
super(id, name, birthMonth);
this.monthlySalary = monthlySalary;
}
@Override
public double calculateMonthlySalary() {
return monthlySalary + getBonus();
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private double workingHours;
public HourlyEmployee(String id, String name, int birthMonth, double hourlyWage, double workingHours) {
super(id, name, birthMonth);
this.hourlyWage = hourlyWage;
this.workingHours = workingHours;
}
@Override
public double calculateMonthlySalary() {
double basicSalary = hourlyWage * workingHours;
double extraHours = workingHours - 160.0;
if (extraHours > 0) {
basicSalary += hourlyWage * extraHours * 1.5;
}
return basicSalary + getBonus();
}
}
class CommissionEmployee extends Employee {
private double commissionRate;
private double sales;
public CommissionEmployee(String id, String name, int birthMonth, double commissionRate, double sales) {
super(id, name, birthMonth);
this.commissionRate = commissionRate;
this.sales = sales;
}
@Override
public double calculateMonthlySalary() {
return commissionRate * sales + getBonus();
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String id, String name, int birthMonth, double commissionRate, double sales, double baseSalary) {
super(id, name, birthMonth, commissionRate, sales);
this.baseSalary = baseSalary;
}
@Override
public double calculateMonthlySalary() {
return super.calculateMonthlySalary() + baseSalary;
}
}
public class SalaryPaymentSystem {
public static void main(String[] args) {
Employee[] employees = {
new SalariedEmployee("001", "张三", 8, 10000.0),
new HourlyEmployee("002", "李四", 5, 50.0, 180.0),
new CommissionEmployee("003", "王五", 11, 0.1, 200000.0),
new BasePlusCommissionEmployee("004", "赵六", 3, 0.08, 150000.0, 5000.0)
};
for (Employee employee : employees) {
employee.printEmployeeInfo();
System.out.println();
}
}
}
```
使用上述代码可以创建一个工资支付系统,其中定义了四种员工类型,每个员工类型都实现了 `calculateMonthlySalary()` 方法以计算月薪,同时也实现了 `printEmployeeInfo()` 方法以打印员工信息。在 `Employee` 类中还实现了 `isBirthdayMonth()` 和 `getBonus()` 方法以计算红包。
在 `SalaryPaymentSystem` 类中,创建了一个 `Employee` 变量数组,保存了每个具体类对象的引用。通过遍历数组,打印每个员工的信息。如果当月是员工的生日所在的月份,则还会另发给他100元作为红包。
阅读全文