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(); } }

} }

here are the tests i have started on(i dont think that they are correct) please help me correct and complete the unit test for all methods. thank you

import org.junit.*; import static org.junit.Assert.*;

@test public void Rate (){ HourlyRate hourlyrate = new HourlyRate (0); HourlyRate.rate(25); assertEquals(25, HourlyRate.getGrossPay(),1); } @test public void empName(){ Payroll payroll = new Payroll(); Payroll.empName(); assertEquals(, Payroll.getGrossPay(),);

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!