HW6: Paystub for Employee CSS 161 Fundamentals of Computing By: Hansel Ong Summa
ID: 3793951 • Letter: H
Question
HW6: Paystub for Employee CSS 161 Fundamentals of Computing By: Hansel Ong Summary So far you have kept track of hours worked for only one employee. However, this is obviously not sustainable as companies typically have more than one employee some companies even have over 100,000 employees! Let's use the object-oriented programming (ooP) paradigm to simplify data collection and storage. Estimated Work Needed This assignment took me about 30-45 minutes to write (not including challenges, but including testing, commenting, and cleanup) in less than 200 lines of code (total across three Java files). In other words, you should expect to spend between 135 to 450 minutes working on this assignment. If you've spent more than 7.5 hours working on this assignment, then you are likely struggling with arrays, loops, basic class design, methods, and the use of the new scope and should re-read the lecture slides (and attempt the exercises within), seek help from your fellow classmates, myself, your lab instructor, QSC tutor, as well as online resources. Skills Expected All the skills from previous Assignment(s) Class Object and Design, including but not limited to o Instance Variables o Getter and Setter (including input validation in Setters) o Constructor o Methods Assignment Description You will write three Class objects (and subsequently submit three java files): Pay stub Employee Employee Driver Notes: ONLY the EmployeeDriver class should have static methods. All the other classes should NOT have static methods or instance variables. Constants should still be static final. The below is the minimum you must include -you could include additional instance variables, methods, etc. as you needExplanation / Answer
Hi,
Please see below the java classes. Please comment for any queries or feedbacks:
Thanks,
Anita
//--------------------------------------------
//Paystub.java
//--------------------------------------------
public class Paystub {
private double hourlyRate;
private double [] hoursWorked;
public static final double MEDICAL_INSURANCE = 50;
public static final double REGULAR_HOURS = 8;
public static final double TAX_RATE = .3;
//Constructor
public Paystub(){
this.hourlyRate = 15;
hoursWorked = new double [14];
}
/***
* To edit the hours in 2 weeks
* @param int week, int day, double hours
* return void
***/
public void editHours(int week, int day, double hours){
int index = 0;
if((week == 2 || week ==1 ) && (day >0 && day<= 7)){
if(1== week){
index = day - 1 ;
}
else if(2 == week){
index = 7 + (day - 1);
}
this.hoursWorked[index] = hours;
}
else{
System.out.println("Invalid week or day !");
}
}
/***
* to display the hours worked in
* @param
* return void
***/
public void printHours(){
for (int i = 0; i<hoursWorked.length ; i++){
if (i<7){
System.out.println("Week 1, "+ "Day "+i+", Worked: "+hoursWorked[i] );
}
else{
int dayTemp = (i+1)-7;
System.out.println("Week 2, "+ "Day "+dayTemp+", Worked: "+hoursWorked[i] );
}
}
}
/***
* To calculate the regular hours worked
* @param
* return double
***/
public double regularHours(){
double regularHours =0 ;
for (int i = 0; i<hoursWorked.length ; i++){
double regHoursWorked = hoursWorked[i];
if(regHoursWorked<=REGULAR_HOURS){
regularHours = regularHours + regHoursWorked;
}
else {
regularHours = regularHours + REGULAR_HOURS;
}
}
return regularHours;
}
/***
* To calculate the overtime hours worked
* @param
* return double
***/
public double overTimeHours(){
double overTimeHours =0 ;
double overTimeHoursWorked = 0;
for (int i = 0; i<hoursWorked.length ; i++){
overTimeHoursWorked = hoursWorked[i];
if(overTimeHoursWorked > REGULAR_HOURS){
overTimeHours = overTimeHours + ( overTimeHoursWorked -REGULAR_HOURS);
}
}
return overTimeHours;
}
/***
* To print the Paystub details
* @param
* return void
***/
public void printPayStub(){
System.out.println(" HourlyRate : $"+this.getHourlyRate());
System.out.println("Worked Hour details :");
printHours();
System.out.println("Regular Hours Worked : "+regularHours());
System.out.println("Overtime Hours Worked : "+overTimeHours());
double totalRate = (regularHours()+overTimeHours()) * hourlyRate;
System.out.println("Gross Income : $"+String.format("%.02f", totalRate));
System.out.println("Deductions : $"+totalRate * TAX_RATE);
System.out.println("Take Home Income : $"+String.format("%.02f", (totalRate - (totalRate*TAX_RATE))));
}
//Getters and setters
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
}
//--------------------------------------------
//Employee.java
//--------------------------------------------
public class Employee {
private double hourlyRate;
private Paystub [] paystubArray;
private int counter=0;
//Constructor
public Employee(double hourlyRate){
this.hourlyRate = hourlyRate;
paystubArray =new Paystub[1];
}
/***
* To add paystub top the array
* @param Paystub paystub
* return void
***/
public void addPaystub(Paystub paystub){
if(this.counter<10){
this.paystubArray[counter] = paystub;
counter++;
}
}
/***
* To print the Employee Income History
* @param
* return void
***/
public void printIncomeHistory(){
System.out.println("Income History : ");
for(int i=0;i<this.paystubArray.length ;i++){
Paystub paystub = paystubArray[i];
paystub.printPayStub();
}
}
//Getters and setters
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
}
//--------------------------------------------
//EmployeeDriver.java
//--------------------------------------------
public class EmployeeDriver {
public static void main(String [] args){
//Create paystub
Paystub paystub1 = new Paystub();
//Edit Hours
paystub1.editHours(1, 1, 8);
paystub1.editHours(1, 2, 9);
paystub1.editHours(1, 3, 10);
paystub1.editHours(1, 4, 11);
paystub1.editHours(1, 5, 5);
paystub1.editHours(1, 6, 7);
paystub1.editHours(1, 7, 6);
paystub1.editHours(2, 1, 10);
paystub1.editHours(2, 2, 11);
paystub1.editHours(2, 3, 9);
paystub1.editHours(2, 4, 8);
paystub1.editHours(2, 5, 7);
paystub1.editHours(2, 6, 9);
paystub1.editHours(2, 7, 7);
//Print hours
paystub1.printHours();
//Create Employee with hourly rate $17.56
Employee emp = new Employee(17.56);
//Create another instance of Paystub with new hours and update the hourly rate
Paystub paystub2 = new Paystub();
paystub2.setHourlyRate(17.56);
paystub2.editHours(1, 1, 8);
paystub2.editHours(1, 2, 9);
paystub2.editHours(1, 3, 10);
paystub2.editHours(1, 4, 11);
paystub2.editHours(1, 5, 5);
paystub2.editHours(1, 6, 7);
paystub2.editHours(1, 7, 6);
paystub2.editHours(2, 1, 10);
paystub2.editHours(2, 2, 11);
paystub2.editHours(2, 3, 9);
paystub2.editHours(2, 4, 8);
paystub2.editHours(2, 5, 7);
paystub2.editHours(2, 6, 9);
paystub2.editHours(2, 7, 7);
//Adding paystub to employee
emp.addPaystub(paystub2);
emp.printIncomeHistory();
}
}
Sample output:
Week 1, Day 0, Worked: 8.0
Week 1, Day 1, Worked: 9.0
Week 1, Day 2, Worked: 10.0
Week 1, Day 3, Worked: 11.0
Week 1, Day 4, Worked: 5.0
Week 1, Day 5, Worked: 7.0
Week 1, Day 6, Worked: 6.0
Week 2, Day 1, Worked: 10.0
Week 2, Day 2, Worked: 11.0
Week 2, Day 3, Worked: 9.0
Week 2, Day 4, Worked: 8.0
Week 2, Day 5, Worked: 7.0
Week 2, Day 6, Worked: 9.0
Week 2, Day 7, Worked: 7.0
Income History :
HourlyRate : $17.56
Worked Hour details :
Week 1, Day 0, Worked: 8.0
Week 1, Day 1, Worked: 9.0
Week 1, Day 2, Worked: 10.0
Week 1, Day 3, Worked: 11.0
Week 1, Day 4, Worked: 5.0
Week 1, Day 5, Worked: 7.0
Week 1, Day 6, Worked: 6.0
Week 2, Day 1, Worked: 10.0
Week 2, Day 2, Worked: 11.0
Week 2, Day 3, Worked: 9.0
Week 2, Day 4, Worked: 8.0
Week 2, Day 5, Worked: 7.0
Week 2, Day 6, Worked: 9.0
Week 2, Day 7, Worked: 7.0
Regular Hours Worked : 104.0
Overtime Hours Worked : 13.0
Gross Income : $2054.52
Deductions : $616.356
Take Home Income : $1438.16
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.