Suppose you were given the job to write a Java class to manage employee payroll
ID: 639431 • Letter: S
Question
Suppose you were given the job to write a Java class to manage employee payroll information in an HR management system. Each employee will be modeled as a single EmployeePayroll object (much like the Student objects discussed in class). The state of the EmployeePayroll object includes the first name and last name of the employee, a unique 15 digit employee number for each employee, a number of hours that they've worked this week (which could be a fraction of an hour), and the wages per hour the employee earns (measured in dollars and cents). Hours worked and wages per hour are never allowed to fall below zero, and the class should enforce this behavior.
The behavior of the EmployeePayroll object is that any of the above member variables can be accessed (so accessor methods need to be written for them). First name, last name and Employee ID can only be set in a constructor method - there should be no mutators given for these fields, and a special constructor should be written to set these values on initialization. The wages per hour field should have a mutator method (but keep in mind that wages cannot be allowed to be set to less than $0.0). In addition to the above, the class should provide three different ways to change the number of hours worked - on that merely sets the hours as a mutator, one that adds to the hours already in place for the employee, and one that resets the employee's hours to zero. Given the above capabilities of the employee payroll record and the skeleton below, write a Java class that can store an employee payroll record.
2. Override the methods toString() and equals() inherited from Object for this EmployeePayroll class. The toString() method should return the employee's name, ID, and current calculated pay (wages * hours) as a String. The equals() method should return true if two EmployeePayroll objects have the same employee ids, regardless of name, wages or hours.
3.Write a short test program that tests the EmployeePayroll class you implemented above. Your short program should create a few new EmployeePayroll objects and test a variety of conditions, including a number that your class should not allow (such as setting a negative hourly wage).
Explanation / Answer
//EmployeePayroll.java
public class EmployeePayroll
{
// private variables go here
private String firstName;
private String lastName;
private String id;
private int hoursWorked;
private double wages_hour;
//Constructor to set the fields of the class
public EmployeePayroll(String firstName,String lastName,String id )
{
// Fill in the constructor. Default for hours and wages is 0
this.firstName=firstName;
this.lastName=lastName;
this.id=id;
hoursWorked=0;
wages_hour=0;
}
//The metod setHours sets the hours of the employee
//and checks if the hours is negative then display
//and a message that "Hours must be positive"
public void setHours(int hours)
{
if(hours>0)
this.hoursWorked=hours;
else
System.out.println("Hours must be positive");
}
//Method to set the wages of the employee
public void setWage(double wage)
{
wages_hour=wage;
}
// Accessors methods
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getID()
{
return id;
}
public int getHours()
{
return hoursWorked;
}
public double getWage()
{
return wages_hour;
}
//This method should take a number of hours worked
// as an argument and increment the total hours by that amount.
public void incrementHours(int numHours)
{
hoursWorked=hoursWorked+numHours;
}
// This method should reset the variables to start a new week
public void resetPayCycle(int hours,double wage)
{
hoursWorked=hours;
wages_hour=wage;
}
@Override
public String toString()
{
return "Name "+firstName+" "+lastName+" "+
"ID "+id+" "+
"Pay :"+hoursWorked*wages_hour;
}
//The method equals returns true if the employeeid is equal
//otherwise returns false
@Override
public boolean equals(Object obj)
{
// TODO Auto-generated method stub
EmployeePayroll empObject=(EmployeePayroll)obj;
return id.equals(empObject.getID());
}
}
------------------------------------------------------------------------------------------------------------
/**
*
* The java program tests the class EmployeePayroll
* java class by createing the objects of the class
* and tests the variours metods
* and print the corresponding results
* */
//EmployeeDriver.java
public class EmployeeDriver
{
public static void main(String[] args)
{
//Create instance of the EmployeePayRoll class
//and set the fields of class
EmployeePayroll employee1=new EmployeePayroll("Michel", "Johnsom",
"EMPLOYEEID12345");
//set hours and wages
employee1.setHours(40);
employee1.setWage(5);
System.out.println("Employee2 :");
//print the object details
System.out.println(employee1.toString());
//Create instance of the EmployeePayRoll class
//and set the fields of class
EmployeePayroll employee2=new EmployeePayroll("Rhonda", "Bryne",
"EMPLOYEEID12346");
//set hours and wages
employee2.setHours(50);
employee2.setWage(6);
System.out.println("Employee2 :");
//print the object details
System.out.println(employee2.toString());
//checking for setting negative hours
employee2.setHours(-4);
if(employee1.equals(employee2))
System.out.println("Employees are equal");
else
System.out.println("Employees are different");
}
}
--------------------------------------------------------------------------------------------
Sample output:
Employee2 :
Name Michel Johnsom
ID EMPLOYEEID12345
Pay :200.0
Employee2 :
Name Rhonda Bryne
ID EMPLOYEEID12346
Pay :300.0
Hours must be positive
Employees are different
Hope this helps you..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.