用Java实现为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班
时间: 2024-02-03 17:16:01 浏览: 154
费);销售提成员工(SalesEmployee,除了固定月薪外,还有销售提成);经理(Manager,除了固定月薪外,还有奖金)。以下是Java实现代码:
```java
abstract class Employee {
private String name;
private int age;
private String department;
public Employee(String name, int age, String department) {
this.name = name;
this.age = age;
this.department = department;
}
public abstract double getSalary();
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int age, String department, double monthlySalary) {
super(name, age, department);
this.monthlySalary = monthlySalary;
}
public double getSalary() {
return monthlySalary;
}
}
class HourlyEmployee extends Employee {
private double hourlyRate;
private double hoursWorked;
public HourlyEmployee(String name, int age, String department, double hourlyRate, double hoursWorked) {
super(name, age, department);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
public double getSalary() {
if (hoursWorked <= 160) {
return hourlyRate * hoursWorked;
} else {
return hourlyRate * 160 + hourlyRate * 1.5 * (hoursWorked - 160);
}
}
}
class SalesEmployee extends Employee {
private double monthlySalary;
private double commissionRate;
private double salesAmount;
public SalesEmployee(String name, int age, String department, double monthlySalary, double commissionRate, double salesAmount) {
super(name, age, department);
this.monthlySalary = monthlySalary;
this.commissionRate = commissionRate;
this.salesAmount = salesAmount;
}
public double getSalary() {
return monthlySalary + commissionRate * salesAmount;
}
}
class Manager extends Employee {
private double monthlySalary;
private double bonus;
public Manager(String name, int age, String department, double monthlySalary, double bonus) {
super(name, age, department);
this.monthlySalary = monthlySalary;
this.bonus = bonus;
}
public double getSalary() {
return monthlySalary + bonus;
}
}
```
在这个例子里,我们使用了抽象类来定义所有员工的通用属性和方法,然后通过继承来实现不同类型员工的特定属性和方法,最后通过调用各自的 `getSalary()` 方法来获得每个员工的月薪。
阅读全文