Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

Writing a Strong Introduction

Answered: 1 week ago