Modify the supplied payroll system to include a private instance variable called
ID: 3885164 • Letter: M
Question
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable.
Use a static variable in the Employee class to help automatically assign each new employee a unique (incremental) id number.
Assume the payroll is processed once per month. Create an array of Employee variables to store references to the various employee objects. In a loop, calculate the payroll for each Employee, and add a €200.00 bonus to the person’s payroll if they joined the company more than five years before the current date.
Change the Earnings() method in Employee and all sub-classes to throw a user defined Exception if the total earnings are less than zero. The exception should have a message with the employee's name, miscalculated wage, and a short description of the problem.
Modify the Test class to be able to handle exceptions. When an exception is encountered calculating an employee's earnings, the Test class should print out the error message and continue as normal with the next employees. Test this by changing the Test class so that two of the employees will have negative earnings.
Comment in each java source file, explaining your code with comments.
Test.java
// Driver for Employee hierarchy
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
public class Test {
// test Employee hierarchy
public static void main(String args[]) {
Employee employee; // superclass reference
String output = "";
Boss boss = new Boss("John", "Smith", 800.0);
CommissionWorker commissionWorker =
new CommissionWorker(
"Sue", "Jones", 400.0, 3.0, 150);
PieceWorker pieceWorker =
new PieceWorker("Bob", "Lewis", 2.5, 200);
HourlyWorker hourlyWorker =
new HourlyWorker("Karen", "Price", 13.75, 40);
DecimalFormat precision2 = new DecimalFormat("0.00");
// Employee reference to a Boss
employee = boss;
output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + " "
+ boss.toString() + " earned $"
+ precision2.format(boss.earnings()) + " ";
// Employee reference to a CommissionWorker
employee = commissionWorker;
output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + " "
+ commissionWorker.toString() + " earned $"
+ precision2.format(
commissionWorker.earnings()) + " ";
// Employee reference to a PieceWorker
employee = pieceWorker;
output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + " "
+ pieceWorker.toString() + " earned $"
+ precision2.format(pieceWorker.earnings()) + " ";
// Employee reference to an HourlyWorker
employee = hourlyWorker;
output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + " "
+ hourlyWorker.toString() + " earned $"
+ precision2.format(hourlyWorker.earnings()) + " ";
JOptionPane.showMessageDialog(null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
} // end class Test
Employee.java
// Abstract base class Employee.
public abstract class Employee {
private String firstName;
private String lastName;
// constructor
public Employee(String first, String last) {
firstName = first;
lastName = last;
}
// get first name
public String getFirstName() {
return firstName;
}
// get last name
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + ' ' + lastName;
}
public abstract double earnings();
}
Boss.java
// Abstract base class Employee.
public abstract class Employee {
private String firstName;
private String lastName;
// constructor
public Employee(String first, String last) {
firstName = first;
lastName = last;
}
// get first name
public String getFirstName() {
return firstName;
}
// get last name
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + ' ' + lastName;
}
public abstract double earnings();
}
HourlyWorker.java
// Definition of class HourlyWorker
public final class HourlyWorker extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week
// constructor for class HourlyWorker
public HourlyWorker(String first, String last,
double wagePerHour, double hoursWorked) {
super(first, last); // call superclass constructor
setWage(wagePerHour);
setHours(hoursWorked);
}
// Set the wage
public void setWage(double wagePerHour) {
wage = (wagePerHour > 0 ? wagePerHour : 0);
}
// Set the hours worked
public void setHours(double hoursWorked) {
hours = (hoursWorked >= 0 && hoursWorked < 168
? hoursWorked : 0);
}
// Get the HourlyWorker's pay
public double earnings() {
return wage * hours;
}
public String toString() {
return "Hourly worker: " + super.toString();
}
}
CommissionWorker.java
// Definition of class HourlyWorker
public final class HourlyWorker extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week
// constructor for class HourlyWorker
public HourlyWorker(String first, String last,
double wagePerHour, double hoursWorked) {
super(first, last); // call superclass constructor
setWage(wagePerHour);
setHours(hoursWorked);
}
// Set the wage
public void setWage(double wagePerHour) {
wage = (wagePerHour > 0 ? wagePerHour : 0);
}
// Set the hours worked
public void setHours(double hoursWorked) {
hours = (hoursWorked >= 0 && hoursWorked < 168
? hoursWorked : 0);
}
// Get the HourlyWorker's pay
public double earnings() {
return wage * hours;
}
public String toString() {
return "Hourly worker: " + super.toString();
}
}
PieceWorker.java
// PieceWorker class derived from Employee
public final class PieceWorker extends Employee {
private double wagePerPiece; // wage per piece output
private int quantity; // output for week
// constructor for class PieceWorker
public PieceWorker(String first, String last,
double wage, int numberOfItems) {
super(first, last); // call superclass constructor
setWage(wage);
setQuantity(numberOfItems);
}
// set PieceWorker's wage
public void setWage(double wage) {
wagePerPiece = (wage > 0 ? wage : 0);
}
// set number of items output
public void setQuantity(int numberOfItems) {
quantity = (numberOfItems > 0 ? numberOfItems : 0);
}
// determine PieceWorker's earnings
public double earnings() {
return quantity * wagePerPiece;
}
public String toString() {
return "Piece worker: " + super.toString();
}
}
Explanation / Answer
Test.java
//Driver for Employee hierarchy
//Java core packages
import org.joda.time.LocalDate;
import java.text.DecimalFormat;
//Java extension packages
import javax.swing.JOptionPane;
public class Test {
// test Employee hierarchy
public static void main(String args[]) throws Exception{
Employee employee; // superclass reference
Employee employeeArray[] = new Employee[4]; // new array of employee created
String output = "";
Boss boss = new Boss("John", "Smith", -800.0); // negative value for testing
employeeArray[0] = boss;
CommissionWorker commissionWorker =
new CommissionWorker(
"Sue", "Jones", 400.0, 3.0, -150); //negative value for testing
employeeArray[1] = commissionWorker;
PieceWorker pieceWorker =
new PieceWorker("Bob", "Lewis", 2.5, 200);
employeeArray[2] = pieceWorker;
HourlyWorker hourlyWorker =
new HourlyWorker("Karen", "Price", 13.75, 40);
employeeArray[3] = hourlyWorker;
DecimalFormat precision2 = new DecimalFormat("0.00");
int i=0;
for(i=0;i<4;i++)
{
LocalDate join = employeeArray[i].getJoinDate(); //retrive the joining date of the employee
LocalDate today = new LocalDate(); //gets the current date
LocalDate past = today.minusDays(365); //gets the five years previous date
if(join.isBefore(past)) //compares whether the join date is greater than five years or not
{
employeeArray[i].setPayroll(200); //extra payroll added
}
}
//Employee reference to a worker
for(i=0;i<4;i++)
{
employee = employeeArray[i];
try{
output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + " "
+ employeeArray[i].toString() + " earned $"
+ precision2.format(employeeArray[i].earnings()) + " ";
}
catch(Exception e)
{
System.out.println(e); //prints user defiened error message
System.out.println(" Name :"+employeeArray[i].toString());
}
}
JOptionPane.showMessageDialog(null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
} // end class Test
Employee.java
// Abstract base class Employee.
import org.joda.time.LocalDate; //joda-time added
public abstract class Employee extends Exception {
private String firstName;
private String lastName;
private LocalDate joinDate;
static int increment = 0; //static field added for unique employee id
private double payroll; //new variable added to calculate extra payroll for more than five year
private int employeeId;
// constructor
public Employee(String first, String last) {
firstName = first;
lastName = last;
joinDate = new LocalDate(); // this will add the current date as joining date of the employee
increment++;
employeeId = increment; //unique employee id stored
payroll = 0;
}
// get first name
public String getFirstName() {
return firstName;
}
// get last name
public String getLastName() {
return lastName;
}
public double getPayroll() {
return payroll;
}
public void setPayroll(double payroll) {
this.payroll = payroll;
}
public LocalDate getJoinDate() {
return joinDate;
}
public void setJoinDate(LocalDate localDate) {
this.joinDate = localDate;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String toString() {
return firstName + ' ' + lastName;
}
public abstract double earnings() throws Exception;
}
Boss.java
// Abstract base class Employee.
public class Boss extends Employee {
private double earning; // monthly earning
private double hours; // hours worked for week
// constructor
public Boss(String first, String last,double earn) {
super(first,last);
earning = earn;
}
public double earnings() throws Exception{
double d= earning+super.getPayroll();
if(d<0)
throw new MyException();
return d;
}
public String toString() {
return "Boss: " + super.toString();
}
}
CommissionWorker.java
// Definition of class HourlyWorker
public final class CommissionWorker extends Employee {
private double wage; // wage per hour
private double hours;// hours worked for week
private double commission;
// constructor for class CommissionWorker
public CommissionWorker(String first, String last,
double wagePerHour, double hoursWorked,double commissionP) {
super(first, last); // call superclass constructor
setWage(wagePerHour);
setHours(hoursWorked);
setCommission(commissionP);
}
// Set the wage
public void setWage(double wagePerHour) {
wage = (wagePerHour > 0 ? wagePerHour : 0);
}
// Set the hours worked
public void setHours(double hoursWorked) {
hours = (hoursWorked >= 0 && hoursWorked < 168
? hoursWorked : 0);
}
//set commission
public void setCommission(double commissionP) {
commission = (commissionP >= 0 && commissionP < 100
? commissionP : 0);
}
// Get the HourlyWorker's pay
public double earnings() throws Exception{
double d= ((wage * hours) *(1+(commission/100)))+super.getPayroll();
if(d<=0)
throw new MyException();
return d;
}
public String toString() {
return "Commission worker: " + super.toString();
}
}
PieceWorker.java
// PieceWorker class derived from Employee
public final class PieceWorker extends Employee {
private double wagePerPiece; // wage per piece output
private int quantity; // output for week
// constructor for class PieceWorker
public PieceWorker(String first, String last,
double wage, int numberOfItems) {
super(first, last); // call superclass constructor
setWage(wage);
setQuantity(numberOfItems);
}
// set PieceWorker's wage
public void setWage(double wage) {
wagePerPiece = (wage > 0 ? wage : 0);
}
// set number of items output
public void setQuantity(int numberOfItems) {
quantity = (numberOfItems > 0 ? numberOfItems : 0);
}
// determine PieceWorker's earnings
public double earnings() throws Exception{
double d= (quantity * wagePerPiece)+super.getPayroll();
if(d<=0)
throw new MyException();
return d;
}
public String toString() {
return "Piece worker: " + super.toString();
}
}
HourlyWorker.java
// Definition of class HourlyWorker
public final class HourlyWorker extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week
// constructor for class HourlyWorker
public HourlyWorker(String first, String last,
double wagePerHour, double hoursWorked) {
super(first, last); // call superclass constructor
setWage(wagePerHour);
setHours(hoursWorked);
}
// Set the wage
public void setWage(double wagePerHour) {
wage = (wagePerHour > 0 ? wagePerHour : 0);
}
// Set the hours worked
public void setHours(double hoursWorked) {
hours = (hoursWorked >= 0 && hoursWorked < 168
? hoursWorked : 0);
}
// Get the HourlyWorker's pay
public double earnings() throws Exception {
double d= ((wage * hours)+super.getPayroll());
if(d<=0)
throw new MyException();
return d;
}
public String toString() {
return "Hourly worker: " + super.toString();
}
}
MyException.java
//user defiened exception class
public class MyException extends Exception{
public String toString()
{
return "User defiened error message .Error occurs in calculating wage of the following employee";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.