import java.util.Scanner;
import java.io.*;

/** The Employee class will read in info about any number of
  * employees and caculate
  * the weekly salary for each employee. 
  * This version uses ARRAYS AND METHODS
  * (v7 uses arrays, v8 uses methods, v9 uses both.
  * v10 shows how values can be placed in an array in a method,
  * and be used back in the calling method
  */
public class EmployeeV10 {
 private static final int SIZE = 100; // assume no more than 100 employees
   
 public static void main(String[] args) throws IOException { 
   // Declare all of the arrays
   String[] names = new String[SIZE];
   double[]  hours = new double[SIZE];
   double[]  rates = new double[SIZE];
   int[] idnums = new int[SIZE];
   double[] grossPay = new double[SIZE];
   double[] netPay = new double[SIZE];
   
   // Step 1: read in data
   int numEmps = readData(idnums, names, hours, rates);
   // Steps 2&3: Calculate gross pay and net pay
   calculatePay(hours, rates, grossPay, netPay, numEmps);
   // Step 4: Print all data
   printAllData(idnums, names, hours, rates, grossPay, netPay, numEmps);
   // Step 5: print sum of all net pays
   double sum = sumPay(netPay, numEmps);
   System.out.printf("We processed %d employees, Total pay is: $%.2f.\n",
                     numEmps, sum);
 }

 /** this method reads in data about employees from a file 
   * @param id id numbers
   * @param names array of String for names of employees 
   * @param hrs hours worked
   * @param rts rate of pay per hour
   * @return number of data elts read in
   */
 public static int readData(int[] id, String[] names, double[] hrs, double[] rts)
   throws IOException
 {
   Scanner sc = new Scanner(new File("employeeData.txt"));
   int count=0; // this will count the number of employees
   while (sc.hasNext() && count<SIZE) {
      // Read in idnum, name, hours, rate for employee
      id[count]=sc.nextInt();
      names[count] = sc.next();
      hrs[count] = sc.nextDouble();
      rts[count] = sc.nextDouble();
      count++;
   }
   sc.close();
   return count;
 }
 
 /** this method calculates both the grosspay and the net pay for
   * all employees.
   * @param hours stores hours worked for all employees
   * @param rates stores rates for all employees
   * @param grossPay comes in as an empty array, and the method fills it
   * @param netPay also gets filled by this method
   * @param numEmps is the number of employees (ie size of all these arrays)
   */
 public static void calculatePay(double[] hours, double[] rates,
 double[] grossPay, double[] netPay, int numEmps) {

   for (int i=0; i<numEmps; i++) {
      if (hours[i] <= 40) {
         grossPay[i] = hours[i] * rates[i];
    }
    else {
         grossPay[i] = 40 * rates[i] + (hours[i]-40)*1.5*rates[i];
    }
    // this would be a good place to use conditional ? : operator
    //grossPay[i] = hours[i]<=40 ? hours[i]*rates[i] : 
      //                40 * rates[i] + (hours[i]-40)*1.5*rates[i];
    // subtract taxes from the gross pay which is the parameter
    if (grossPay[i] >= 1000){
         netPay[i] = grossPay[i] - .15 * grossPay[i];
      }
      else {
         netPay[i] = grossPay[i] - .10 * grossPay[i];
      }
   }
 }

 /** method sumPay receives an array representing net Pay and sums
   * all the values.
   * @param arr array to sum
   * @param size number of elts in array to be processed
   * @return sum of values as a double
   */
 public static double sumPay(double[] arr, int size) {
    double sum=0.0;
    for (int i=0; i<size; i++)
       sum+=arr[i];
    return sum;
 }
 /** method printAllData gets all of the arrays from main and
   * prints them to a file. 
   * @param size is the number of elements in the arrays.
   */
 public static void printAllData(int[] idnums, String[] names, double[] hours,
        double[] rates, double[] grossPay, double[] netPay, int size) throws IOException {
      
      PrintWriter outFile = new PrintWriter("employeePay.txt");
      // write headings to file
      outFile.println("ID      Name     Hours     Rate    Gross Pay    Net Pay");
      for (int i=0; i<size; i++) {
         outFile.printf("%d    %-8s %5.2f %9.2f", 
                     idnums[i], names[i], hours[i], rates[i]);
         // print gross pay
         outFile.printf("%11.2f", grossPay[i]);
         // print net pay
         outFile.printf("%12.2f", netPay[i]);
         outFile.println(); // goes to next line
      }
      outFile.close();
 }
}
/*****
  Optional to add to HW7:
    
Suppose I have a file with answers of all students in it. Each line
  of the file represents one student's answers.
  Have your program grade all of the students quizes and store the
  grades in a grades array where grades[i] represents the grade of
  the students whose answers are in line i in the input file.
  *******/
  
  
  
  


