Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Suppose you were given the job to write a Java class to manage employee payrol

ID: 639659 • Letter: #

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 singleEmployeePayroll 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.

- 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.

- 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

Program Code:

import java.text.DecimalFormat;

import java.util.Objects;

public class EmployeePayroll

{

     String firstName;

     String lastName;

     long id;

     double hours;

     double wages;

     DecimalFormat df=new DecimalFormat("######.##");

     // private variables go here

     public EmployeePayroll(String fname, String lname, long number)

     {

          firstName=fname;

          lastName=lname;

          id=number;

          // Fill in the constructor. Default for hours and wages is 0

     }

     // write accessors as described above

     // write mutators as described above

     public void incrementHours(double hours)

     {

          this.hours+=hours;

     }

     public void resetPayCycle(double hours)

     {

          this.hours=hours;

     }

     public String getFirstName()

     {

          return firstName;

     }

     public String getLastName()

     {

          return lastName;

     }

     public long getId()

     {

          return id;

     }

     public double getHours()

     {

          return hours;

     }

     public double getWages()

     {

          return wages;

     }

     public void setHours(double hours)

     {        

          this.hours = hours;    

     }

    

    

     public double computePay()

     {

          double payment=this.hours*this.wages;

          return payment;

     }

     public void setWages(double wages)

     {

         

          this.wages = wages;

         

     }

     public String toString()

     {

          String s="Employee's Name: "+getFirstName()+" "+getLastName();

          s+=" Employee ID: "+getId()+" ";

          s+="Employee Payment: "+df.format(computePay())+" ";

          return s;

     }

     public boolean equals(Object obj)

     {

          if(super.equals(obj))

          {

              return Objects.equals(getId(),

                        ((EmployeePayroll)(obj)).getId());

          }

          else

          {

              return false;

          }

     }

}

import java.util.*;

public class EmployeePayImplementation

{

     public static void main(String args[])

     {

          ArrayList<EmployeePayroll> epr=new ArrayList<EmployeePayroll>();

          Scanner in=new Scanner(System.in);

          String fname, lname;

          String id;

          for(int i=0;i<3;i++)

          {

              System.out.println("Enter the first name: ");

              fname=in.next();

              System.out.println("Enter the last name: ");

              lname=in.next();

              System.out.println("Enter the id name: ");

              id=in.next();

              if(id.length()<15)

              {

                   System.out.println("Please enter the id of 15 digits: ");

                   id=in.next();

              }

              EmployeePayroll ep=new EmployeePayroll(fname, lname, Long.parseLong(id));

              System.out.println("Enter the wages per hour: ");

              double wages=in.nextDouble();

              if(wages<0)

              {

                   System.out.println("Sorry! Wages cannot be less than zero.");

                   wages=in.nextDouble();

              }

              ep.setWages(wages);

              System.out.println("Enter the number of hours worked: ");

              double hours=in.nextDouble();

             

              if(hours<0)

              {

                   System.out.println("Sorry! Hours cannot be less than zero.");

                   hours=in.nextDouble();

              }

              ep.setHours(hours);

              epr.add(ep);

          }

    

                  

          for(int i=0;i<epr.size();i++)

          {

              System.out.println(epr.get(i).toString());

          }

         

          System.out.println("Working of the equals() method: ");

          for(int i=1;i<epr.size();i++)

          {            

              System.out.println("ids are equal "+epr.get(0).equals(epr.get(i)));                   

          }

         

     }

}

------------------------------------------------------------------------------------------------

Sample output:

Enter the first name:

James

Enter the last name:

Cameron

Enter the id name:

123451234512345

Enter the wages per hour:

123.50

Enter the number of hours worked:

38.0

Enter the first name:

Hillary

Enter the last name:

James

Enter the id name:

123451234512345

Enter the wages per hour:

200.5

Enter the number of hours worked:

38.0

Enter the first name:

Kevin

Enter the last name:

Neo

Enter the id name:

987987123123456

Enter the wages per hour:

140.8

Enter the number of hours worked:

38.9

Employee's Name: James Cameron

Employee ID: 123451234512345

Employee Payment: 4693

Employee's Name: Hillary James

Employee ID: 123451234512345

Employee Payment: 7619

Employee's Name: Kevin Neo

Employee ID: 987987123123456

Employee Payment: 5477.12

Working of the equals() method:

ids are equal false

ids are equal false