java实现:BasePlusSalesEmployee四个类的对象各一个,并计算某个月这四个对象的工资注意: 要求把每个类都做成完全封装,不允许非私有化属性。类图如下 :
时间: 2023-02-05 17:52:42 浏览: 66
你可以用如下的代码来实现:
```java
import java.util.Scanner;
class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
public Employee(String firstName, String lastName, String socialSecurityNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public String toString() {
return String.format("%s %s%nsocial security number: %s", getFirstName(), getLastName(), getSocialSecurityNumber());
}
}
class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) {
super(firstName, lastName, socialSecurityNumber);
if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");
if (grossSales < 0.0) // validate
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public void setGrossSales(double grossSales) {
if (grossSales < 0.0) // validate
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales = grossSales;
}
public double getGrossSales() {
return grossSales;
}
public void setCommissionRate(double commissionRate) {
if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");
this.commissionRate = commissionRate;
}
public double getCommissionRate() {
return commissionRate;
}
public double earnings() {
return getCommissionRate() * getGrossSales();
}
public String toString() {
return String.format("%s: %s%n%s: $%,.2f; %s: %.2f", "commission employee", super.toString(), "gross sales", getGrossSales(), "commission rate", getCommissionRate());
}
}
class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary; // base salary per week
public BasePlusCommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary) {
super(firstName, lastName, socialSecurityNumber, grossSales, commissionRate);
if (baseSalary < 0.0) // validate baseSalary
throw new IllegalArgumentException("Base salary
阅读全文