Create one single java program that does the following: You will be creating a P
ID: 653723 • Letter: C
Question
Create one single java program that does the following:
You will be creating a Payroll which will use Inheritance, Exceptions, and Aggregation. A GUI is optional.
Create a project folder which will be used to test your final project. It could be called something like PayrollManager. Create the following classes and interfaces:
Employee Class implements Serializable
private String name; // Employee name
private String employeeNumber; // Employee number
private String hireDate; // Employee hire date
Default constructor puts null or 0 as appropriate
Initializer constructor initializes the private data and throws an InvalidEmployeeNumber Exception
write sets for all the private data and for the setEmpNumber throw an InvalidEmployeeNumber Exception if the String does not parse to an int or the number is out of the 0 to 9999 range
Write the toString method to return the data.
Write a main method within this class to test the Employee with valid and invalid data using try catch.
ProductionWorker Class extends Employee implements Serializable
// Constants for the day and night shifts.
public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2;
private int shift; // The employee's shift
private double payRate; // The employee's pay rate
Default constructor calls super and puts null or 0 as appropriate
Initialzer constructor throws InvalidEmployeeNumber, InvalidShift, InvalidPayRate
The shift must be either 1 or 2 or throw an InvalidShift Exception
The payRate must be a positive number or throw an InvalidPayRate Exception
Write the toString method to return the data.
Write a main method within this class to test the ProductionWorker with valid and invalid data using try catch.
ShiftSupervisor Class extends Employee implements Serializable
private double salary; // Annual salary
private double bonus; // Annual bonus
Default constructor puts null or 0 as appropriate
Initializer constructor initializes the private data and throws an InvalidEmployeeNumber Exception
Write the sets for this class which do not throw any additional Exceptions
Write the toString method to return the data.
Write a main method within this class to test the ShiftSupervisor with valid and invalid data using try catch.
TeamLeader Class extends ProductionWorker implements Serializable
private double monthlyBonus; // Monthly bonus
private double requiredTrainingHours; // Required training hours
private double trainingHoursAttended; // Training hours attended
Default constructor puts null or 0 as appropriate
Initializer constructor initializes the private data and throws an InvalidEmployeeNumber Exception , InvalidShift Exception and InvalidPayRate Exception
You do not have to throw any new exceptions as they are thrown from the ProductionWorker super class.
Write the toString method to return the data.
Write a main method within this class to test the TeamLeader with valid and invalid data using try catch.
Payroll Class implements Serializable
private ArrayList <Employee> payroll;
The default constructor creates and empty ArrayList cabable of pointing to Employee objects
Create an addToPayroll method given an Employee type object
Create a removeFromPayroll method given an Employee String id. Overload this with given an Employee object. Return a string message to indicate success or failure.
Create a findInPayroll method given an Employee String id and returns an int value of the index or -1 if not there
Create a getEmployee method which given the String empId returns a copy of the employee or a null value if not found.
Write the toString method checking to make sure that payroll is not null before testing the size.
Write the writeToFile(String filename)throws FileNotFoundException,IOException
(because payroll is serializable you can use one statement to write out the whole payroll) return a String message.
Write the readFromFile(String filename)throws FileNotFoundException,
IOException,ClassNotFoundException
(because payroll is Serializable you can use one statement to read into the whole payroll but must cast the returned payroll as (ArrayList<Employee>) return a String message.
Write a main method within this class to test the Payroll class by adding, removing, storing and retrieving Employees.
Test your PayrollManager class as an interactive interface allowing the User to input data. This class handles the output which the other classes return information.
Explanation / Answer
Given below are the needed classes for the question. Please rate the answer if it helped. Thank you.
public class InvalidEmployeeNumber extends Exception {
public InvalidEmployeeNumber(String msg)
{
super(msg);
}
}
============
public class InvalidShift extends Exception {
public InvalidShift(String msg)
{
super(msg);
}
}
=============
public class InvalidPay extends Exception {
public InvalidPay(String msg)
{
super(msg);
}
}
===========
import java.io.Serializable;
public class Employee implements Serializable{
private String name;
private String employeeNumber;
private String hireDate;
public Employee()
{
name = null;
employeeNumber = null;
hireDate = null;
}
public Employee(String name, String empno, String hiredt) throws InvalidEmployeeNumber
{
this.name = name;
this.hireDate = hiredt;
setEmployeeNumber(empno);
}
public void setEmployeeNumber(String empno) throws InvalidEmployeeNumber
{
try
{
int n = Integer.parseInt(empno);
if(n < 0 || n > 9999)
throw new InvalidEmployeeNumber("Invalid employee number - " + empno + ". Out of range 0-9999.");
employeeNumber = "" + n;
}
catch(NumberFormatException e)
{
throw new InvalidEmployeeNumber("Invalid employee number - " + empno);
}
}
public void setName(String n)
{
name = n;
}
public void setHireDate(String dt)
{
hireDate = dt;
}
public String getName() {
return name;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public String getHireDate() {
return hireDate;
}
public String toString()
{
return "Name: " + name + " Emp. No.: " + employeeNumber + " Hire Date: " + hireDate;
}
public static void main(String[] args) {
try {
Employee e1 = new Employee("John", "111", "05/03/2016");
System.out.println(e1);
Employee e2 = new Employee("Henry", "10001", "02/02/2016");
System.out.println(e2);
} catch (InvalidEmployeeNumber e) {
System.out.println(e.getMessage());
}
}
}
===============
import java.io.Serializable;
public class ProductionWorker extends Employee implements Serializable {
// Constants for the day and night shifts.
public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2;
private int shift; // The employee's shift
private double payRate; // The employee's pay rate
public ProductionWorker()
{
super();
shift = 0;
payRate = 0;
}
public ProductionWorker(String name, String empno, String hiredt, int shift, double rate) throws InvalidEmployeeNumber, InvalidShift, InvalidPay
{
super(name, empno, hiredt);
setShift(shift);
setPayRate(rate);
}
public int getShift() {
return shift;
}
public void setShift(int shift) throws InvalidShift{
if(shift != DAY_SHIFT && shift != NIGHT_SHIFT)
throw new InvalidShift("Invalid shift " + shift);
this.shift = shift;
}
public double getPayRate() {
return payRate;
}
public void setPayRate(double payRate) throws InvalidPay {
if(payRate <= 0)
throw new InvalidPay("Invalid pay rate " + payRate + ". Should be positive number.");
this.payRate = payRate;
}
public String toString()
{
String str = super.toString();
str += " Shift: " + shift + " Pay rate: " + payRate;
return str;
}
public static void main(String[] args) {
ProductionWorker pw1 = null;
try {
pw1 = new ProductionWorker("John", "1111", "03/05/2016",DAY_SHIFT,100);
System.out.println(pw1);
pw1.setShift(3);
} catch (InvalidEmployeeNumber | InvalidShift | InvalidPay e) {
System.out.println(e.getMessage());
}
try {
pw1.setPayRate(0);
} catch (InvalidPay e) {
System.out.println(e.getMessage());
}
}
}
============
import java.io.Serializable;
public class ShiftSupervisor extends Employee implements Serializable {
private double salary; // Annual salary
private double bonus; // Annual bonus
public ShiftSupervisor()
{
super();
salary = 0;
bonus = 0;
}
public ShiftSupervisor(String name, String empno, String hiredt, double sal, double bon) throws InvalidEmployeeNumber
{
super(name, empno, hiredt);
salary = sal;
bonus = bon;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public String toString()
{
String str = super.toString();
str += " Salary : " + salary + " Bonus : " + bonus;
return str;
}
public static void main(String[] args) {
try {
ShiftSupervisor e1 = new ShiftSupervisor("John", "111", "05/03/2016", 2000, 500);
System.out.println(e1);
ShiftSupervisor e2 = new ShiftSupervisor("Henry", "10001", "02/02/2016", 1000, 200);
System.out.println(e2);
} catch (InvalidEmployeeNumber e) {
System.out.println(e.getMessage());
}
}
}
===========
import java.io.Serializable;
public class TeamLeader extends ProductionWorker implements Serializable {
private double monthlyBonus; // Monthly bonus
private double requiredTrainingHours; // Required training hours
private double trainingHoursAttended; // Training hours attended
public TeamLeader()
{
super();
requiredTrainingHours = 0;
monthlyBonus = 0;
trainingHoursAttended = 0;
}
public TeamLeader(String name, String empno, String hiredt, int shift, double rate, double bon, double reqhrs, double attdhrs) throws InvalidEmployeeNumber, InvalidShift, InvalidPay
{
super(name, empno, hiredt, shift, rate);
requiredTrainingHours = reqhrs;
monthlyBonus = bon;
trainingHoursAttended = attdhrs;
}
public double getMonthlyBonus() {
return monthlyBonus;
}
public void setMonthlyBonus(double monthlyBonus) {
this.monthlyBonus = monthlyBonus;
}
public double getRequiredTrainingHours() {
return requiredTrainingHours;
}
public void setRequiredTrainingHours(double requiredTrainingHours) {
this.requiredTrainingHours = requiredTrainingHours;
}
public double getTrainingHoursAttended() {
return trainingHoursAttended;
}
public void setTrainingHoursAttended(double trainingHoursAttended) {
this.trainingHoursAttended = trainingHoursAttended;
}
public String toString()
{
String str = super.toString();
str += " Monthly bonus : " + monthlyBonus + " Required Training hours : " + requiredTrainingHours
+ " Attended Hours: " + trainingHoursAttended;
return str;
}
public static void main(String[] args) {
ProductionWorker pw = null;
try {
pw = new ProductionWorker("John", "1111", "03/05/2016",DAY_SHIFT,100);
System.out.println(pw);
pw.setShift(3);
} catch (InvalidEmployeeNumber | InvalidShift | InvalidPay e) {
System.out.println(e.getMessage());
}
try {
pw = new ProductionWorker("Peter", "222", "10/05/2016",NIGHT_SHIFT,-10);
} catch (InvalidPay | InvalidEmployeeNumber | InvalidShift e) {
System.out.println(e.getMessage());
}
}
}
=============
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class Payroll implements Serializable {
private ArrayList<Employee> payroll;
public Payroll()
{
payroll = new ArrayList<Employee>();
}
public void addToPayroll(Employee e)
{
payroll.add(e);
}
public String removeFromPayroll(String id)
{
for(Employee e : payroll)
{
if(e.getEmployeeNumber().equals(id))
{
payroll.remove(e);
return "Successfully removed employee with Id " + id;
}
}
return "Could not remove employee with Id " + id;
}
public Employee getEmployee(String id)
{
for(Employee e: payroll)
if(e.getEmployeeNumber().equals(id))
return e;
return null;
}
public int findInPayroll(String id)
{
for(int i = 0; i < payroll.size(); i++)
{
if(payroll.get(i).getEmployeeNumber().equals(id))
return i;
}
return -1;
}
public String toString()
{
String str = "";
for(Employee e : payroll)
str += e.toString() + " ";
return str;
}
public void writeToFile(String filename)throws FileNotFoundException,IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(payroll);
oos.close();
}
public void readFromFile(String filename)throws FileNotFoundException,
IOException,ClassNotFoundException
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
payroll = (ArrayList<Employee>) ois.readObject();
ois.close();
}
public static void main(String[] args) {
String filename = "payroll.ser";
Payroll p = new Payroll();
try {
p.readFromFile(filename);
System.out.println("loaded file contents " + p.toString());
} catch (ClassNotFoundException | IOException e) {
System.out.println("No data loaded");
}
Payroll p2 = new Payroll();
try {
p2.addToPayroll(new Employee("John", "111", "02/03/2016"));
p2.addToPayroll(new ProductionWorker("Peter", "222", "03/04/2016", ProductionWorker.NIGHT_SHIFT, 100));
p2.addToPayroll(new ShiftSupervisor("Michael", "333", "06/07/2016", 2000, 300));
p2.addToPayroll(new TeamLeader("Joseph", "444","09/10/2016",ProductionWorker.DAY_SHIFT,5000,500,15,8));
System.out.println(p2);
System.out.println("Finding 222 " + p2.findInPayroll("222"));
System.out.println("Finding 888 " + p2.findInPayroll("888"));
System.out.println("get 222 " + p2.getEmployee("222"));
System.out.println("get 888 " + p2.getEmployee("888"));
System.out.println("remove 333 " + p2.removeFromPayroll("333"));
System.out.println("remove 999 " + p2.removeFromPayroll("999"));
} catch (InvalidEmployeeNumber | InvalidShift | InvalidPay e1) {
System.out.println(e1.getMessage());
}
try {
p.writeToFile(filename);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.