COSC 210-Cosc 210-object oriented and GUI Programming Assignment 3 Problem State
ID: 3807462 • Letter: C
Question
COSC 210-Cosc 210-object oriented and GUI Programming Assignment 3 Problem Statement BoxMart wants a new payroll application to generate pay checks and pay stubs for its employees. Each employee clocks in at that start of a work period and clock outs at the end of the period, this information is recorded in a time entry BoxMart Pay Report Employee No: 48399 Employee Name: Bill Jones Pay Period: 01/14/2012 to 01/20/2012 Normal Hours Worked 40 Overtime Hours Worked: 8 hrs Hourly Wage 12.25/hr Normal Pay: 490.00 Overtime Pay: 147.00 Gross Pay: 637.00 Federal Tax 13.70 State Tax 19.11 Net Pay: 604.19 Time Log In Out Hours OT Hrs Date 01/14/2012 8:00 17:00 01/15/2012 8:00 20:00 01/16/2012 12:00 19:00 01/17/2012 12:00 19:00 01/18/2012 8:00 17:00 01/19/2012 11:00 18:00 01/20/2012 9:00 12:00 01/27/2012 Pay Check 604.19 Pay to: Bill JonesExplanation / Answer
Employee.java
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Iterator;
import java.util.TreeMap;
public class Employee {
private String empNo;
private String empName;
private double hourelyWage;
private TreeMap<LocalDate, TimeEntry> workPeriodList = new TreeMap<>();
public Employee(String empNo, String empName, double hourelyWage) {
this.empNo = empNo;
this.empName = empName;
this.hourelyWage = hourelyWage;
}
public void addEntry(LocalTime inTime, LocalTime outTime, LocalDate date) {
TimeEntry timeEntry = new TimeEntry(inTime, outTime, date);
workPeriodList.put(date, timeEntry);
}
/**
* @return the hourelyWage
*/
public double getHourelyWage() {
return hourelyWage;
}
/**
* @param hourelyWage the hourelyWage to set
*/
public void setHourelyWage(double hourelyWage) {
this.hourelyWage = hourelyWage;
}
/**
* @return the empNo
*/
public String getEmpNo() {
return empNo;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @return the workPeriodList
*/
public TreeMap<LocalDate, TimeEntry> getWorkPeriodList() {
return workPeriodList;
}
public LocalDate getPayPeriodStart() {
LocalDate d = null;
for(Iterator<LocalDate> iterator = workPeriodList.keySet().iterator(); iterator.hasNext();) {
LocalDate date = iterator.next();
if(d== null || date.isBefore(d)) {
d = date;
}
}
return d;
}
public LocalDate getPayPeriodEnd() {
LocalDate d = null;
for(Iterator<LocalDate> iterator = workPeriodList.keySet().iterator(); iterator.hasNext();) {
LocalDate date = iterator.next();
if(d== null || date.isAfter(d)) {
d = date;
}
}
return d;
}
}
TimeEntry.java:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class TimeEntry {
private LocalTime inTime, outTime;
private LocalDate date;
private long normalHours, overtimeHours;
public TimeEntry(LocalTime inTime, LocalTime outTime, LocalDate date) {
this.inTime = inTime;
this.outTime = outTime;
this.date = date;
long hoursGap = calculateHoursGap(inTime, outTime);
if(hoursGap > 6) {
// reduce 1 hour for lunch time
hoursGap -= 1;
}
// for more than 8 hours, consider excess hours as overtime.
if(hoursGap > 8) {
normalHours = 8;
overtimeHours = hoursGap - 8;
} else {
normalHours = hoursGap;
overtimeHours = 0;
}
}
// find total hours worked in a day
private long calculateHoursGap(LocalTime inTime, LocalTime outTime) {
return inTime.until(outTime, ChronoUnit.HOURS);
}
/**
* @return the inTime
*/
public LocalTime getInTime() {
return inTime;
}
/**
* @return the outTime
*/
public LocalTime getOutTime() {
return outTime;
}
/**
* @return the date
*/
public LocalDate getDate() {
return date;
}
/**
* @return the normalHours
*/
public long getNormalHours() {
return normalHours;
}
/**
* @return the overtimeHours
*/
public long getOvertimeHours() {
return overtimeHours;
}
}
PayStub.java:
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Iterator;
import java.util.TreeMap;
/*
* A static class just to print the paycheck and report for an employee
* contains static methods
*/
public class PayStub {
private static DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("MM/dd/yyyy");
private static DecimalFormat decimalFormatter = new DecimalFormat("###.00");
// to print the overall report
public static void calculateAndPrintReport(Employee employee) {
double grossPay = printPayReport(employee);
printPayCheck(employee, grossPay);
}
// to print pay report
public static double printPayReport(Employee employee) {
System.out.println(" Bookmart Pay Report ");
System.out.println("Employee No: " + employee.getEmpNo());
System.out.println("Employee Name: " + employee.getEmpName());
System.out.println("Pay Period: "
+ formatter.format(employee.getPayPeriodStart()) + " to "
+ formatter.format(employee.getPayPeriodEnd()));
// get all the time entries
TreeMap<LocalDate, TimeEntry> workPeriodRecords = employee
.getWorkPeriodList();
double normalHours = 0, overtimeHours = 0;
for (Iterator<LocalDate> iterator = workPeriodRecords.keySet()
.iterator(); iterator.hasNext();) {
LocalDate date = iterator.next();
TimeEntry timeEntry = workPeriodRecords.get(date);
normalHours += timeEntry.getNormalHours();
overtimeHours += timeEntry.getOvertimeHours();
}
// if total normal hours are more than 40, make the overtime
if (normalHours > 40) {
overtimeHours += normalHours - 40;
normalHours = 40;
}
System.out.println();
System.out.println("Normal Hours Worked: "
+ String.format("%10s", normalHours + " hrs"));
System.out.println("Overtime Hours Worked: "
+ String.format("%10s", overtimeHours + " hrs"));
System.out.println("Hourly Wage: $"
+ String.format("%10s", employee.getHourelyWage() + "/hr"));
System.out.println();
System.out.println("Normal pay: $"
+ String.format(
"%10s",
decimalFormatter.format(normalHours
* employee.getHourelyWage())));
System.out.println("Overtime pay: $"
+ String.format(
"%10s",
decimalFormatter.format(overtimeHours
* employee.getHourelyWage() * 1.5)));
double grossPay = normalHours * employee.getHourelyWage()
+ overtimeHours * employee.getHourelyWage() * 1.5;
System.out.println("Gross pay: $"
+ String.format("%10s", decimalFormatter.format(grossPay)));
double federalTax = 0, stateTax = grossPay * 0.03;
if (grossPay > 500) {
federalTax = 0.1 * (grossPay - 500);
}
double netPay = (grossPay - federalTax - stateTax);
System.out.println("Federal Tax: $"
+ String.format("%10s", decimalFormatter.format(federalTax)));
System.out.println("State Tax: $"
+ String.format("%10s", decimalFormatter.format(stateTax)));
System.out.println("Net pay: $"
+ String.format("%10s", decimalFormatter.format(netPay)));
// print time entries
System.out.println(" Time Log");
System.out.println("Date In Out Hours OT Hours");
// keep checking total normal hour
// once total normal hours becomes greater than 40, we need to
// convert the normal hours into overtime hours, and print accordingly
// we just need to put this logic for printing
// for computing the grossPay, we already have implemented this logic
double totalNormalHours = 0;
for (Iterator<LocalDate> iterator = workPeriodRecords.keySet()
.iterator(); iterator.hasNext();) {
LocalDate date = iterator.next();
TimeEntry timeEntry = workPeriodRecords.get(date);
double thisNormal = timeEntry.getNormalHours();
// if adding this entry doesn't make totalNormal hours greater than 40
if(totalNormalHours + thisNormal <= 40) {
totalNormalHours += thisNormal;
System.out.println(formatter.format(date)
+ " "
+ timeEntry.getInTime()
+ " "
+ timeEntry.getOutTime()
+ " "
+ (int)timeEntry.getNormalHours()
+ " "
+ ((timeEntry.getOvertimeHours() > 0) ? (int)timeEntry
.getOvertimeHours() : ""));
// if totalNormal hours are already greater than 40 without adding this entry
} else if(totalNormalHours >= 40){
System.out.println(formatter.format(date)
+ " "
+ timeEntry.getInTime()
+ " "
+ timeEntry.getOutTime()
+ " "
+ "" // don't show normal hours
+ " "
+ (int)(timeEntry.getOvertimeHours() + timeEntry.getNormalHours()));
// if totalNormal hours are less than 40 now, but adding this entry will
// make totalNormal hours greater than 40
} else {
// adding this record will make the overall normal hours more than 40;
System.out.println(formatter.format(date)
+ " "
+ timeEntry.getInTime()
+ " "
+ timeEntry.getOutTime()
+ " "
+ (int)(40 - totalNormalHours) // just put enough normal hours to make overall 40
+ " "
// add remaining normal hours to overall
+ (int)(timeEntry.getOvertimeHours() + (thisNormal - (40 - totalNormalHours))));
totalNormalHours += thisNormal;
}
}
System.out.println(" ");
return netPay;
}
// to print employee paycheck
public static void printPayCheck(Employee employee, double grossPay) {
System.out.println("Pay Check "
+ formatter.format(employee.getPayPeriodStart().plus(13,
ChronoUnit.DAYS)));
System.out.println(" Pay to:" + employee.getEmpName() + " $"
+ String.format("%10s", decimalFormatter.format(grossPay)));
}
}
TestPayroll.java:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
public class TestPayroll {
public static void main(String args[]) {
Employee emp1 = new Employee("1234", "Emp1", 12.25);
Employee emp2 = new Employee("1234", "Emp1", 7.5);
emp1.addEntry(LocalTime.of(8, 00), LocalTime.of(17, 00), LocalDate.of(2017, Month.JANUARY, 14));
emp1.addEntry(LocalTime.of(8, 00), LocalTime.of(20, 00), LocalDate.of(2017, Month.JANUARY, 15));
emp1.addEntry(LocalTime.of(12, 00), LocalTime.of(19, 00), LocalDate.of(2017, Month.JANUARY, 16));
emp1.addEntry(LocalTime.of(12, 00), LocalTime.of(19, 00), LocalDate.of(2017, Month.JANUARY, 17));
emp1.addEntry(LocalTime.of(8, 00), LocalTime.of(17, 00), LocalDate.of(2017, Month.JANUARY, 18));
emp1.addEntry(LocalTime.of(11, 00), LocalTime.of(18, 00), LocalDate.of(2017, Month.JANUARY, 19));
emp1.addEntry(LocalTime.of(9, 00), LocalTime.of(12, 00), LocalDate.of(2017, Month.JANUARY, 20));
emp2.addEntry(LocalTime.of(8, 00), LocalTime.of(17, 00), LocalDate.of(2017, Month.JANUARY, 14));
emp2.addEntry(LocalTime.of(8, 00), LocalTime.of(20, 00), LocalDate.of(2017, Month.JANUARY, 15));
emp2.addEntry(LocalTime.of(12, 00), LocalTime.of(19, 00), LocalDate.of(2017, Month.JANUARY, 16));
emp2.addEntry(LocalTime.of(12, 00), LocalTime.of(19, 00), LocalDate.of(2017, Month.JANUARY, 17));
emp2.addEntry(LocalTime.of(8, 00), LocalTime.of(17, 00), LocalDate.of(2017, Month.JANUARY, 18));
emp2.addEntry(LocalTime.of(11, 00), LocalTime.of(18, 00), LocalDate.of(2017, Month.JANUARY, 19));
emp2.addEntry(LocalTime.of(9, 00), LocalTime.of(12, 00), LocalDate.of(2017, Month.JANUARY, 20));
System.out.println("+++++++++++++++++++++++++++++++++++++++ Employee1 ++++++++++++++++++++++++++++++++++++++++++");
PayStub.calculateAndPrintReport(emp1);
System.out.println(" +++++++++++++++++++++++++++++++++++++++ Employee2 ++++++++++++++++++++++++++++++++++++++++++");
PayStub.calculateAndPrintReport(emp2);
}
}
Sample Output:
+++++++++++++++++++++++++++++++++++++++ Employee1 ++++++++++++++++++++++++++++++++++++++++++
Bookmart Pay Report
Employee No: 1234
Employee Name: Emp1
Pay Period: 01/14/2017 to 01/20/2017
Normal Hours Worked: 40.0 hrs
Overtime Hours Worked: 8.0 hrs
Hourly Wage: $ 12.25/hr
Normal pay: $ 490.00
Overtime pay: $ 147.00
Gross pay: $ 637.00
Federal Tax: $ 13.70
State Tax: $ 19.11
Net pay: $ 604.19
Time Log
Date In Out Hours OT Hours
01/14/2017 08:00 17:00 8
01/15/2017 08:00 20:00 8 3
01/16/2017 12:00 19:00 6
01/17/2017 12:00 19:00 6
01/18/2017 08:00 17:00 8
01/19/2017 11:00 18:00 4 2
01/20/2017 09:00 12:00 3
Pay Check 01/27/2017
Pay to:Emp1 $ 604.19
+++++++++++++++++++++++++++++++++++++++ Employee2 ++++++++++++++++++++++++++++++++++++++++++
Bookmart Pay Report
Employee No: 1234
Employee Name: Emp1
Pay Period: 01/14/2017 to 01/20/2017
Normal Hours Worked: 40.0 hrs
Overtime Hours Worked: 8.0 hrs
Hourly Wage: $ 7.5/hr
Normal pay: $ 300.00
Overtime pay: $ 90.00
Gross pay: $ 390.00
Federal Tax: $ 0.00
State Tax: $ 11.70
Net pay: $ 378.30
Time Log
Date In Out Hours OT Hours
01/14/2017 08:00 17:00 8
01/15/2017 08:00 20:00 8 3
01/16/2017 12:00 19:00 6
01/17/2017 12:00 19:00 6
01/18/2017 08:00 17:00 8
01/19/2017 11:00 18:00 4 2
01/20/2017 09:00 12:00 3
Pay Check 01/27/2017
Pay to:Emp1 $ 378.30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.