在下面的课程中,我试图在工资系统中打印员工的详细信息。请注意,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
请注意,预期的输出应该是打印每个测试数据成员的详细信息,即每个药剂师、外科医生等......