Question

In: Computer Science

java/netbeans 1.Design an abstract class Employee that stores: employee name, unique auto-generated id, hire year. Implement...

java/netbeans

1.Design an abstract class Employee that stores: employee name, unique auto-generated id, hire year. Implement any necessary methods in addition to:

a.The toString and equals methods. The equals method should NOT compare the id.

b.The copy constructor (should copy the same id too)

c.An abstract method float getWeeklyCheckAmount()

d.Implement the appropriate interface to be able to sort employee names in alphabetical order. Subclasses should NOT be allowed to override this implementation.

2.Design an abstract class HourlyWorker that extends Employee and stores: hourly rate and number of hours worked a week. Implement any needed methods.

3.Design an abstract class SalariedWorker that extends Employee and stores: annual salary. Implement any needed methods.

4.Design a class Secretary that extends HourlyWorker and stores: weekly bonus. Implement any needed methods.

5.Design a class SalesWorker that extends HourlyWorker and stores: number of sales made and commission amount per sale. Implement any needed methods.

6.Design a class Manager that extends SalariedWorker and stores a list of Employees being managed.

7.Write a main method that:

a.Asks the user for the name of the Manager, hire year, annual salary and creates one.

b.Asks the user for the number of employees managed. For each employee, ask if it is a Secretary or a SalesWorker. Ask for the appropriate information. Add the employee to the list of Employees stored in the Manager.

c.Repeat the above steps for a second manager.

d.Print whether the 2 Manager objects are equal.

e.For the first Manager, print the Secretaries first and then the SalesWorkers.

f.Print all Employees for the first Manager in alphabetical order.

g.Print all the Employees for the first Manager according to their weekly check amount using the comparator interface.

Print all Employees for the first Manager in ascending order of hire year using lambda expressions

Solutions

Expert Solution

Answer;

Hi,

Please find the below code according to the given requirements.

Note: as the implementation requirements for the float getWeeklyCheckAmount() method is not given in the question, it is not implemented. you can just write the logic for it.

*********************************************************************************************

Employee.java

*********************************************************************************************

public abstract class Employee implements Comparable<Employee>{

   private String name;
   private int hireYear;
   private int id = (int) Math.random()*100;




   /**
   * @param name
   * @param hireYear
   */
   public Employee(String name, int hireYear) {
    
       this.name = name;
       this.hireYear = hireYear;
   }

   public abstract float getWeeklyCheckAmount();

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }


   /**
   * @param name the name to set
   */
   public void setName(String name) {
       this.name = name;
   }


   /**
   * @return the hireYear
   */
   public int getHireYear() {
       return hireYear;
   }


   /**
   * @param hireYear the hireYear to set
   */
   public void setHireYear(int hireYear) {
       this.hireYear = hireYear;
   }


   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   @Override
   public int compareTo(Employee o) {
       // TODO Auto-generated method stub
       return this.getName().compareTo(o.getName());
   }

   @Override
   public String toString() {
       // TODO Auto-generated method stub
       return super.toString();
   }


   /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + hireYear;
       result = prime * result + ((name == null) ? 0 : name.hashCode());
       return result;
   }


   /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Employee other = (Employee) obj;
       if (hireYear != other.hireYear)
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }

}

*********************************************************************************************

HourlyWorker.java

*********************************************************************************************

public abstract class HourlyWorker extends Employee {


   private int noOfHoursWorkedAWeek;
   private int hourlyRate;



   /**
   * @param name
   * @param hireYear
   * @param noOfHoursWorkedAWeek
   * @param hourlyRate
   */
   public HourlyWorker(String name, int hireYear, int noOfHoursWorkedAWeek, int hourlyRate) {
       super(name, hireYear);
       this.noOfHoursWorkedAWeek = noOfHoursWorkedAWeek;
       this.hourlyRate = hourlyRate;
   }
   /**
   * @return the noOfHoursWorkedAWeek
   */
   public int getNoOfHoursWorkedAWeek() {
       return noOfHoursWorkedAWeek;
   }
   /**
   * @param noOfHoursWorkedAWeek the noOfHoursWorkedAWeek to set
   */
   public void setNoOfHoursWorkedAWeek(int noOfHoursWorkedAWeek) {
       this.noOfHoursWorkedAWeek = noOfHoursWorkedAWeek;
   }
   /**
   * @return the hourlyRate
   */
   public int getHourlyRate() {
       return hourlyRate;
   }
   /**
   * @param hourlyRate the hourlyRate to set
   */
   public void setHourlyRate(int hourlyRate) {
       this.hourlyRate = hourlyRate;
   }

}

