为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100元作为红包
时间: 2024-02-05 22:10:23 浏览: 107
工资计算系统
4星 · 用户满意度95%
以下是Java代码实现:
```java
import java.util.Calendar;
public class PayrollSystemTest {
public static void main(String[] args) {
// 创建Employee对象数组
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("001", "John", "1990-01-01", 8000);
employees[1] = new HourlyEmployee("002", "Tom", "1995-05-20", 20, 180);
employees[2] = new CommissionEmployee("003", "Lucy", "1992-06-30", 10000, 0.08);
employees[3] = new BasePlusCommissionEmployee("004", "Mike", "1985-11-15", 8000, 0.06, 2000);
// 获取当前月份
int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
// 遍历数组,计算月薪并输出信息
for (Employee employee : employees) {
System.out.println(employee.toString() +
", Monthly Salary: " + employee.earnings());
if (currentMonth == employee.getBirthdayMonth()) {
System.out.println("Happy Birthday! You got a red packet of 100 yuan.");
}
}
}
}
class Employee {
private String id;
private String name;
private String birthDate;
public Employee(String id, String name, String birthDate) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public int getBirthdayMonth() {
String[] dateArr = birthDate.split("-");
return Integer.parseInt(dateArr[1]);
}
public double earnings() {
return 0;
}
@Override
public String toString() {
return "Employee{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", birthDate='" + birthDate + '\'' +
'}';
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String id, String name, String birthDate, double monthlySalary) {
super(id, name, birthDate);
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
@Override
public String toString() {
return "SalariedEmployee{" +
"monthlySalary=" + monthlySalary +
"} " + super.toString();
}
}
class HourlyEmployee extends Employee {
private double wage;
private double hours;
public HourlyEmployee(String id, String name, String birthDate, double wage, double hours) {
super(id, name, birthDate);
this.wage = wage;
this.hours = hours;
}
@Override
public double earnings() {
if (hours <= 160) {
return wage * hours;
} else {
return wage * 160 + wage * 1.5 * (hours - 160);
}
}
@Override
public String toString() {
return "HourlyEmployee{" +
"wage=" + wage +
", hours=" + hours +
"} " + super.toString();
}
}
class CommissionEmployee extends Employee {
private double grossSales;
private double commissionRate;
public CommissionEmployee(String id, String name, String birthDate, double grossSales, double commissionRate) {
super(id, name, birthDate);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
@Override
public double earnings() {
return grossSales * commissionRate;
}
@Override
public String toString() {
return "CommissionEmployee{" +
"grossSales=" + grossSales +
", commissionRate=" + commissionRate +
"} " + super.toString();
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;
public BasePlusCommissionEmployee(String id, String name, String birthDate, double grossSales, double commissionRate, double baseSalary) {
super(id, name, birthDate, grossSales, commissionRate);
this.baseSalary = baseSalary;
}
@Override
public double earnings() {
return super.earnings() + baseSalary;
}
@Override
public String toString() {
return "BasePlusCommissionEmployee{" +
"baseSalary=" + baseSalary +
"} " + super.toString();
}
}
```
代码中,Employee为父类,SalariedEmployee、HourlyEmployee、CommissionEmployee和BasePlusCommissionEmployee为其子类。每个子类都重写了父类的earnings方法,以计算不同类型员工的月薪。在PayrollSystemTest中创建Employee对象数组,遍历数组并调用对象方法输出信息,如果当月是员工的生日所在的月份,则输出祝福语。
阅读全文