In: Computer Science
java
Write our Test Driver program that tests our Classes and Class Relationship
How we calculate Net Pay after calculating taxes and deductions taxes:
regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay )
overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay )
Example printPayStub() output:
Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00 OvertimeGrossPay: $375.00 TotalGrossPay: 1375.00 RegTaxes Deducted: $150.00 ($100.00 State $50.00 Federal ) OvertimeTaxes Deducted: $56.25 ($37.50 State $18.75 Federal) TotalTaxesDeducted: $205.75 ($137.00 State $68.75 Federal) Tax Deductions: $75.00 RegPay, $60.00 Overtime, $15.00 total deductions NetRegPay: $910.00 NetOvertimePay: $333.75 TotalNetPay: $1,243.75 payday: FRIDAY
public class Worker {
// Member variables
private String name;
private String employeeId;
private boolean dayShift;
private double HourlyPay;
// The default constructor
public Worker() {
this.name = "";
this.employeeId = "";
this.dayShift = false;
this.HourlyPay = 0.0;
}
public Worker(String name, String employeeId, boolean dayShift,
double hourlyPay) {
super();
this.name = name;
this.employeeId = employeeId;
this.dayShift = dayShift;
HourlyPay = hourlyPay;
}
public Worker(Worker w) {
this.name = w.name;
this.employeeId = w.employeeId;
this.dayShift = w.dayShift;
this.HourlyPay = w.HourlyPay;
}
// This method sets the name of the worker
public void setName(String n) {
this.name = n;
}
// This method sets the employee Id
public void setemployeeId(String i) {
this.employeeId = i;
}
// This method sets the day shift
public void setdayShift(boolean s) {
this.dayShift = s;
}
// This method sets the hourly pay
public void setHourlyPay(double p) {
this.HourlyPay = p;
}
// This method returns the name of the worker
public String getname() {
return this.name;
}
// This method returns the employee Id
public String getemployeeId() {
return this.employeeId;
}
// This method returns if the worker is in day shift
public boolean getdayShift() {
return this.dayShift;
}
// This method returns the hourly pay
public double getHourlyPay() {
return this.HourlyPay;
}
// This method calculates the overtime pay
public double calculateOvertimePay(int hours) {
return (hours - 40) * 1.5 * HourlyPay;
}
// This method calculates and returns regular pay
public double calculateRegPay(int hours) {
return HourlyPay * hours;
}
@Override
public String toString() {
return "Worker name=" + name + ", employeeId=" + employeeId + ", dayShift=" + dayShift + ", HourlyPay="
+ HourlyPay;
}
}
/**************************************Payroll.java******************************/
public class Payroll {
private Worker w;
private int dependents;
private static final double STATE_TAX = 0.10;
private static final double FED_TAX = 0.05;
public Payroll() {
this.w = new Worker();
this.dependents = 0;
}
public Payroll(String name, String id, double pay, boolean s, int
d) {
this.w = new Worker(name, id, s, pay);
this.dependents = d;
}
public Payroll(Worker w, int dependents) {
super();
this.w = w;
this.dependents = dependents;
}
public Worker getW() {
return w;
}
public void setW(Worker w) {
this.w = w;
}
public int getDependents() {
return dependents;
}
public void setDependents(int dependents) {
this.dependents = dependents;
}
public double calculateStateTax(int hours) {
return (w.calculateRegPay(hours) + w.calculateOvertimePay(hours)) *
STATE_TAX;
}
public double calculateFedTax(int hours) {
return (w.calculateRegPay(hours) + w.calculateOvertimePay(hours)) *
FED_TAX;
}
public void PrintPayStub(int hours) {
double regPay, overPay;
if (hours <= 40) {
// get the regular pay
regPay = w.calculateRegPay(hours) - calculateStateTax(hours) - calculateFedTax(hours)
+ (dependents * .03 * w.calculateRegPay(hours));
overPay = 0;
} else {
// get the overtime pay
regPay = 40 * w.getHourlyPay() - calculateStateTax(hours) - calculateFedTax(hours);
overPay = w.calculateOvertimePay(hours) - calculateStateTax(hours) - calculateFedTax(hours);
}
// display the pay Stub
System.out.println("\nName: \t" + w.getname());
System.out.println("Employee ID: " + w.getemployeeId());
System.out.print("Shift: ");
if (w.getdayShift())
System.out.println("\t\t day");
else
System.out.println("\t\tnight");
System.out.printf("Hourly Pay: $ %7.2f", w.getHourlyPay());
System.out.printf("\nRegular Pay: $ %.2f", regPay);
System.out.printf("\nOvertime Pay: $ %4.2f", overPay);
System.out.printf("\nGross Pay: $ %8.2f\n", (regPay + overPay));
}
}
-----------------
import java.util.Scanner;
public class WorkerPayrollTest {
public static void main(String[] args) {
Worker[] workers = new Worker[5];
Payroll[] payrolls = new Payroll[5];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < workers.length; i++) {
System.out.print("Enter worker name: ");
String name = scan.nextLine();
System.out.print("Enter id: ");
String id = scan.nextLine();
System.out.print("Hourly Pay: ");
double pay = scan.nextDouble();
scan.nextLine();
System.out.print("Is day shift: ");
boolean shift = scan.nextBoolean();
scan.nextLine();
workers[i] = new Worker(name, id, shift, pay);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString());
System.out.print("Enter dependent: ");
int dependents = scan.nextInt();
scan.nextLine();
payrolls[i] = new Payroll(workers[i], dependents);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString()+" dependent: "+payrolls[i].getDependents());
System.out.print("Enter hours worked for "+workers[i].getname()+": ");
int hours = scan.nextInt();
scan.nextLine();
payrolls[i].PrintPayStub(hours);
}
}
}
update 2 methods
1) PrintPayStub in payroll.java
2)calculateRegPay in worker.java
please find the below code for the same
package demo;
public class Worker {
// Member variables
private String name;
private String employeeId;
private boolean dayShift;
private double HourlyPay;
// The default constructor
public Worker() {
this.name = "";
this.employeeId = "";
this.dayShift = false;
this.HourlyPay = 0.0;
}
public Worker(String name, String employeeId, boolean dayShift, double hourlyPay) {
super();
this.name = name;
this.employeeId = employeeId;
this.dayShift = dayShift;
HourlyPay = hourlyPay;
}
public Worker(Worker w) {
this.name = w.name;
this.employeeId = w.employeeId;
this.dayShift = w.dayShift;
this.HourlyPay =
w.HourlyPay;
}
// This method sets the name of the worker
public void setName(String n) {
this.name = n;
}
// This method sets the employee Id
public void setemployeeId(String i) {
this.employeeId = i;
}
// This method sets the day shift
public void setdayShift(boolean s) {
this.dayShift = s;
}
// This method sets the hourly pay
public void setHourlyPay(double p) {
this.HourlyPay = p;
}
// This method returns the name of the worker
public String getname() {
return this.name;
}
// This method returns the employee Id
public String getemployeeId() {
return this.employeeId;
}
// This method returns if the worker is in day
shift
public boolean getdayShift() {
return this.dayShift;
}
// This method returns the hourly pay
public double getHourlyPay() {
return this.HourlyPay;
}
// This method calculates the overtime pay
public double calculateOvertimePay(int hours)
{
return (hours - 40) * 1.5 *
HourlyPay;
}
// This method calculates and returns regular pay
public double calculateRegPay(int hours) {
if(hours>=40) {
return HourlyPay* 40;
}else {
return
HourlyPay* hours;
}
}
@Override
public String toString() {
return "Worker name=" + name + ", employeeId=" + employeeId + ", dayShift=" + dayShift + ", HourlyPay="
+ HourlyPay;
}
}
package demo;
public class Payroll {
private Worker w;
private int dependents;
private static final double STATE_TAX = 0.10;
private static final double FED_TAX = 0.05;
public Payroll() {
this.w = new Worker();
this.dependents = 0;
}
public Payroll(String name, String id, double pay,
boolean s, int d) {
this.w = new Worker(name, id, s,
pay);
this.dependents = d;
}
public Payroll(Worker w, int dependents) {
super();
this.w = w;
this.dependents = dependents;
}
public Worker getW() {
return w;
}
public void setW(Worker w) {
this.w = w;
}
public int getDependents() {
return dependents;
}
public void setDependents(int dependents) {
this.dependents = dependents;
}
public double calculateStateTax(int hours) {
return (w.calculateRegPay(hours) +
w.calculateOvertimePay(hours)) * STATE_TAX;
}
public double calculateFedTax(int hours) {
return (w.calculateRegPay(hours) +
w.calculateOvertimePay(hours)) * FED_TAX;
}
public void PrintPayStub(int hours) {
double regPay, overPay;
if (hours <= 40) {
// get the regular pay
regPay =
w.calculateRegPay(hours) - calculateStateTax(hours) -
calculateFedTax(hours)
+ (dependents * .03 *
w.calculateRegPay(hours));
overPay =
0;
} else {
// get the overtime pay
regPay = 40 *
w.getHourlyPay() - calculateStateTax(40) - calculateFedTax(40)+
(dependents * .03 * w.calculateRegPay(40));
overPay =
w.calculateOvertimePay(hours) -
(calculateStateTax(hours)-calculateStateTax(40)) -
(calculateFedTax(hours)-calculateFedTax(40))
+ (dependents * .02 *
w.calculateOvertimePay(hours));
}
// display the pay Stub
System.out.println("\nName: \t" +
w.getname());
System.out.println("Employee ID: "
+ w.getemployeeId());
System.out.print("Shift: ");
if (w.getdayShift())
System.out.println("\t\t day");
else
System.out.println("\t\tnight");
System.out.printf("Hourly Pay: $
%7.2f", w.getHourlyPay());
System.out.printf("\nRegular Pay: $
%.2f", regPay);
System.out.printf("\nOvertime Pay:
$ %4.2f", overPay);
System.out.printf("\nGross Pay: $
%8.2f\n", (regPay + overPay));
}
}
package demo;
import java.util.Scanner;
public class WorkerPayrollTest {
public static void main(String[] args) {
Worker[] workers = new
Worker[5];
Payroll[] payrolls = new
Payroll[5];
Scanner scan = new
Scanner(System.in);
for (int i = 0; i <
workers.length; i++) {
System.out.print("Enter worker name: ");
String name = scan.nextLine();
System.out.print("Enter id: ");
String id = scan.nextLine();
System.out.print("Hourly Pay: ");
double pay = scan.nextDouble();
scan.nextLine();
System.out.print("Is day shift: ");
boolean shift = scan.nextBoolean();
scan.nextLine();
workers[i] = new
Worker(name, id, shift, pay);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString());
System.out.print("Enter dependent: ");
int dependents = scan.nextInt();
scan.nextLine();
payrolls[i] = new Payroll(workers[i], dependents);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString() + " dependent: " + payrolls[i].getDependents());
System.out.print("Enter hours worked for " + workers[i].getname() + ": ");
int hours = scan.nextInt();
scan.nextLine();
payrolls[i].PrintPayStub(hours);
}
}
}