*********************************************************************************************

SalariedWorker.java

*********************************************************************************************

public abstract class SalariedWorker extends Employee{

   private int annualSalary;

   /**
   * @param name
   * @param hireYear
   * @param annualSalary
   */
   public SalariedWorker(String name, int hireYear, int annualSalary) {
       super(name, hireYear);
       this.annualSalary = annualSalary;
   }

   /**
   * @return the annualSalary
   */
   public int getAnnualSalary() {
       return annualSalary;
   }

   /**
   * @param annualSalary the annualSalary to set
   */
   public void setAnnualSalary(int annualSalary) {
       this.annualSalary = annualSalary;
   }

}

*********************************************************************************************

Secretary.java

*********************************************************************************************

public class Secretary extends HourlyWorker{
   private int weeklyBonus;

   /**
   * @param name
   * @param hireYear
   * @param noOfHoursWorkedAWeek
   * @param hourlyRate
   * @param weeklyBonus
   */
   public Secretary(String name, int hireYear, int noOfHoursWorkedAWeek, int hourlyRate, int weeklyBonus) {
       super(name, hireYear, noOfHoursWorkedAWeek, hourlyRate);
       this.weeklyBonus = weeklyBonus;
   }

   /**
   * @return the weeklyBonus
   */
   public int getWeeklyBonus() {
       return weeklyBonus;
   }

   /**
   * @param weeklyBonus the weeklyBonus to set
   */
   public void setWeeklyBonus(int weeklyBonus) {
       this.weeklyBonus = weeklyBonus;
   }

   @Override
   public float getWeeklyCheckAmount() {
       // TODO Auto-generated method stub
       return 0;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Secretary [weeklyBonus=" + weeklyBonus + ", getNoOfHoursWorkedAWeek()=" + getNoOfHoursWorkedAWeek()
               + ", getHourlyRate()=" + getHourlyRate() + ", getName()=" + getName() + ", getHireYear()="
               + getHireYear() + ", getId()=" + getId() + "]";
   }

}

*********************************************************************************************

SalesWorker.java

*********************************************************************************************

public class SalesWorker extends HourlyWorker{

   private int noOfSalesMade;
   private int commisionAmount;


   /**
   * @param name
   * @param hireYear
   * @param noOfHoursWorkedAWeek
   * @param hourlyRate
   * @param noOfSalesMade
   * @param commisionAmount
   */
   public SalesWorker(String name, int hireYear, int noOfHoursWorkedAWeek, int hourlyRate, int noOfSalesMade,
           int commisionAmount) {
       super(name, hireYear, noOfHoursWorkedAWeek, hourlyRate);
       this.noOfSalesMade = noOfSalesMade;
       this.commisionAmount = commisionAmount;
   }
   /**
   * @return the noOfSalesMade
   */
   public int getNoOfSalesMade() {
       return noOfSalesMade;
   }
   /**
   * @param noOfSalesMade the noOfSalesMade to set
   */
   public void setNoOfSalesMade(int noOfSalesMade) {
       this.noOfSalesMade = noOfSalesMade;
   }
   /**
   * @return the commisionAmount
   */
   public int getCommisionAmount() {
       return commisionAmount;
   }
   /**
   * @param commisionAmount the commisionAmount to set
   */
   public void setCommisionAmount(int commisionAmount) {
       this.commisionAmount = commisionAmount;
   }
   @Override
   public float getWeeklyCheckAmount() {
       // TODO Auto-generated method stub
       return 0;
   }
   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "SalesWorker [noOfSalesMade=" + noOfSalesMade + ", commisionAmount=" + commisionAmount
               + ", getNoOfHoursWorkedAWeek()=" + getNoOfHoursWorkedAWeek() + ", getHourlyRate()=" + getHourlyRate()
               + ", getName()=" + getName() + ", getHireYear()=" + getHireYear() + ", getId()=" + getId() + "]";
   }

}

*********************************************************************************************

Manager.java

*********************************************************************************************

import java.util.List;

public class Manager extends SalariedWorker{

