用java 为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费)
时间: 2024-02-09 21:11:49 浏览: 277
好的,这是一个具体的问题,我可以为您解答。首先,我们需要设计一个类层次结构来表示这四类员工,然后为每个类编写一个计算月薪的方法。
我们可以先定义一个抽象类Employee,它有一个抽象方法calculatePay()用于计算月薪。然后派生出四个具体的子类:SalariedEmployee、HourlyEmployee、CommissionedEmployee、BasePlusCommissionedEmployee。这四个子类分别表示领固定月薪、计时取酬、按销售额提成、底薪加销售提成的员工。
下面是一个示例的类定义:
```java
abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
this.name = name;
this.address = address;
this.number = number;
}
public abstract double calculatePay();
// getters and setters
}
class SalariedEmployee extends Employee {
private double salary;
public SalariedEmployee(String name, String address, int number, double salary) {
super(name, address, number);
this.salary = salary;
}
public double calculatePay() {
return salary;
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String name, String address, int number, double hourlyWage, double hoursWorked) {
super(name, address, number);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
public double calculatePay() {
double basePay = hourlyWage * hoursWorked;
if (hoursWorked > 160) {
double overtimePay = (hoursWorked - 160) * hourlyWage * 0.5;
return basePay + overtimePay;
} else {
return basePay;
}
}
}
class CommissionedEmployee extends Employee {
private double sales;
private double commissionRate;
public CommissionedEmployee(String name, String address, int number, double sales, double commissionRate) {
super(name, address, number);
this.sales = sales;
this.commissionRate = commissionRate;
}
public double calculatePay() {
return sales * commissionRate;
}
}
class BasePlusCommissionedEmployee extends CommissionedEmployee {
private double baseSalary;
public BasePlusCommissionedEmployee(String name, String address, int number, double sales, double commissionRate, double baseSalary) {
super(name, address, number, sales, commissionRate);
this.baseSalary = baseSalary;
}
public double calculatePay() {
return super.calculatePay() + baseSalary;
}
}
```
对于HourlyEmployee类,我们在calculatePay()方法中判断工作小时数是否超过160,如果超过则计算加班费,否则只计算基本工资。其他三个子类的计算月薪方法比较简单,直接按照题目要求计算即可。
我们还需要一个测试程序来测试这些类的功能:
```java
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("Alice", "123 Main St", 1, 1000);
employees[1] = new HourlyEmployee("Bob", "456 Oak Ave", 2, 20, 180);
employees[2] = new CommissionedEmployee("Charlie", "789 Elm St", 3, 5000, 0.05);
employees[3] = new BasePlusCommissionedEmployee("Dave", "101 Maple Ave", 4, 10000, 0.03, 2000);
for (Employee e : employees) {
System.out.println("Name: " + e.getName() + ", Pay: " + e.calculatePay());
}
}
}
```
这个测试程序创建了四个不同类型的员工对象,并调用它们的calculatePay()方法计算月薪。运行结果如下:
```
Name: Alice, Pay: 1000.0
Name: Bob, Pay: 3800.0
Name: Charlie, Pay: 250.0
Name: Dave, Pay: 500.0
```
这个结果符合预期,说明我们的工资支付系统设计正确。
阅读全文