Question: Java: Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: The Employee class should throw an exception named InvalidEmployeeNumber
Java: Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur:
The Employee class should throw an exception named InvalidEmployeeNumber when it receives an invalid employee number.
The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift.
The ProductionWorker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. Write a test program that demonstrates how each of these exception conditions works.
!!! Each exception class must have a no argument and a single argument constructor.
!!! For each of the three exceptions of this problem, the program will re-prompt the user until correct data is entered. When the three data entries are correct, the program prints the data to the console and ends.
!!! The program also has to keep track of the total number of exceptions thrown until correct data is entered. Print this number at the end of the program.
There should be six files in the end: Employee, ProductionWorker, InvalidEmployeeNumber.java, InvalidShift.java, InvalidPayRate.java, test.java
Thank you very much
Please see my code below, as I am having trouble implementing this:
File 1: Employee.java
package cs280_Assignment1;
import java.util.regex.Pattern;
public class Employee {
private String name;
private String number;
private String date;
public Employee(String n, String num, String date) {
this.number = num;
this.name = n;
this.date = date;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
public String getnumber() {
return number;
}
public void setnumber(String e) {
if(isValidEmpNum(e)) {
this.number = e;
}
}
public String getdate() {
return date;
}
public void setdate(String date) {
this.date = date;
}
public boolean isValidEmpNum(String e) {
String pattern = "[0-9]{3}[-]{1}[A-M]{1}";
return Pattern.matches(pattern, e);
}
@Override
public String toString() {
return "Name:" + getName() + " " + "Employee Number: " +
getnumber() + " " + "Hire Date:" + getdate() + " ";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (number==null) {
if (other.number != null)
return false;
}
else if (!number.equals(other.number))
return false;
if (date == null) {
if (other.date != null)
return false;
}
else if (!date.equals(other.date))
return false;
if (name == null) {
if (other.name !=null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
}
File 2: ProductionWorker.java
package cs280_Assignment1;
public class ProductionWorker extends Employee {
private int shift;
private double payRate;
public ProductionWorker(String n, String num, String date, int sh, double rate) {
super(n,num,date);
this.shift = sh;
this.payRate = rate;
}
public ProductionWorker() {
}
public int getShift() {
return shift;
}
public void setShift(int shift) {
this.shift=shift;
}
public double getPayRate() {
return payRate;
}
public void setPayRate(double payRate) {
this.payRate = payRate;
}
@Override
public String toString() {
String shiftString = "Night";
if (shift==1) {
shiftString = "Day";
}
return super.toString() + "Shift:" + shiftString + " " + "Hourly Pay Rate: $" + getPayRate();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
if (getClass() != obj.getClass())
return false;
ProductionWorker other = (ProductionWorker) obj;
if (Double.doubleToLongBits(payRate)!= Double.doubleToLongBits(other.payRate))
return false;
if(shift != other.shift)
return false;
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
