/* Company.java        Authors: Koffman & Wolz 
 * Class for processing array of employees and computing
 * company payroll.
 * Uses Employee
*/
import psJava.KeyIn;

public class Company {

  // Data fields
  private Employee[] employees; // array of employees
  private double payroll;       // company payroll

  // Methods
  // postcondition: Allocates storage for an array of Employee objects
  //   of the specified size. 
  public Company(int numEmp) {
     employees = new Employee[numEmp];
  }

  public void setPayroll(double totPay) {
    payroll = totPay;
  }

  // postcondition: Returns true if its argument 
  //   is a valid subscript for array employees
  private boolean inRange(int index) {
    return index >= 0 && index < employees.length;
  }

  // postcondition: Stores the employee at the specified index if
  //    the index is a valid subscript.
  public void setEmployee(Employee emp, int index) {
    if (inRange(index))
       employees[index] = emp;
  }

  public double getPayroll() {
    return payroll;
  }

  // postcondition: If the index is valid, returns that employee
  //   otherwise, returns null.
  public Employee getEmployee(int index) {
    if (inRange(index))
      return employees[index];
    else
      return null;
  }

  // postcondition: Returns an Employee object after reading its data.
  private static Employee readOneEmployee() {
     String id = KeyIn.readString("Employee id:");
     double hours = KeyIn.readDouble("Hours worked:");
     double rate = KeyIn.readDouble("Hourly rate $");
     return new Employee(id, hours, rate);
  }

  // postcondition: Stores all employee data in myCompany.
  public void readPayrollData() {
    for (int countEmp = 0; countEmp < employees.length; countEmp++) {
       // Read data for a new employee and store
       //    the data in array employees
       employees[countEmp] = readOneEmployee();
    }
  }

  // postcondition: Compute the total payroll for the company.
  public void computePayroll() {
    // Compute total gross pay.
    payroll = 0;
    for (int index = 0; index < employees.length; index++) {
       payroll += (employees[index].getHours() *
                   employees[index].getRate());
    }
  }

  public String toString() {
    String empStr = "Employee information: \n";
    for (int countEmp = 0; countEmp < employees.length; countEmp++) {
       empStr = empStr + employees[countEmp] + "\n";
    }
    return empStr + "\nCompany payroll is $" + payroll;
  }


}

