/* Method8.java CIS 160 10/18/04 payroll calculation example using methods */ import java.io.*; import java.text.*; 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(); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("Totals" + leftPad(df.format(totalGross),54) + leftPad(df.format(totalNet),10)); } 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) { DecimalFormat df = new DecimalFormat("0.00"); System.out.println(rightPad(eName, 20) + leftPad(df.format(hRate), 10) + leftPad(df.format(hours), 10) + leftPad(df.format(taxRate), 10) + leftPad(df.format(gross), 10) + leftPad(df.format(net), 10)); } private static String leftPad(String s, int width) { if (width < s.length()) s = s.substring(0,width); while (width > s.length()) s = " " + s; return s; } private static String rightPad(String s, int width) { if (width < s.length()) s = s.substring(0,width); while (width > s.length()) s += " "; return s; } 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; } }