0

在下面的课程中,我试图在工资系统中打印员工的详细信息。请注意,Porter、Pharmacist 和 Surgeon 都继承自 Employee。

然而,它只是重复打印添加到数组中的第一个员工的详细信息。我知道预期的输出,但我无法重现它。

班级:

public class PayrollManager {

    public static final int HOURS_PER_WEEK = 35;

    public static Employee[] employees = new Employee[6];

    public static void main(String[] args) {

        // create all employees
        Employee generalEmployee = new Employee("Odd", "Jobbie", 10.50);
        Employee porter1 = new Porter("Ivana", "Patient", 10.50, "Royal");
        Employee porter2 = new Porter("Amanda", "Pushabed", 10.50, "BCH");
        Employee surgeon1 = new Surgeon("Jack", "Ripper", 55.25, "Renal",
                650.00);
        Employee surgeon2 = new Surgeon("Edward", "Lister", 55.25, "Vascular",
                800.00);
        Employee pharmacist = new Pharmacist("Poppy", "Pill", 30.50, 7, 750);

        // call method to handle adding the employees to the list
        addEmployeeToList(generalEmployee);
        addEmployeeToList(porter1);
        addEmployeeToList(porter2);
        addEmployeeToList(surgeon1);
        addEmployeeToList(surgeon2);
        addEmployeeToList(pharmacist);

        // show all employees
        displayAllEmployees();
        System.out.println();
        // run payroll
        processWeeklyPayroll();

    }// end of main

    /**
     * Method to add an Employee to an array
     * 
     * @param e
     */
    public static void addEmployeeToList(Employee e) {

        for (int loop = 0; loop < employees.length; loop++) {
            // check there is a space
            if (employees[loop] == null) {
                // add it to free space
                employees[loop] = e;

            }
        }

    }

    public static void displayAllEmployees() {

        for (Employee e : employees) {

            if (e != null) {

                e.displayAll();
                System.out.println();
            }
        }
    }

    public static void processWeeklyPayroll() {

        for (Employee e : employees) {

            if (e != null) {

                e.calculateWeeklySalary(HOURS_PER_WEEK);
                // line break
                System.out.println();
            }
        }

    }

}

电流输出:

[Employee] Odd Jobbie 10.500000
[Employee] Odd Jobbie 10.500000
[Employee] Odd Jobbie 10.500000
[Employee] Odd Jobbie 10.500000
[Employee] Odd Jobbie 10.500000
[Employee] Odd Jobbie 10.500000

Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000
Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000
Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000
Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000
Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000
Odd Jobbie [Base Rate Employee] : 35.000000hrs * £10.500000= £367.500000

请注意,预期的输出应该是打印每个测试数据成员的详细信息,即每个药剂师、外科医生等......

4

1 回答 1

1

这是罪魁祸首:

public static void addEmployeeToList(Employee e) {

    for (int loop = 0; loop < employees.length; loop++) {
        // check there is a space
        if (employees[loop] == null) {
            // add it to free space
            employees[loop] = e;

        }
    }

}

它只是将列表中的所有员工设置为第一次调用该函数的参数;当你第一次调用它时,employees 充满了 null 元素,因此条件始终为 true;但是第二次、第三次等调用它时,所有元素都设置为generalEmployee。

一个快速的解决方法是:

public static void addEmployeeToList(Employee e) {

    for (int loop = 0; loop < employees.length; loop++) {
        // check there is a space
        if (employees[loop] == null) {
            // add it to free space
            employees[loop] = e;
            break;
        }
    }

}

但我强烈建议使用 ArrayList。

ArrayList<Employee> employees = new ArrayList<Employee>(); //here you declare a dinamically resizing list.
employees.add(e) //this is how you add a new Employee to the list.
于 2014-06-05T17:38:33.297 回答