In: Computer Science
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
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:)