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