   public Manager(String name, int hireYear, int annualSalary) {
       super(name, hireYear, annualSalary);
   }


   private List<Employee> empList;


   /**
   * @return the empList
   */
   public List<Employee> getEmpList() {
       return empList;
   }


   /**
   * @param empList the empList to set
   */
   public void setEmpList(List<Employee> empList) {
       this.empList = empList;
   }


   @Override
   public float getWeeklyCheckAmount() {
       // TODO Auto-generated method stub
       return 0;
   }


   /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = super.hashCode();
       result = prime * result + ((empList == null) ? 0 : empList.hashCode());
       return result;
   }


   /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (!super.equals(obj))
           return false;
       if (getClass() != obj.getClass())
           return false;
       Manager other = (Manager) obj;
       if (empList == null) {
           if (other.empList != null)
               return false;
       } else if (!empList.equals(other.empList))
           return false;
       return true;
   }



}

*********************************************************************************************

SortByWeeklyCheck.java

*********************************************************************************************

import java.util.Comparator;

public class SortByWeeklyCheck implements Comparator<Employee>{

   @Override
   public int compare(Employee o1, Employee o2) {
       return (int)(o1.getWeeklyCheckAmount()-o2.getWeeklyCheckAmount());
   }

}

*********************************************************************************************

Tester.java

*********************************************************************************************

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Tester {

   public static void main(String[] args) {
    
       Scanner sc = new Scanner(System.in);
       int hireYear,annualSalary,noOfEmployees,type,hourlyRate,noOfHoursWorkedAWeek,weeklyBonus,commisionAmount,noOfSalesMade;
       String name;
    
       List<Employee> empList1 = new ArrayList<>();
    
       System.out.println("What is the manager's Name?");
       name = sc.nextLine();
    
       System.out.println("What year was the manager hired?");
       hireYear = sc.nextInt();

       System.out.println("What is the manager's annual salary?");
       annualSalary = sc.nextInt();

       Manager manager1 = new Manager(name, hireYear, annualSalary);
    
       System.out.println("How many employees are working under this manager?");
       noOfEmployees = sc.nextInt();

       for(int i=0;i<=noOfEmployees;i++) {
        
           System.out.println("Enter 1 for Secretary and 2 for Sales Worker?");
           type = sc.nextInt();
        
           if(type==1) {
               System.out.println("Enter secretary's name: ");
               name = sc.nextLine();
            
               System.out.println("Enter hire year: ");
               hireYear = sc.nextInt();

               System.out.println("Enter hourly rate:");
               hourlyRate= sc.nextInt();
            
               System.out.println("Enter weekly bonus:");
               weeklyBonus = sc.nextInt();
            
               System.out.println("Enter number of hours a week:");
               noOfHoursWorkedAWeek = sc.nextInt();
               Employee secretary = new Secretary(name, hireYear, noOfHoursWorkedAWeek, hourlyRate, weeklyBonus);
               empList1.add(secretary);
           }
           else if(type==2) {
               System.out.println("Enter sales worker's name: ");
               name = sc.nextLine();
            
               System.out.println("Enter hire year: ");
               hireYear = sc.nextInt();

               System.out.println("Enter hourly rate:");
               hourlyRate= sc.nextInt();
            
               System.out.println("Enter commission per sale:");
               commisionAmount= sc.nextInt();
            
               System.out.println("Enter number of hours a week:");
               noOfHoursWorkedAWeek = sc.nextInt();
            
               System.out.println("Enter number of sales made:");
               noOfSalesMade= sc.nextInt();
               Employee salesWorker = new SalesWorker(name, hireYear, noOfHoursWorkedAWeek, hourlyRate, noOfSalesMade, commisionAmount);
               empList1.add(salesWorker);
            
           }
       }
    
       manager1.setEmpList(empList1);
    
       List<Employee> empList2 = new ArrayList<>();
    
       System.out.println("What is the manager's Name?");
       name = sc.nextLine();
    
       System.out.println("What year was the manager hired?");
       hireYear = sc.nextInt();

       System.out.println("What is the manager's annual salary?");
       annualSalary = sc.nextInt();

       Manager manager2 = new Manager(name, hireYear, annualSalary);
    
       System.out.println("How many employees are working under this manager?");
       noOfEmployees = sc.nextInt();

       for(int i=0;i<=noOfEmployees;i++) {
        
           System.out.println("Enter 1 for Secretary and 2 for Sales Worker?");
           type = sc.nextInt();
        
           if(type==1) {
               System.out.println("Enter secretary's name: ");
               name = sc.nextLine();
            
               System.out.println("Enter hire year: ");
               hireYear = sc.nextInt();

               System.out.println("Enter hourly rate:");
               hourlyRate= sc.nextInt();
            
               System.out.println("Enter weekly bonus:");
               weeklyBonus = sc.nextInt();
            
               System.out.println("Enter number of hours a week:");
               noOfHoursWorkedAWeek = sc.nextInt();
               Employee secretary = new Secretary(name, hireYear, noOfHoursWorkedAWeek, hourlyRate, weeklyBonus);
               empList2.add(secretary);
           }
           else if(type==2) {
               System.out.println("Enter sales worker's name: ");
               name = sc.nextLine();
            
               System.out.println("Enter hire year: ");
               hireYear = sc.nextInt();

               System.out.println("Enter hourly rate:");
               hourlyRate= sc.nextInt();
            
               System.out.println("Enter commission per sale:");
               commisionAmount= sc.nextInt();
            
               System.out.println("Enter number of hours a week:");
               noOfHoursWorkedAWeek = sc.nextInt();
            
               System.out.println("Enter number of sales made:");
               noOfSalesMade= sc.nextInt();
               Employee salesWorker = new SalesWorker(name, hireYear, noOfHoursWorkedAWeek, hourlyRate, noOfSalesMade, commisionAmount);
               empList2.add(salesWorker);
            
           }
       }
       manager2.setEmpList(empList2);
    
       if(manager1.equals(manager2))
           System.out.println("\n---- Managers entered are equal!");
       else
           System.out.println("\n---- Managers entered are NOT equal!");

       for(Employee employee : manager1.getEmpList()) {
           if(employee instanceof Secretary) {
               System.out.println("\n**** Secretary Employees:");
               System.out.println("\t" + employee);
           }
       }
    
       for(Employee employee : manager1.getEmpList()) {
           if(employee instanceof SalesWorker) {
               System.out.println("**** Sales Employees:");
               System.out.println("\t" + employee);
           }
       }

    

    

       System.out.println("\n---- Employees sorted alphabetically:");
       Collections.sort(manager1.getEmpList());
       System.out.println(manager1.getEmpList());

       System.out.println("\n---- Employees sorted on weekly check:");
       Collections.sort(manager1.getEmpList(),new SortByWeeklyCheck());
       System.out.println(manager1.getEmpList());
    
       System.out.println("\n---- Employees sorted on hire year:");
       Comparator<Employee> lambda= (emp1,emp2) -> emp1.getHireYear()-emp2.getHireYear();
       manager1.getEmpList().sort(lambda);
       System.out.println(manager1.getEmpList());
    
   }

}

