I need help with making these following adjustments to my program: Add a user-defined exception that can
Question:
I need help with making these following adjustments to my program:
Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should then make an instance of the class and call the method in such a way that the exception is thrown (e.g. invalid input or state of the system).
Please see the following code: PLEASE PROVIDE THE ANSWER SEPARATE IN JAVA FILE.
TESTER:
public class Tester{
public static void main(String[] args) { System.out.print("Nekia Hunter Discussion 5 CMIS 242-7380 04/17/2022 ");
// Create. an array of object - Employee employees[] = new Employee[3];
employees[0] = new HourlyEmployee(1, "Donna", "Jones", 45, 19.89); employees[1] = new SalaryEmployee(2, "Fay", "Hunt", 30000); employees[2] = new CommissionEmployee(3, "Marie", "Sessum", 0.05, 6000, 150000);
// now we call the tostring to display details for (int i = 0; i < employees.length; i++) { System.out.println(employees[i]); System.out.println(" "); } } }
EMPLOYEE
public abstract class Employee {
// Fields private int employeeId; private String firstName; private String lastName;
// Default constructor public Employee() {
}
/** * Parameter Constructor * * @param employeeId * @param fristName * @param lastName */ public Employee(int employeeId, String firstName, String lastName) { this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName;
}
// Getter methods public int getEmployeeId() { return employeeId;
}
public String getfirstName() { return firstName;
}
public String getlastName() { return lastName;
}
// Setter methods public void setEmployeeId(int id) { this.employeeId = id;
}
public void setFirstName(String name) { this.firstName = name;
}
public void setLastName(String name) { this.lastName = name;
}
// Abstract method /** * Function to calculate salary of an employee - All the child class must * override this function * * @return salary */ public abstract double calculateSalary();
@Override public String toString() { return String.format("%-20s %d%n", "Employee ID:", employeeId) + String.format("%-20s %s%n", "Employee Name: ", firstName + " " + lastName); //String.format("%-20s %s%n", "args"); } }
HOURLY EMPLOYEE
public class HourlyEmployee extends Employee {
private double hoursWorked; private double hourlyRate;
public HourlyEmployee() { super();
}
/** * Parameter constructor to instantiate HourlyEmployee Object * * @param employeeId * @param firstName * @param lastName * @param hoursWorked * @param hourlyRate */ public HourlyEmployee(int employeeId, String firstName, String lastName, double hoursWorked, double hourlyRate) { super(employeeId, firstName, lastName); this.hoursWorked = hoursWorked; this.hourlyRate = hourlyRate;
}
// Getter methods for local variables of this class public double getHoursWorked() { return hoursWorked; }
public double getHourlyRate() { return hourlyRate; }
/** * Function to calculate salary of an employee - All the child class must * override this function * * @return salary */ @Override public double calculateSalary()
{ double salary = 0; double overtime = 0;
if (hoursWorked > 40) { double hours = hoursWorked - 40; hoursWorked = 40; overtime = hours * (hourlyRate * 1.5); }
salary = hoursWorked * hourlyRate; salary += overtime;
return salary; }
@Override public String toString() { return super.toString() + String.format("%-20s %.2f%n", "Salary:", calculateSalary()); }
}
SALARY EMPLOYEE
public class SalaryEmployee extends Employee { private double baseSalary;
public SalaryEmployee() { super();
}
/** * Initialize Salary Employee from give parameters. * @param employeeId * @param firstName * @param lastName * @param salary */ public SalaryEmployee(int employeeId, String firstName, String lastName, double salary) { super(employeeId, firstName, lastName); baseSalary = salary; }
public void setBaseSalary(double salary) { this.baseSalary = salary; }
/** * Function to calculate salary of an employee - All the child class must * override this function * * @return salary */ @Override public double calculateSalary()
{ return baseSalary; }
@Override public String toString() { return super.toString() + String.format("%-20s %.2f%n", "Salary:", calculateSalary());
}
}
COMMISSION EMPLOYEE
public class CommissionEmployee extends Employee {
private double commissionRate; private double baseSalary; private double totalSales;
public CommissionEmployee() { super(); }
public CommissionEmployee(int employeeId, String firstName, String lastName, double rate, double baseSalary, double totalSales) { super(employeeId, firstName, lastName); this.commissionRate = rate; this.baseSalary = baseSalary; this.totalSales = totalSales; }
// Getter methods public double getCommissionRate() { return commissionRate;
}
public double getBaseSalary() { return this.baseSalary; }
public double getSales() { return totalSales; }
// Setter methods public void setCommissionRate(double rate) { this.commissionRate = rate; }
public void setBaseSalary(double Salary) { this.baseSalary = Salary; }
public void setSales(double sales) { this.totalSales = sales; }
public double calculateCommission() { return totalSales * getCommissionRate(); }
/** * Function to calculate salary of an employee - All the child class must * override this function * * @return salary */ public double calculateSalary() { return calculateCommission() + baseSalary; }
@Override public String toString() { return super.toString() + String.format("%-20s %.2f%n", "Salary:", calculateSalary()); } }