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
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
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...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for taxes (we will do a flat 17%), and the final amount (for...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT