EmployeeManager输出错误

时间:2015-10-03 16:51:52

标签: java

对于我的Java II类,我们正在编写一个程序,使EmployeeManager能够使用测试程序以及教师提供的4个类程序。但是,我遇到了一些获得正确输出的问题。

public class mcshonsey_EmployeeManager
{
final int EMPLOYEE_MAX = 15;
Employee[] employees = new Employee[EMPLOYEE_MAX];
int currentEmployees;

public mcshonsey_EmployeeManager()
{
    employees = new Employee[this.getEmployeeMax()];
    currentEmployees = 0;
}

public int getEmployeeMax()
{
    return this.EMPLOYEE_MAX;
}

public void setCurrentEmployees(int currentEmployees)
{
    this.currentEmployees = currentEmployees;
}

public int getCurrentEmployees()
{
    return this.currentEmployees;
}

public void addEmployee(int type, String firstName, String lastName, char mI, char gender, int employeeNumber, boolean fulltime, double amount)
{
    if (this.getCurrentEmployees() < 15);
    {
        setCurrentEmployees(getCurrentEmployees() + 1);
    }

    if (this.getCurrentEmployees() == 15);
    {
        System.out.println("Maximum number of Employees reached. Employee " + employeeNumber + " not added. ");
    }


    if (type == 1)
    {
        employees[this.getCurrentEmployees()] = new HourlyEmployee(firstName, lastName, mI, gender, employeeNumber, fulltime, amount);
    }
    else if (type == 2)
    {
        employees[this.getCurrentEmployees()] = new SalaryEmployee(firstName, lastName, mI, gender, employeeNumber, fulltime, amount);
    }
    else
    {
        employees[this.getCurrentEmployees()] = new CommissionEmployee(firstName, lastName, mI, gender, employeeNumber, fulltime, amount);
    }
}

public void removeEmployee(int index)
{
    do
    {
        employees[index] = employees[14];
        this.currentEmployees = currentEmployees;
        currentEmployees += -1;
    } while (index <= this.currentEmployees);
}

public void listAll()
{
    if (this.currentEmployees == 0)
    {
        System.out.print("None \n");
    }
    else
    {
        for (int x = 0; x < employees.length; x++)
        {
            if (employees[x] != null)
            {
                System.out.println(employees[x]);
            }
        }
    }
}

public void listHourly()
    {
        int a = 0;
        for (int x = 0; x < employees.length; x++)
        {
            if (employees[x] instanceof HourlyEmployee)
            {
                a += 1;
                System.out.println(employees[x]);
            }
        }

            if (a == 0)
            {
                System.out.print("None \n");
            }
    }

public void listSalary()
    {
        int a = 0;
        for (int x = 0; x < employees.length; x++)
      {
            if (employees[x] instanceof SalaryEmployee)
            {
                a += 1;
                System.out.println(employees[x]);
            }
      }

            if (a == 0)
            {
                System.out.print("None \n");
            }
    }

public void listCommission()
    {
        int a = 0;
        for (int x = 0; x < employees.length; x++)
        {
            if (employees[x] instanceof CommissionEmployee)
            {
                a += 1;
                System.out.println(employees[x]);
            }
        }

        if (a == 0)
        {
            System.out.print("None \n");
        }
    }

public void resetWeek()
{
    for (int x = 0; x < currentEmployees; x++)
    {
        employees[x].resetWeek();
    }
}

public double calculatePayout()
{
    double totalPay = 0.0;
    double y = 0.0;

    for (int x = 0; x < currentEmployees; x++)
    {
        y = employees[x].calculateWeeklyPay();
        totalPay += y;
    }

    return totalPay;
}

public void removeRedundancies()
{

}

public int getIndex(int employeeNumber)
{
    int index = 0;
    int y;

    for (int x = 0; x < currentEmployees; x++)
    {
        y = employees[x].getEmployeeNumber();

        if (y == employeeNumber)
        {
            index = x;
            x = 1000;
        }

        else
        {
            index = -1;
        }
    }

    return index;
}

public void sortNumber()
{
    Employee[] employees2 = new Employee[1];
    int aindex;
    int bindex;

    int [] employeeNumbers = new int[currentEmployees];
    boolean flag = true;
    for (int x = 0; x < currentEmployees; x++)
    {
        employeeNumbers[x] = employees[x].getEmployeeNumber();
    }

    while(flag)
    {
        flag = false;
        int temporary;

        for (int b = 0; b < currentEmployees; b++)
        {
            if (employeeNumbers[b] > employeeNumbers[b+1])
            {
                temporary = employeeNumbers[b];
                employeeNumbers[b] = employeeNumbers[b+1];
                employeeNumbers[b+1] = temporary;
                flag = true;
            }
        }
    }

    for (int b = 0; b < currentEmployees; b++)
    {
        aindex = getIndex(employeeNumbers[b]);
        bindex = b;

        employees2[0] = employees[b];
        employees[b] = employees[aindex];
        employees[aindex] = employees2[0];
    }
}
public void sortName()
{
    Employee[] employee2 = new Employee[1];
    int aindex;
    int bindex;

    String [] names = new String[currentEmployees];
    for (int b = 0; b < currentEmployees; b++)
    {
        for (int c = b + 1; c < currentEmployees; c++)
        {
            if (employees[c].getLastName().compareTo(employees[b].getLastName()) < 0)
            {
                employee2[0] = employees[b];
                employees[b] = employees[c];
                employees[c] = employee2[0];
            }
        }
    }
}

public void annualRaise()
{

    for(int x = 0; x < currentEmployees; x++)
    {
        employees[x].annualRaise();
    }
}

public double holidayBonuses()
{
    double bonus = 0.0;

    for (int x = 0; x < currentEmployees; x++)
    {
        bonus += employees[x].holidayBonus();
    }
    return bonus;
}

public void increaseHours(int index, double amount)
{
    for (Employee currentEmployees: employees)
    {
        if (currentEmployees instanceof HourlyEmployee)
        {
            ((HourlyEmployee) employees[index]).increaseHours(amount);
        }
    }
}

public void increaseSales(int index, double amount)
{
    for (Employee currentEmployees: employees)
    {
        if (currentEmployees instanceof CommissionEmployee)
        {
            ((CommissionEmployee) employees[index]).increaseSales(amount);
        }
    }
}                                                                           }

