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 METHODS */ public class EmployeeV7 { public static void main(String[] args) throws IOException { String name; double hours, rate, sum=0.0; int idnum, numEmployees=0; // this will count the number of employees Scanner sc = new Scanner(new File("employeeData.txt")); PrintWriter outFile = new PrintWriter("employeePay.txt"); // write headings to file printHeadings(outFile); // while there is data to be read while (sc.hasNext()) { // Step 1: Read in idnum, name, hours, rate for employee idnum=sc.nextInt(); name = sc.next(); hours = sc.nextDouble(); rate = sc.nextDouble(); // Step 2: Calculate gross pay and net pay double grossPay = calculateGross(hours, rate); double netPay = calculateNet(grossPay); // Step 3: print to the file printEmployee(outFile, idnum, name, hours, rate, grossPay, netPay); // Step 4: update total and counter sum+=netPay; numEmployees++; }// 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", numEmployees, sum); sc.close(); outFile.close(); } /** prints headings to a file. * @param out which is a PrintWriter */ public static void printHeadings(PrintWriter out) { out.println("ID Name Hours Rate " + " Gross Pay Net Pay"); } /** prints data about a single employee * accepts all pieces of data about the employee and a PrintWriter */ public static void printEmployee(PrintWriter out, int id, String nm, double hrs, double rt, double gross, double net) { out.printf("%d %-8s %5.2f %9.2f %10.2f %11.2f", id, nm, hrs, rt, gross, net); out.println(); // goes to next line } 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; } } } /** Lab: write 3 methods besides for main: * 1. read in a temperature in degrees celcius and return that number * 2. accept a temperature in degrees celcius, convert it to * faranheit and return the faranheit number. * 3. isHot that will return true or false based upon a temperature * * main should call all three methods. main should print a sentence * saying for example: * # in degrees celcius is # in faranheit and it IS hot. * No printing is done in the methods. * E.C. main calls the methods in a loop. */