In this lab we will create an employee hierarchy At the top

In this lab we will create an employee hierarchy. At the top will be the abstract class Employee, which
will hold an employee\'s name and the abstract method \"public abstract double
calculateMonthlyEarnings()\"
. Also include the appropriate constructors, get and set, and toString()
methods.

Class HourlyEmployee will inherit from Employee. It will hold a field for the hours worked and a
final for the hourly wage. It will calculate the monthly earnings for an hourly worker. Also include
the appropriate constructors, get and set, and toString() methods.

Class CommissionEmployee will inherit from Employee. It will hold a field for the commission
rate and monthly sales total. It will calculate the monthly earnings for a commission worker. Also
include the appropriate constructors, get and set, and toString() methods.

You will add a third type of employee to the existing hierarchy. SalariedEmployee will inherit from
Employee and hold fields for yearly salary and a field for the expected number of weeks per year they work. It will calculate the monthly earnings for a salaried worker. Also include the appropriate constructors, get and set, and toString() methods.

Create a Company class that supports an ArrayList of all the different types of Employees the company
employs. This class will employ methods that add and remove employees. It will also implement a
method to display all the employees listing their names and their monthly earnings. Proper format of all
output is expected.

Finally, provide a driver class that will create at least one of each employee and add them to the array
list. It will then display all the Employee data to the terminal window. Then it will remove one of the
Employees and redisplay the Employee list one final time, for testing purposes.


Note: pay formula: (salary / weeksPerYear) * (52/12)

Solution

import java.util.ArrayList;
import java.util.List;

// abstract class employee
abstract class Employee{
   private String name;
  
   // constructor to initialize the name
   Employee(String name){
       this.name=name;
   }
  
   public abstract double calculateMonthlyEarnings(); // abstract method
  
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
  
   // toString method will return the name of the employee
   @Override
   public String toString() {
       return this.name;
   }
}

class HourlyEmployee extends Employee{
  
   private int hoursWorked;
   private final double HOURLY_WAGE=200.00;

   // constructor to initialize the name,hoursWorked
   HourlyEmployee(String name,int hoursWorked) {
       super(name);
       this.hoursWorked=hoursWorked;
   }

   // method to calculate the salary
   @Override
   public double calculateMonthlyEarnings() {
       return (Double.valueOf(this.hoursWorked))*HOURLY_WAGE;
   }
  
   // method to return the name and salary of the employee
   @Override
   public String toString() {
       return this.getName()+\" : \"+calculateMonthlyEarnings()+\" $\";
   }

   public int getHoursWorked() {
       return hoursWorked;
   }

   public void setHoursWorked(int hoursWorked) {
       this.hoursWorked = hoursWorked;
   }

   public double getHOURLY_WAGE() {
       return HOURLY_WAGE;
   }
  
}


class CommissionEmployee extends Employee{
   private double totalMonthlySales;
   private double commisionRate;

   // constructor to initialize the name,totalMonthlySales,commisionRate
   CommissionEmployee(String name,double totalMonthlySales,double commisionRate) {
       super(name);
       this.totalMonthlySales=totalMonthlySales;
       this.commisionRate=commisionRate;
   }

   // method to calculate the salary
   @Override
   public double calculateMonthlyEarnings() {
       return (totalMonthlySales*commisionRate)/100;
   }
  
   // method to return the name and salary of the employee
   @Override
   public String toString() {
       return this.getName()+\" : \"+calculateMonthlyEarnings()+\" $\";
   }

   public double getTotalMonthlySales() {
       return totalMonthlySales;
   }

   public void setTotalMonthlySales(double totalMonthlySales) {
       this.totalMonthlySales = totalMonthlySales;
   }

   public double getCommisionRate() {
       return commisionRate;
   }

   public void setCommisionRate(double commisionRate) {
       this.commisionRate = commisionRate;
   }
}

class SalariedEmployee extends Employee{
   private double yearlySalary;
   private int noOfWeeksWorked;

   // constructor to initialize the name,yearlySalary,noOfWeeksWorked
   SalariedEmployee (String name,double yearlySalary,int noOfWeeksWorked) {
       super(name);
       this.yearlySalary=yearlySalary;
       this.noOfWeeksWorked=noOfWeeksWorked;
   }

   // method to calculate the salary
   @Override
   public double calculateMonthlyEarnings() {
       return (yearlySalary / noOfWeeksWorked) * (52/12);
   }
  
   // method to return the name and salary of the employee
   @Override
   public String toString() {
       return this.getName()+\" : \"+calculateMonthlyEarnings()+\" $\";
   }

   public double getYearlySalary() {
       return yearlySalary;
   }

   public void setYearlySalary(double yearlySalary) {
       this.yearlySalary = yearlySalary;
   }

   public int getNoOfWeeksWorked() {
       return noOfWeeksWorked;
   }

   public void setNoOfWeeksWorked(int noOfWeeksWorked) {
       this.noOfWeeksWorked = noOfWeeksWorked;
   }
}

class Company{
   // arraylist which stores all the employees
   private List<Employee>employeeList;
  
   // method to add the employee
   public void addEmployee(Employee emp){
       if(employeeList==null){ // if employee list is null initialize with empty arraylist
           employeeList= new ArrayList<>();
       }
       employeeList.add(emp);// adding the employee in the arraylist
   }
  
   //method to remove the employee from the arrayList
   public void removeEmployee(Employee emp){
       if(employeeList!=null && employeeList.size()>0){
       employeeList.remove(emp);// removing the employee
       System.out.println(emp.getName()+\" has been removed----------------------\");
       }else{
           System.out.println(\"List is empty. Please add employee to the list\");  
       }
   }
  
   //method to display all the employees
   public void displayAllEmployEarnings(){
       if(employeeList!=null && employeeList.size()>0){
           for (Employee employee : employeeList) {
               System.out.println(employee);
           }
       }else{
           System.out.println(\"List is empty. Please add employee to the list\");  
       }
   }
}

public class Driver {

   public static void main(String[] args) {
       Company company=new Company();
       HourlyEmployee hourlyEmployee=new HourlyEmployee(\"John\", 200);
       CommissionEmployee commissionEmployee= new CommissionEmployee(\"Jack\", 200000, 15);
       SalariedEmployee salariedEmployee= new SalariedEmployee(\"Nick\", 72000, 48);
       company.addEmployee(hourlyEmployee);
       company.addEmployee(commissionEmployee);
       company.addEmployee(salariedEmployee);
       company.displayAllEmployEarnings();
       company.removeEmployee(salariedEmployee);
       company.displayAllEmployEarnings();
   }

}

-------------------------------------------output--------------------------------------------------------------

John : 40000.0 $
Jack : 30000.0 $
Nick : 6000.0 $
Nick has been removed----------------------
John : 40000.0 $
Jack : 30000.0 $

In this lab we will create an employee hierarchy. At the top will be the abstract class Employee, which will hold an employee\'s name and the abstract method \
In this lab we will create an employee hierarchy. At the top will be the abstract class Employee, which will hold an employee\'s name and the abstract method \
In this lab we will create an employee hierarchy. At the top will be the abstract class Employee, which will hold an employee\'s name and the abstract method \
In this lab we will create an employee hierarchy. At the top will be the abstract class Employee, which will hold an employee\'s name and the abstract method \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site