Hope this helps you!!

Have a Good day!!

Thank you:)


Related Solutions

JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
Using UML, design a class that allows you to store and print employee information: name, id,...
Using UML, design a class that allows you to store and print employee information: name, id, and hourly_salary. Include the appropriate mutator and accessor methods. Write the code to create the class. Write a test program that instantiates an employee object. After the object is created write the code to give the employee a 3% raise. (IN PYTHON)
Develop an algorithm to implement an employee list with employee ID ,name designation and department using...
Develop an algorithm to implement an employee list with employee ID ,name designation and department using link list and perform the following operation on the list i)add employee details based on department ii)remove employee details iv)count the number of employee in each department
Write an SQL query that will output the employee id, first name and hire date from...
Write an SQL query that will output the employee id, first name and hire date from the employee table. Pick only those employees whose employee ID is specified in the employee table (no nulls). If the employee id is 777, name is ABC and hire date is 01-JAN-2016, the output should be like - ' ABC (Employee ID - 777) was hired on 1, January of 2016'. Note - The date should not have preceding zeros.
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee...
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee id Employee hours worked per week Employee Pay Rate Menu with Option to: Print out Employee Report in the following report format Search for an Employee Display the Report in Sorted based on Last Name or ID Calculate the Pay Quit Criteria: Be sure to use Parallel Arrays to Store the Employee Information Output must be formatted and line up with the column header...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT