I'm trying to write a class(SalaryEmployee) and subclass(ComissionEmployee) but I'm not getting the right salary in the main and I could not do a se1.setSalesAmount(20000). Where did I go wrong?
This is what is in main:
public static void main(String[] args) {
SalaryEmployee se1 = new CommissionEmployee("E3", "John", 10000);
se1.setSalesAmount(20000) // not found
System.out.println(se1.getSalary());
}
SalaryEmployee which has id (String), name(String), salary(double). It has a constructor to initialise the instance variables. It has a getSalary
method that returns the salary as well as a toString
method that returns the values of the instance variables.
public class SalaryEmployee {
private String id;
private String name;
double salary;
public SalaryEmployee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public double getSalary() {
return salary;
}
public String toString() {
return "ID=" + id + " Name=" + name + " Salary=" + salary;
}
}
Then in ComissionEmployee I need to write it as a subclass of SalaryEmployee which has id(String), name(String), baseSalary(double), salesAmount(double). It has a constructor to initialise id, name and baseSalary only. It has get/set methods for salesAmount. It has a getSalary method that returns the salary based on the baseSalary + commission as follows:
salesAmount below 10,000 - No commission,
at least 10,000 but below 20,000 - 3% of baseSalary,
at least 20,000 - 5% of baseSalary
public class CommissionEmployee extends SalaryEmployee {
private double baseSalary;
private double salesAmount;
public CommissionEmployee(String id, String name, double salary) {
super(id, name, salary);
this.baseSalary = salary;
}
public void setSalesAmount(double amount) {
this.salesAmount = amount;
}
public double getSalesAmount() {
return salesAmount;
}
public double getSalary() {
if (salesAmount <= 10000 || salesAmount < 20000) {
return 1.03 * baseSalary;
} else if (salesAmount > 20000) {
return 1.05 * baseSalary;
} else if (salesAmount < 10000) {
return baseSalary;
}
return baseSalary;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…