现在,代码编译得很好,并且运行良好。但是,运行它会产生这样的结果:

   List of all employees, HourlyEmployees, SalaryEmployees and   CommissionEmployees:
 None
 None
 None
 None

 Adding employees. . .
 Maximum number of Employees reached. Employee 35487 not added.
 Maximum number of Employees reached. Employee 18224 not added.
 Maximum number of Employees reached. Employee 68176 not added.
 Maximum number of Employees reached. Employee 87995 not added.
 Maximum number of Employees reached. Employee 54145 not added.
 Maximum number of Employees reached. Employee 35487 not added.
 Maximum number of Employees reached. Employee 18224 not added.
 Maximum number of Employees reached. Employee 98573 not added.
 Maximum number of Employees reached. Employee 83574 not added.
 Maximum number of Employees reached. Employee 90842 not added.
 Maximum number of Employees reached. Employee 72108 not added.
 Maximum number of Employees reached. Employee 35487 not added.
 Maximum number of Employees reached. Employee 18224 not added.
 Maximum number of Employees reached. Employee 68176 not added.
 Maximum number of Employees reached. Employee 43872 not added.
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    at mcshonsey_EmployeeManager.addEmployee  (mcshonsey_EmployeeManager.java:64)
    at EmployeeManagerTest.main(EmployeeManagerTest.java:79)

(新产品)

哪个,不是正确的输出。在转移添加员工之前,没有人应该出现4次,只应列出一名员工不要添加。

