/* Method8.java CIS 160 Dave Klick 2004-10-18 payroll calculation example using methods */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class EmployeeRecord { public String employeeName =""; public double hourlyRate=0.0, hoursWorked=0.0, taxRate=0.0; public double grossAmount=0.0, netAmount=0.0; } public class Method8 { private static double totalGross = 0.0; private static double totalNet = 0.0; public static void main(String[] args) throws IOException { EmployeeRecord rec = new EmployeeRecord(); BufferedReader in = new BufferedReader(new FileReader("paydata.txt")); int numRecs = 0; instructions(); reportTitle(); while (getInput(in, rec)) { numRecs++; processPay(rec); printEmployeeInfo(rec.employeeName, rec.hourlyRate, rec.hoursWorked, rec.taxRate, rec.grossAmount, rec.netAmount); totalAmounts(rec.grossAmount, rec.netAmount); } in.close(); System.out.printf("Totals %53.2f %9.2f\n", totalGross, totalNet); } private static boolean getInput(BufferedReader in, EmployeeRecord r) { try { r.employeeName = in.readLine(); r.hourlyRate = Double.parseDouble(in.readLine()); r.hoursWorked = Double.parseDouble(in.readLine()); r.taxRate = Double.parseDouble(in.readLine()); } catch (IOException e1) { return false; } catch (NumberFormatException e2) { return false; } catch (NullPointerException e3) { return false; } return true; } private static void instructions() { System.out.println(" Instructions for Payroll Report Program"); System.out.println("This program calculates a paycheck for each employee."); System.out.println("A text file will be created outside of the program with the"); System.out.println("following information:"); System.out.println(" name, rate of pay, hours worked, and tax percentage"); System.out.println(""); System.out.println("The program will create a report in columnar format showing"); System.out.println("the employee name, hourly rate, number of hours worked, tax rate,"); System.out.println("gross pay, and net pay."); System.out.println(""); System.out.println("After all employees are processed, totals will be displayed,"); System.out.println("including total gross amount and total net pay."); System.out.println(""); } private static void reportTitle() { System.out.println(" Payroll Report"); System.out.println("Employee Hourly Hours Tax Gross Net"); System.out.println("Name Rate Worked Rate Amount Amount"); System.out.println(""); } private static void printEmployeeInfo(String eName, double hRate, double hours, double taxRate, double gross, double net) { System.out.printf("%-20s%10.2f%10.2f%10.2f%10.2f%10.2f\n", eName, hRate, hours, taxRate, gross, net); } private static void processPay(EmployeeRecord r) { r.grossAmount = calcGross(r.hourlyRate, r.hoursWorked); double tax = (r.taxRate/100) * r.grossAmount; r.netAmount = r.grossAmount - tax; } private static double calcGross(double hRate, double hours) { double gross = hours * hRate; if (hours > 40) gross += (hours-40) * (hRate/2); return gross; } private static void totalAmounts(double gross, double net) { totalGross += gross; totalNet += net; } }