Question
Employee and ProductionWorkder Classes Design a class named Employee. The class should keep the following information in fields: Employee name Employee number Hire date Write
Employee and ProductionWorkder Classes
Design a class named Employee. The class should keep the following information in fields:
Employee name
Employee number
Hire date
Write one or more constructors and the appropriate accessor and mutator methods for the class.
Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following formation:
Shift (an integer)
Hourly pay rate (a double)
The workday is divided into two shifts: day and night. The shift field will be an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write one or more constructors and the appropriate accessor and mutator methods for the class.
2. 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.
For the employee number, I want the exception to be for a 6 digit number
For the InvaildShift, I want it to throw the exception if the user inputs an invalid number ( not 1 or 2)
For the InvalidPayRate it should be used when the user puts a negative number
I have the following:
Employee.java
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for file classes
public class Employee{
private String employeeName;
private String employeeNumber;
private String hireDate;
// default constructor implementation
public Employee()
{
employeeName = "";
employeeNumber = "";
hireDate = "";
}
// parameterized constructor implementation
public Employee(String empName, String empNumber, String date) throws InvalidEmployeeNumber
{
setEmployeeName(empName);
setEmployeeNumber(empNumber);
setHireDate(date);
}
// accessor methods
// getEmployeeName method implementation
public String getEmployeeName()
{
return employeeName;
}
// getEmployeeNumber method implementation
public String getEmployeeNumber()
{
return employeeNumber;
}
// getHireDate method implementation
public String getHireDate()
{
return hireDate;
}
// mutator methods
// setEmployeeName method implementation
public void setEmployeeName(String empName)
{
employeeName = empName;
}
// setEmployeeNumber method implementation
public void setEmployeeNumber(String empNumber) throws InvalidEmployeeNumber
{
if(empNumber.length() == 6 && Character.isDigit(empNumber.charAt(0))
&& Character.isDigit(empNumber.charAt(1))
&& Character.isDigit(empNumber.charAt(2))
&& Character.isDigit(empNumber.charAt(3))
&& Character.isDigit(empNumber.charAt(4))
&& Character.isDigit(empNumber.charAt(5))
)
{
employeeNumber = empNumber;
}
else
{
employeeNumber = "Invalid number must be 6 digits no more, no lesss";
throw new InvalidEmployeeNumber();
}
}
// setHireDate method implementation
public void setHireDate(String date)
{
hireDate = date;
}
// toString method implementation
public String toString()
{
String result = "";
result = "Employee name: " + employeeName + " "
+ "Employee number: " + employeeNumber + " "
+ "Employee hire date: " + hireDate;
return result;
}
}
ProductionWorker.java
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for file classes
public class ProductionWorker extends Employee
{
//set int values to types of shifts
public final int NoShift = 0;
public final int DShift = 1;
public final int NShift = 2;
//data fields
private int shift;
private double hourlyPayRate;
// default constructor implementation
public ProductionWorker()
{
super();
shift = NoShift;
hourlyPayRate = 0.0;
}
// parameterized constructor implementation
public ProductionWorker(String empName, String empNumber, String date, int sh, double rate)
{
super(empName, empNumber, date);
setShift(sh);
setHourlyPayRate(rate);
}
// accessor methods
// getShift method implementation
public int getShift()
{
return shift;
}
// getHourlyPayRate method implementation
public double getHourlyPayRate()
{
return hourlyPayRate;
}
// mutator methods
// setShift method implementation
public void setShift(int sh) throws InvaildShift
{
if(sh == DShift || sh == NShift)
shift = sh;
else
{
throw new InvalidShift();
}
}
// setHourlyPayRate method implementation
public void setHourlyPayRate(double rate)
{
hourlyPayRate = rate;
}
// toString method implementation
public String toString()
{
String result = "";
result = super.toString() + " ";
if(shift == DShift)
result += "Shift: Day ";
else if(shift == NShift)
result += "Shift: Night ";
else
{
result += "Shift: Invalid ";
}
result += "Hourly pay rate: " + String.format("$%.2f", hourlyPayRate);
return result;
}
}
InvalidEmployeeNumber.java
public class InvalidEmployeeNumber extends Exception { public InvalidEmployeeNumber() { //error message super("Error: Invalid employee number found"); } }
InvalidShift.java
public class InvalidShift extends Exception
{
public InvalidShift()
{
//error message
super("Error: Invalid shift found");
}
}
InvalidPayRate.java
public class InvalidPayRate extends Exception { public InvalidPayRate() { //error message super("Error: Invalid pay rate found"); } }
ProductionWorkerDemo.java
import java.util.Scanner;
public class ProductionWorkerDemo extends Exception
{
// start main method
public static void main(String[] args)
{
// create an object for Scanner class
Scanner input = new Scanner(System.in);
char choice;
// repeat the loop until the user wants to exit
do
{
// prompt the user to enter the employee name
System.out.print("Enter the employee name: ");
String empName = input.nextLine();
// prompt the user to enter the employee number
System.out.print("Enter the 6 digit: ");
String empNumber = input.nextLine();
// prompt the user to enter the employee hire date
System.out.print("Enter the employee hire date: ");
String date = input.nextLine();
// prompt the user to enter the employee shift
System.out.print("Enter the employee shift (1 for day shift and 2 for night shift): ");
int sh = Integer.parseInt(input.nextLine());
// prompt the user to enter the employee hourly pay rate
System.out.print("Enter the hourly pay rate: $");
double rate = Double.parseDouble(input.nextLine());
if (rate < 0)
throw new InvalidPayRate("Invalid number must be positive");
// create an object for ProductionWorker class
try {
ProductionWorker worker = new ProductionWorker(empName, empNumber, date, sh, rate);
// display the details of the employee
System.out.println(" Details of the employee: ");
System.out.println(worker);
}
catch(InvalidPayRate invalidPayRate)
{
InvalidPayRate.getMessage();
InvalidPayRate.printStackTrace();
}
// prompt the user to enter the repetition choice
System.out.print(" Do you want to demonstrate another ProductionWorker object? (Y/N): ");
choice = input.nextLine().charAt(0);
System.out.println();
}while(choice == 'Y' || choice == 'y');
}
}
What I'm having trouble on is displaying multiple error messages if they incorrectly say the user makes multiple errors such as giving the wrong ID, wrong shift, and a negative number for pay rate? As of right now, if the user makes multiple errors, it only displays the first error. Any help will be appreciated!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started