Question: Payroll class Design a Payroll class that has fields for an employees name, ID number, hourly pay rate, and number of hours worked. Write the

Payroll class

Design a Payroll class that has fields for an employees name, ID number, hourly pay rate, and number of hours worked. Write the appropriate accessor and mutator methods and a constructor that accepts the employees name and ID number as arguments. The class should also have a method that returns the employees gross pay, which is calculated as the number of hours worked multiplied by the hourly pay rate. * here is the orginal question* the part i am struggling with is: Each method ( the methods needing junit tests: private String name; private int id; private double rate; private double hoursWorked ) for example should have unit tests covering valid data, as well as exceptions. here is the code i have. it all seems to work i just need help with the unit tests. thank you.

import java.io.*;

public class Payroll implements Serializable { private String name; private int id; private double rate; private double hoursWorked;

private static final long serialVersionUID = 1L;

public Payroll(String name, int id) throws InvalidNameException, InvalidIDException { setName(name); setId(id); }

public String getName() { return name; }

public void setName(String empName) throws InvalidNameException { if (empName == null || empName.equals("")) { throw new InvalidNameException("Invalid Name. Name can not be empty"); } this.name = empName; }

public int getId() { return id; }

public void setId(int empID) throws InvalidIDException {

if (empID <= 0) { throw new InvalidIDException("EmpID can not be negative or zero"); } this.id = empID; }

public double getHourlyRate() { return rate; }

public void setHourlyRate(double rate) throws HourlyRateException {

if (rate < 0 || rate > 25) { throw new HourlyRateException("rate can not be negative or greater than 25"); } this.rate = rate; }

public double getHoursWorked() { return hoursWorked; }

public void setHoursWorked(double hours) throws ValidHoursException { if(hours < 0 || hours > 84 ){ throw new ValidHoursException("hours can not be negative or greater than 84"); } this.hoursWorked = hours; } public double getGrossPay() { return rate * hoursWorked; } }

/** * Auto Generated Java Class. */

import java.util.Scanner; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.*; // TestPayroll.javan public class TestPayroll {

// this method serialize a payroll object int file payroll.ser public void serializePayroll(Payroll payroll) {

FileOutputStream fout = null; ObjectOutputStream oos = null;

try {

fout = new FileOutputStream("payroll.ser"); oos = new ObjectOutputStream(fout); oos.writeObject(payroll);

System.out.println("Done. File written to " + System.getProperty("user.dir"));

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (fout != null) { try { fout.close(); } catch (IOException e) { e.printStackTrace(); } }

if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } }

} }

public static void main(String[] args)throws HourlyRateException,InvalidIDException,ValidHoursException,InvalidNameException { Scanner in = new Scanner(System.in); String name; int empID; System.out.print("Enter employee's name: "); name = in.next(); System.out.print("Enter employee's empID: "); empID = in.nextInt(); Payroll payroll = new Payroll(name, empID); System.out.print("Enter employee's hourly rate: ");

double rate = in.nextDouble(); payroll.setHourlyRate(rate); System.out.print("Enter employee's number of hours worked: "); double hours = in.nextDouble();

payroll.setHoursWorked(hours);

// Payroll.main(name, empID, rate, hours);

System.out.println("Gross pay is " + payroll.getGrossPay());

}

}

(unit test in: new... test file or new junit test depending on what ide you are using)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!