/* NewCompany.java        Authors: Koffman & Wolz
 * A class for processing an array of employees of 
 * different types and for computing company payroll.
 * Uses NewEmployee, HourlyEmployee, SalaryEmployee.
 */
public class NewCompany {

  // Data fields
  private NewEmployee[] employees;  // array of employees
  private double payroll;           // company payroll

  // Methods
  // postcondition: Allocates storage for numEmp employees
  //   in a new NewCompany object.
  public NewCompany(int numEmp) {
    employees = new NewEmployee[numEmp];
  }

  // postcondition: Stores employee emp at position index
  //   of array employees if index is in range. 
  public void setEmployee(NewEmployee emp, int index) {
    if (index >= 0 && index < employees.length)
       employees[index] = emp;
  }
 
  // postcondition: Returns an employee at position index
  //   of employees. Returns null if index is not in range.
  public NewEmployee getEmployee(int index) {
    if (index >= 0 && index < employees.length)
       return employees[index];
    else
       return null;
  }

  // postcondition: Computes weekly pay for each employee,
  //   adds it to payroll, and updates employee's total pay.
  //   Displays each employee's ID (social security number) and pay.
  public void computePayroll() {
    System.out.println("ID" + "\t" + "pay");
    for (int i = 0; i < employees.length; i++) {
       // Calculate weekly pay.
       double weekPay;
       NewEmployee emp = employees[i];
       if (emp instanceof HourlyEmployee)
          weekPay = ((HourlyEmployee) emp).calcWeeklyPay();
       else if (emp instanceof SalaryEmployee)
          weekPay = ((SalaryEmployee) emp).calcWeeklyPay();
       else
          weekPay = 0.0;

       // Add the weekly pay amount to total pay.
       emp.updateTotalPay(weekPay);

       // Add the weekly pay amount to the payroll.
       payroll += weekPay;

       // Display ID and pay.
       System.out.println(emp.getSocial() +
                          "\t" + weekPay);
    }
  }


  public String toString() {
    String result = "Total payroll is $" + payroll +
                    "\n\nEmployee information:\n";
    for (int i = 0; i < employees.length; i++) {
       result = result + employees[i].toString() + "\n\n";
    }
    return result;
  }

}

