/* Author: Johnny A. Shaieb Program: Lab4 - lab4.java Date: 2-13-2002 4:30 Instructor: Gabriela Perez */ import java.text.NumberFormat; public class lab4 { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.print("Hours Worked ==>"); double h = console.readDouble(); System.out.print("Hourly Rate ==>"); double w = console.readDouble(); System.out.print("# of Dependents: ==>"); int d = console.readInt(); Employee info = new Employee(h,w,d); info.Display(); System.out.println(); } } class Employee { public Employee(double h, double w, int d) { hours = h; wage = w; depend = d; } public Employee(Employee e) { hours = e.hours; wage = e.wage; depend = e.depend; } public double grossPay() { int cond1 = 40; int cond2 = 60; double diff, diff1, diff2, bf, ot, ot1, ot2; double gp = 0; if(hours > cond1) { //if(hours <= 60) if(hours <= cond2) { diff = hours - cond1; //before forty bf = wage * cond1; //time in half ot = (wage + wage/2) * diff; gp = bf + ot; } //else if(hours > 60) else if(hours > cond2) { //before forty bf = wage * cond1; //20 hours diff1 = cond2 - cond1; //time in half ot1 = (wage + wage/2) * diff1; diff2 = hours - cond2; //over 60 ot2 = (2 * wage) * diff2; gp = ot1 + ot2; } } else { //forty hours bf = wage * cond1; gp = bf; } return gp; } public double getTax() { double tax = 0.10; double drate = 25; double fedtax = (grossPay() * tax) - (depend * drate); if(fedtax < 0) { fedtax = 0; } return fedtax; } public double getTakeHome() { double takehome = grossPay() - getTax(); return takehome; } public void Display() { NumberFormat formatter = NumberFormat.getCurrencyInstance(); formatter.setMaximumFractionDigits(2); formatter.setMinimumFractionDigits(2); System.out.println(); System.out.println("The Hours Worked : " + (new Sprintf(formatter.format(hours),10,"r")).formatIt()); System.out.println("The Hourly Rate : " + (new Sprintf(formatter.format(wage),10,"r")).formatIt()); System.out.println("Number of Dependents: " + (new Sprintf(depend,10,"r")).formatIt()); System.out.println("Gross Income : " + (new Sprintf(formatter.format(grossPay()),10,"r")).formatIt()); System.out.println("Federal Tax : " + (new Sprintf(formatter.format(getTax()),10,"r")).formatIt()); System.out.println("Take Home Pay : " + (new Sprintf(formatter.format(getTakeHome()),10,"r")).formatIt()); } private double hours; private double wage; private int depend; } class Sprintf { public Sprintf(String sValue, int spaceValue, String strJustify) { str = sValue; spacing = spaceValue; justify = strJustify; } public Sprintf(int sValue, int spaceValue, String strJustify) { str = Integer.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(double sValue, int spaceValue, String strJustify) { str = Double.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(Sprintf o) { str = o.str; spacing = o.spacing; justify = o.justify; } public String formatIt() { int l = str.length(); String strFormat; if(l < spacing) { int diff = spacing - l; String spaces = ""; for(int k=1; k<=diff; k++) { spaces = spaces + " "; } if(justify.equalsIgnoreCase("l")) { strFormat = str + spaces; } else if(justify.equalsIgnoreCase("r")) { strFormat = spaces + str; } else { //if user value does not match l or r, //r will be default strFormat = spaces + str; } } else { strFormat = str; } return strFormat; } public void sprintfDisplay() { System.out.println(formatIt()); } private String str; private int spacing; private String justify; }