继承-从Employee类创建一个Class Employee雇员并进行测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承-从Employee类创建一个Class Employee雇员并进行测试相关的知识,希望对你有一定的参考价值。

[早上好,我被分配给:将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.StackOverflowErrorat CommissionEmployee.earnings(CommissionEmployee.java:45)

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

答案

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

以上是关于继承-从Employee类创建一个Class Employee雇员并进行测试的主要内容,如果未能解决你的问题,请参考以下文章

PHP - 子类没有从父类继承[重复]

实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff。 具体要求如下: Person类:姓名,地址,电话号码和电子邮件地址; Em

创建controller/service/mapper/impl过程

Hibernate入门

实验六201771010101 白玛次仁

VB.Net 中的类命名决策