我真的很难解决这个问题,以使其正常工作。我该如何解决这个问题?

编辑:显示错误的第64行是:

 employees[this.getCurrentEmployees()] = new SalaryEmployee(firstName, lastName, mI, gender, employeeNumber, fulltime, amount);

再次编辑:这是main()方法,EmployeeManagerTest。

import java.util.Scanner;  



// To display currency correctly  

import java.text.NumberFormat;  



// To get current locale  

import java.util.Locale;  



public class EmployeeManagerTest  

{  

public static void main(String[] args)  

{  

    // Variable declarations  

     mcshonsey_EmployeeManager em =          // An EmployeManager object  

                              new mcshonsey_EmployeeManager();  



   Locale currentLocale =              // Store current locale  

                           Locale.getDefault();  



     NumberFormat currencyFormatter =    // Currency formatter  

                           NumberFormat.getCurrencyInstance(currentLocale);  



   // Display all employees, HourlyEmployees, SalaryEmployees and  

   // CommissionEmployees  

    System.out.println("List of all employees, HourlyEmployees, " +  

                      "SalaryEmployees and CommissionEmployees:");  

   em.listAll();  

    em.listHourly();  

   em.listSalary();  

    em.listCommission();  



    // Add employees  

    // Random names generated at http://random-name-generator.info  

 // Random employee numbers generated at http://random.org  

   // For more rigorous tests, uncomment the adds below.  
    System.out.println("\nAdding employees. . .");  

   em.addEmployee(1, "Florence", "Copeland", 'F', 'F', 35487, true, 10);  

    em.addEmployee(3, "Jacqueline", "Newton", 'G', 'F', 18224, false, 1);  

   em.addEmployee(2, "Norma", "Flowers", 'E', 'F', 68176, false, 10000);  

    em.addEmployee(1, "Marian", "Moran", 'T', 'F', 87995, false, 20);  

   em.addEmployee(3, "Anna", "Jacobs", 'F', 'F', 54145, true, 2);  



    // The next two employees are duplicates of the first and second  

    // (employees are equal if their employeeNumbers are the same).  

   em.addEmployee(2, "Eloise", "Franklin", 'I', 'F', 35487, true, 40000);  

    em.addEmployee(1, "Edwin", "Tyler", 'J', 'M', 18224, false, 10);  



                 em.addEmployee(1, "Jamie", "Lamb", 'O', 'F', 98573, false, 30);  

  em.addEmployee(2, "Donnie", "Hogan", 'Q', 'M', 83574, false, 30000);  

     em.addEmployee(1, "Dennis", "Andrews", 'G', 'M', 90842, false, 40);  

    em.addEmployee(1, "Sherry", "Rowe", 'U', 'F', 72108, true, 50);  



    // em.addEmployee(3, "Delia", "Hunter", 'C', 'F', 13695, true, 4);  

    // em.addEmployee(2, "Yvonne", "Douglas", 'D', 'F', 88511, false,       50000);  

    // em.addEmployee(3, "Alonzo", "Welch", 'O', 'M', 18088, false, 5);  

    // em.addEmployee(3, "Gregory", "Tate", 'C', 'M', 97772, true, 1);  

    // em.addEmployee(1, "Sally", "Mendoza", 'D', 'F', 52896, true, 20);  

   // em.addEmployee(3, "Ellis", "Adams", 'G', 'M', 77332, false, 2);  

    // em.addEmployee(3, "Clifford", "Lindsey", 'D', 'M', 39033, false, 3);  

    // em.addEmployee(2, "Kristy", "Morales", 'O', 'F', 51171, true, 10000);  

    // em.addEmployee(3, "Rudy", "Cobb", 'Q', 'F', 65485, true, 4);  



    // The next three employees are duplicates of the first, second, and    third  

    // (employees are equal if their employeeNumbers are the same).  

     em.addEmployee(2, "Johnnie", "Maldonado", 'R', 'M', 35487, true, 20000);  

    em.addEmployee(3, "Andre", "Graham", 'C', 'M', 18224, false, 5);  

     em.addEmployee(1, "Dwayne", "Watts", 'E', 'M', 68176, true, 30);  



          em.addEmployee(2, "Susie", "Barker", 'L', 'F', 43872, true, 20000);  

   em.addEmployee(3, "Nora", "Lucas", 'L', 'F', 35764, true, 3);  



    // Display all employees  

    System.out.println("\nList of all employees:");  

     em.listAll();  



    // Remove redundancies and display  

   System.out.println("\nRemoving redundancies. . .");  

     em.removeRedundancies();  

  System.out.println("\nList of all employees after removing redundancies:");  

   em.listAll();  



    // Display just the HourlyEmployees, SalaryEmployees and                CommissionEmployees  

    System.out.println("\nList of HourlyEmployees:");  

    em.listHourly();  

    System.out.println("\nList of SalaryEmployees:");  

   em.listSalary();  

     System.out.println("\nList of CommissionEmployees:");  

   em.listCommission();  



    // Sort by employee number and display  

   System.out.println("\nSorting by employee number. . .");  

    em.sortNumber();  

   em.listAll();  



   // Sort by last name and display  

  System.out.println("\nSorting by last name. . .");  

    em.sortName();  

   em.listAll();  



   // Calculate and display total weekly payout  

    System.out.printf("\nTotal weekly payout: %s.\n",  

                     currencyFormatter.format(em.calculatePayout()));  



   // Get the index of an employee that doesn't exist  

    System.out.printf("\nIndex of employee with employee number 11111:      %d\n",  

                    em.getIndex(11111));  



     // Attempt to increase hours (can only be applied to HourlyEmployees)  

   System.out.println("\nIncreasing hours. . .");  

   em.increaseHours(em.getIndex(35487), 40);  

     em.increaseHours(em.getIndex(18224), 40);  

  em.increaseHours(em.getIndex(68176), 40);  

    em.increaseHours(em.getIndex(87995), 40);  

   em.increaseHours(em.getIndex(54145), 40);  

    em.increaseHours(em.getIndex(43872), 40);  



   // Attempt to increase sales (can only be applied to                    CommissionEmployees)  

  System.out.println("Increasing sales. . .");  
  em.increaseSales(em.getIndex(35487), 40);  

    em.increaseSales(em.getIndex(18224), 40);  

    em.increaseSales(em.getIndex(68176), 40);  

   em.increaseSales(em.getIndex(87995), 40);  

   em.increaseSales(em.getIndex(54145), 40);  

  em.increaseSales(em.getIndex(43872), 40);  



     // Calculate and display total weekly payout after increases  

    System.out.printf("\nTotal weekly payout after increases: %s.\n",  

                      currencyFormatter.format(em.calculatePayout()));  



    // Give four years of annual raises.  

    System.out.println("\nGiving four years of annual raises. . .");  

   em.annualRaise();  

     em.annualRaise();  

    em.annualRaise();  

    em.annualRaise();  



    // Calculate and display total weekly payout after raises  

   System.out.printf("\nTotal weekly payout after raises: %s.\n",  

                      currencyFormatter.format(em.calculatePayout()));  

   // Calculate and display holiday bonus  

   System.out.printf("\nHoliday bonuses: %s.\n",  
                  currencyFormatter.format(em.holidayBonuses()));  



    // Reset weeks  

   System.out.println("\nResetting the weeks. . .");  

   em.resetWeek();  



    // Calculate and display total weekly payout  

    System.out.printf("\nTotal weekly payout after reset: %s.\n",  

                     currencyFormatter.format(em.calculatePayout()));  

 } // End main()  

} // End class  

再次编辑:我的导师给出了一些修复建议,并且我已经更新了它现在提供的代码和输出。

0 个答案:

没有答案