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.
  * next time (monday nov 5): put in one more method for reading.
  */
public class EmployeeV9 {
  
 public static void main(String[] args) throws IOException {
   Scanner sc = new Scanner(new File("employeeData.txt"));
   final int SIZE = 100; // assume no more than 100 employees
   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];
   double sum=0.0;
   
   // while there is data to be read
   int numEmps=0; // this will count the number of employees
   while (sc.hasNext()) {
      // Step 1: Read in idnum, name, hours, rate for employee
      idnums[numEmps]=sc.nextInt();
      names[numEmps] = sc.next();
      hours[numEmps] = sc.nextDouble();
      rates[numEmps] = sc.nextDouble();
      // Steps 2&3: Calculate gross pay and net pay
      grossPay[numEmps] = calculateGross(hours[numEmps], rates[numEmps]);
      netPay[numEmps] = calculateNet(grossPay[numEmps]);
      sum+=netPay[numEmps];
      numEmps++;
   }// end while loop
   // after fall out of while loop, print the number of employees and total pay
   System.out.printf("We processed %d employees, Total pay is: $%.2f.\n",
                     numEmps, sum);
   // HERE, I HAVE ACCESS TO ALL INFO THAT WAS READ IN AND CALCULATED
   // to pass an array to a method, put ONLY the name of the array
   printAllData(idnums, names, hours, rates, grossPay, netPay, numEmps);
   sc.close();
 }

 public static double calculateGross(double hours, double rate) {
    if (hours <= 40) {
         return hours * rate;
    }
    else {
         return 40 * rate + (hours-40)*1.5*rate;
    }
 }
 public static double calculateNet(double pay) { 
      // subtract taxes from the gross pay which is the parameter
      if (pay >= 1000){
         return pay - .15 * pay;
      }
      else {
         return pay - .10 * pay;
      }
 }

 /** 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();
 }
/** Lab assignment:   
  * Declare an array of ints. Read in values as long as user wants
  * to enter values and store the values in the array.
  * (hint: in order to do this, you will need a counter that will both
  * count and be used as the index into the array.
  * 
  * Call a method to print all the values in the array.
  * Note: the method should only print the number of elts that you
  * read in.
  */
}