继承-从Employee类和测试中创建一个Class Commission Employee

时间:2019-12-14 18:34:56

标签: inheritance stack-overflow subclass tostring superclass

早上好,我已经分配给了:  将CommissionEmployee类重写为Employee的子类。 CommissionEmployee应该仅包含未在超类Employee中声明的实例变量和方法。 CommissionEmployee的构造函数应调用Employee的构造函数,而CommissionEmployee的toString方法应调用Employee的toString方法。 创建一个Driver类来测试新的CommissionEmployee类。提示用户输入名字,姓氏,社会保险号,销售总额和佣金率,并使用toString方法打印其信息来创建CommissionEmployee对象。 我似乎遇到的问题是CommissionEmployeeTest类不会输出放置在CommissionEmployee类的toString方法中的内容。我觉得我的超类员工和测试类都是正确的,但我认为错误在于我确定收入的方法中。我正在寻找的输出是 员工名字姓氏 社会安全号码 收益 收入为总销售额+(佣金率*总销售额) 这是我的错误信息:

class Employee
public class Employee extends Object {

    protected final String firstName;
    protected final String lastName;
    protected final 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;
    }

    @Override

    public String toString() {
        return String.format(firstName, lastName, socialSecurityNumber);
    }
}
class CommissionEmployee
public class CommissionEmployee extends Employee {

    private double grossSales;
    private double commissionRate;
    private double earnings;

    public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales,
            double commissionRate, double earnings) {
        super(firstName, lastName, socialSecurityNumber);       

        if (grossSales < 0.0)
            throw new IllegalArgumentException("Gross sales must be >= 0.0");

        if (commissionRate <= 0.0 || commissionRate >= 1.0)
            throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");

        this.grossSales = grossSales;
        this.commissionRate = commissionRate;
    }

    public void setGrossSales(double grossSales) {

        if (grossSales < 0.0)
            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)
            throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");
        this.commissionRate = commissionRate;
    }

    public double getCommissionRate() {
        return commissionRate;
    }


    public Double earnings(double d) {
        return earnings(grossSales+ (commissionRate * grossSales));
    }

    public double getEarnings() {
        return earnings;
    }

    public void setEarnings(double earnings) {
        this.earnings = earnings;
    }
    @Override

    public String toString() {

        return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f%n%s: %.2f%n%s",
                "commission employee", firstName, lastName,
                "social security number", socialSecurityNumber,
                "Total earnings", earnings(earnings));
    }

    public void setFirstName(String firstName) {
    }

    public void setLastName(String lastName) {
    }

    public void setSocialSecurityNumber(String socialSecurityNumber) {
    }

    public void setGrossSales(String grossSales) {  
    }

    public void setCommissionRate(String commissionRate) {
    }
}
class CommissionEmployeeTest
  //CommissionEmployee test program.
 import java.util.Scanner;

  public class CommissionEmployeeTest {
     public static void main(String[] args) { // instantiate CommissionEmployee object

        CommissionEmployee employee =  
           new CommissionEmployee(null, null, null, 0, .1, 0);

        Scanner input = new Scanner(System.in);

       // get commission employee data
       System.out.printf(
          "Employee information obtained by get methods:%n");

       System.out.printf("Enter employee's First name:");
               String firstName = input.nextLine();
               employee.setFirstName(firstName);

       System.out.printf("Enter employee's last name:");
               String lastName = input.nextLine();
               employee.setLastName(lastName);

       System.out.printf("Enter employee's social security number:");
               String socialSecurityNumber = input.nextLine();
               employee.setSocialSecurityNumber(socialSecurityNumber);

       System.out.printf("Gross sales for employee:");
               String grossSales = input.nextLine();
               employee.setGrossSales(grossSales);

       System.out.printf("Commission rate for employee:");
               String commissionRate = input.nextLine();
               employee.setCommissionRate(commissionRate);         

        System.out.printf("%n%s:%n%n%s%n",
            "Updated employee information obtained by toString",                          

          employee.toString());
    }
  }

错误 通过get方法获得的员工信息: 输入员工的名字:John 输入员工的姓氏: 输入员工的社会保险号:123456789 员工销售总额:4000.00 员工佣金率:.1 线程“主”中的异常java.lang.StackOverflowError     在CommissionEmployee.earnings(CommissionEmployee.java:45)

我尝试在CommissionEmployee中操纵我的toString方法以及确定收入的方法,但是我感觉自己在转动轮子。感谢您的帮助。我上课还有1周的时间,感觉我每个星期在发布问题时都可以提供更多信息,然后我做了最后一个问题。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。经过审查和修订(几次),我找到了答案。我需要对toString方法和收益方法进行修订。清理了很多代码并解决了。