正确调用不同类中的方法| Java的

时间:2015-09-28 10:32:22

标签: java methods encapsulation superclass

我有一个超类Employee及其子类(HourlyEmployee和CommissionEmployee)和一个测试人员类。

当我运行程序并接收用户值时,在询问小时/销售额并计算付款后 - 给出的值为0.0。薪水没有正确计算 - 或者根本没有 - 为什么会这样,我怎么能正确地计算?

abstract class Employee {

// Data members
private String firstName;
private String lastName;
private int employeeNumber;
private int numberOfEmployees;
protected int hours;
protected int sales;
protected double pay;

// Default constructor
public Employee() {
    firstName = null;
    lastName = null;
    employeeNumber = 0;
    numberOfEmployees = 0;
}

// Getter and setter methods
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getEmployeeNumber() {
    return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber) {
    this.employeeNumber = employeeNumber;
}

public int getNumberOfEmployees() {
    return numberOfEmployees;
}
public void setNumberOfEmployees(int numberOfEmployees) {
    this.numberOfEmployees = numberOfEmployees;
}

// Abstract method to be implemented in sublcasses
public abstract void earnings();

@Override
public String toString(){
    return "First Name: " + getFirstName() + "\n" + "Last Name: " + getLastName() + "\n" +
            "Employee Number: " + getEmployeeNumber() + "\n" + "Number of Employees: "
            + getNumberOfEmployees();
}

}

`

public class HourlyEmployee extends Employee {

// Constructor
   public HourlyEmployee() {
        //Calls default constructor in superclass
        super();
   }

   // Establish the amount of pay for hourly employees
   int rate = 15;

   // Override earnings method
   @Override
   public void earnings(){
         pay = hours * rate;
   }

   // Return String representation of class
   public String toString(){
         return "First Name: " + getFirstName() + "Last Name: " + getLastName() +
        "Employee Number: " + getEmployeeNumber() + "Number of Employees: "
        + getNumberOfEmployees();
  }
  }

`

public class CommissionEmployee extends Employee {

     // Constructor
     public CommissionEmployee() {
         //Calls default constructor in superclass
         super();
     }

     // Establish the amount of pay for hourly employees
     double commission = 0.10;

     // Override earnings method
     @Override
     public void earnings(){
         pay = commission * sales;
     }

     // Return String representation of class
     public String toString(){
     return "First Name: " + getFirstName() + "Last Name: " + getLastName() +
        "Employee Number: " + getEmployeeNumber() + "Number of Employees: "
        + getNumberOfEmployees();
     }
}

`

import java.util.LinkedList;
import java.util.Scanner;

public class EmployeeTester {

    public static void main(String[] args) {

        // Protected double only visible in superclass and subclass.
        // Must be declared again in tester class.
        double pay;
        int hours;
        int sales;

        // Create new LinkedList
        LinkedList<Employee> employeeList = new LinkedList<>();

        // Create Scanner obkect
        Scanner keyboard = new Scanner(System.in);

        char yes = 'y';
        int x = 0;

        while(yes == 'y' || yes == 'Y'){

            // Declare & create a HourlyEmployee odject
            HourlyEmployee employee1 = new HourlyEmployee();
            employeeList.add(employee1);

            System.out.print("Enter First Name: ");
            String firstName = keyboard.next();
            employee1.setFirstName(firstName);
            employeeList.get(x).setFirstName(firstName);
            System.out.print("Enter Last Name: ");
            String lastName = keyboard.next();
            employee1.setLastName(lastName);
            employeeList.get(x).setLastName(lastName);
            System.out.print("Enter Employee Number: ");
            int employeeNumber = keyboard.nextInt();
            employee1.setEmployeeNumber(employeeNumber);
            employeeList.get(x).setEmployeeNumber(employeeNumber);
            System.out.print("Enter Number of Employees: ");
            int numberOfEmployees = keyboard.nextInt();
            employee1.setNumberOfEmployees(numberOfEmployees);
            employeeList.get(x).setNumberOfEmployees(numberOfEmployees);
            System.out.print("Enter Hours Worked: ");
            hours = keyboard.nextInt();


            // Calculate earnings
            employee1.earnings();

            System.out.println(employee1.toString());
            System.out.println("Total Earnings: " + employee1.pay);

            x++; // increment x

            System.out.print("Continue? [y/n] ");
            yes = keyboard.next().charAt(0);
        }

            // Declare & create a CommissionEmployee odject
            CommissionEmployee employee2 = new CommissionEmployee();
            employeeList.add(employee2);

            System.out.print("Enter First Name: ");
            String firstName = keyboard.next();
            employee2.setFirstName(firstName);
            employeeList.get(x).setFirstName(firstName);
            System.out.print("Enter Last Name: ");
            String lastName = keyboard.next();
            employee2.setLastName(lastName);
            employeeList.get(x).setLastName(lastName);
            System.out.print("Enter Employee Number: ");
            int employeeNumber = keyboard.nextInt();
            employee2.setEmployeeNumber(employeeNumber);
            employeeList.get(x).setEmployeeNumber(employeeNumber);
            System.out.print("Enter Number of Employees: ");
            int numberOfEmployees = keyboard.nextInt();
            employee2.setNumberOfEmployees(numberOfEmployees);
            employeeList.get(x).setNumberOfEmployees(numberOfEmployees);
            System.out.print("Enter Sales Made: ");
            sales = keyboard.nextInt();

            // Calculate earnings
            employee2.earnings();

            System.out.println(employee2.toString());
            System.out.println("Total Earnings: " + employee2.pay);

            x++; // increment x

            System.out.print("Continue? [y/n] ");
            yes = keyboard.next().charAt(0);

        }     
    }

2 个答案:

答案 0 :(得分:1)

您需要为员工对象设置小时数和销售额,目前它们为0,因为,作为int,销售额和小时数都会初始化为0,

因此,commission * sales将变为0,hours * rate将变为0。

在EmployeeTester中,将Hours设置为HourlyEmployee对象

System.out.print("Enter Hours Worked: ");
hours = keyboard.nextInt();
employee1.setHours(hours);

在EmployeeTester中,将Sales设置为CommissionEmployee对象

System.out.print("Enter Sales Made: ");
sales = keyboard.nextInt();
employee2.setSales(sales);

答案 1 :(得分:0)

您需要在员工

中添加setHours方法
public void setHours(int hours) {
        this.hours = hours;
    }

在获得数小时之后,在EmployeeTester中调用此方法。