Question
Should be in Java Interface Worker should extend Serializable from java.io.* In addition to shown data fields, each Worker object should have a name field,
Should be in Java
Interface Worker should extend Serializable from java.io.*
In addition to shown data fields, each Worker object should have a name field, a workerID field, and a Department field.The classes and/or interfaces where these fields belong are not indicated, so you need to decide where they best belong in order to reduce code redundancy as much as possible; it may not be possible to put them in only one place.
Write get and set methods for every data field. You may use the automatic generator of your IDE, but all data should be validated. I should not be able to set invalid data such a negative hours. The program should not terminate when receiving bad data but should recover, whether input is coming from a file or interactively.
All appropriate classes must override the equals method so that it compares workers by workerID; two workers should be considered equal if they have the same workerID, even if they are not the same kind of Worker; when comparing a WorkerObject to an object of another type, the equals method should return false.
Class HourlyWorker should be an abstract class because it contains an abstract toString method that will be implemented in the subclasses; in the subclasses, it should return the type of subclass along with all data field values in a user-friendly understandable format.
Data field hours in the HourlyWorker class represents the number of hours worked so far during the current month
Decide if any of the methods requires a precondition; if so, they should throw an appropriate exception when the precondition is not met
Supply appropriate constructors for all concrete and abstract classes. Each should have a no-arg constructor and a fully qualified constructor.
Create an application class named WorkerTest to demonstrate the hierarchy.
The application should create Volunteer, HourlyEmployee, and SalariedEmployee objects with initial field values specified interactively. Invalid data should produce a message and a re-prompt.
The application should then create a series of objects by reading initial field values from a file. The file format is up to you. The input should test both the no-arg constructors and the fully qualified constructors. If the file contains invalid data, the program send a message to err.out, ignore the invalid data and proceed to the next valid data. The file input data should test all caught exceptions. I should not be able to crash your program, regardless of the input data I provide.
Check each object against abstract class HourlyWorker and each interface by using the instanceof operator. Output results indicating whether each object is or is not of the checked type.
Create an ArrayList containing the objects and polymorphically traverse the array, writing the output of each objects' toString() method to a file.
I know I need to do a department class and also an employee class.
Here is the code i have so far.
public abstract class Worker { private final String lastName; private final String workerID; private final String Department; //contrructor public Worker(String lastName, String workerID, String Department) { this.lastName = lastName; this.workerID = workerID; this.Department = Department; } public String getLastName() { return lastName; } public String getWorkerID() { return workerID; } public String getDepartment() { return Department; } //return String of Worker object @Override public String toString() { return String.format("%s %s%nID worker ID: %s", getLastName(), getWorkerID(), getDepartment()); } public abstract double earnings(); } // worker class
public class HourlyEmpoyee extends Worker { private double wage; private double hours; public HourlyEmpoyee(String lastName, String workerID, String Department, double wage, double hours) { super(lastName, workerID, Department); if (wage < 0.0) throw new IllegalArgumentException("Hours worked must be >= 0.0"); if ((hours <0.0) || (hours > 168.0)) throw new IllegalArgumentException("Hours worked must be >= 0.0 and <=168.0"); this.wage = wage; this.hours = hours; } public void setWage(double wage) { if(wage <0.0) throw new IllegalArgumentException("Hourly wage must be >= 0.0"); this.wage = wage; } public double getWage() { return wage; } public void setHours(double hours) { if((hours <0.0) || (hours > 168.0)) throw new IllegalArgumentException("Hours worked must be >= 0.0 and <= 168.0"); this.hours = hours; } public double getHours() { return hours; } @Override public double earnings() { if(getHours() <=40) return getWage() * getHours(); else return 40 * getWage() + (getHours() - 40) * getWage() * 1.5; } @Override public String toString() { return String.format("hourly employee: %s%n%s: $%,.2f; %s: %,.2f", super.toString(), "hourly wage", getWage(), "hours worked", getHours()); } }
public class SalariedEmployee extends Worker { private double weeklySalary; public SalariedEmployee(String lastName, String workerID, String Department, double weeklySalary) { super(workerID, lastName, Department); if (weeklySalary <0.0) throw new IllegalArgumentException("Weekly salary must be >= 0.0"); this.weeklySalary = weeklySalary; } public void setWeeklySalary(double weeklySalary) { if(weeklySalary <0.0) throw new IllegalArgumentException("Weekly salary must be >= 0.0"); this.weeklySalary = weeklySalary; } public double getWeeklySalary() { return weeklySalary; } @Override public double earnings() { return getWeeklySalary(); } @Override public String toString() { return String.format("salaried employee: %s%n%s: $%, .2f", super.toString(),"weekly salary", getWeeklySalary()); } }public abstract class Volunteer extends Worker// extends hourly worker due to hours worked { public Volunteer(String lastName, String workerID, String Department) { super(lastName, workerID, Department); } public double pay() { return 0; } }public class WorkerTest { public static void main(String[] args)// add a input scanner and a file reader { SalariedEmployee salariedEmployee = new SalariedEmployee("Burger", "ID001", "Sales" 777.00); HourlyEmployee hourlyEmployee = new HourlyEmployee ("Smith", "ID002", "Claims" 14.44, 40); Volunteer volunteer = new Volunteer("Johnson", "ID003", "Help" ); System.out.println("Workers processed individually:"); System.out.printf("%n%s%n%s: $%,.2f%n%n", salariedEmployee, "earned" salariedEmployee.earning()); System.out.printf("%s%n%s: $%,.2f%n%n", hourlyEmployee, "earned" hourlyEmployee.earnings()); System.out.println("Volunteer earnings are: 0 "); Worker[] employees = new Worker[3]; employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = volunteer; System.out.printf("Employees processed polymorphically: %n%n"); for(Worker currentEmployee : employees) { System.out.println(currentEmployee); System.out.printf("earned $%,.2f%n%n", currentEmployee.earnings()); } for (int j = 0; j
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