In: Computer Science
#2 Extend Employee and Manager classes created in lab#3. § Replace field year by the field hireDate of type java.util.Date § Your classes should implement Comparable interface. (Employee1 > Employee2 if its salary is more than the salary of Employee2, the same for managers, but if their salaries are equal, compare by bonus). (java oop)-> laboratory work
lab3# Create a class called Employee whose objects are records
for an employee. This class will be a derived class of the class
Person (MUST contain equals and toString methods). An employee
record has an employee's name (inherited from the class Person), an
annual salary represented as a single value of type double, a year
the employee started work as a single value of type int and a
national insuranceNumber, which is a value of type String. Inside
this class you need to override toString and equals methods of the
Person class. Your class should have a reasonable number of
constructors and accessor methods. Then create a class Manager
extending Employee, each manager has a team of Employees (Vector)
and can get a bonus. You need to override toString and equals
methods. Write another class containing a main method to fully test
your class definition. *Use super() keyword whenever possible.
Otherwise you will lose points. *Check the quality of your equals
and hashcode methods by adding several employees to a HashSet and
checking whether it allows duplicate items.
Person Class
import java.util.Objects; public class Person { protected String name; // protected so that Employee child class can inherit it. public Person() { } public Person(String name) { this.name = name; } // Equality is based on Name. 2 person with same name is considered same. // Question does not mention basis of equality, so this is the default implementation. @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(name, person.name); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } @Override public int hashCode() { return Objects.hash(name); } // Accessor Methods public String getName() { return name; } public void setName(String name) { this.name = name; } }
Employee Class
import java.util.Objects; public class Employee extends Person { // derived class of the class Person. Employee class has inherited name variable from Person class. See Person class. double salary; // an annual salary represented as a single value of type double, int yearOfStart; // a year the employee started work as a single value of type int String nationalInsuranceNumber; //national insuranceNumber, which is a value of type String. public Employee() { } public Employee(double salary) { this.salary = salary; } public Employee(double salary, int yearOfStart) { this.salary = salary; this.yearOfStart = yearOfStart; } public Employee(String name,double salary, int yearOfStart, String nationalInsuranceNumber) { super(name); this.salary = salary; this.yearOfStart = yearOfStart; this.nationalInsuranceNumber = nationalInsuranceNumber; } @Override public String toString() { return "Employee{" + "salary=" + salary + ", yearOfStart=" + yearOfStart + ", nationalInsuranceNumber='" + nationalInsuranceNumber + '\'' + ", name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; Employee employee = (Employee) o; return Double.compare(employee.salary, salary) == 0 && yearOfStart == employee.yearOfStart && Objects.equals(nationalInsuranceNumber, employee.nationalInsuranceNumber); } @Override public int hashCode() { return Objects.hash(super.hashCode(), salary, yearOfStart, nationalInsuranceNumber); } // Accessor Methods public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public int getYearOfStart() { return yearOfStart; } public void setYearOfStart(int yearOfStart) { this.yearOfStart = yearOfStart; } public String getNationalInsuranceNumber() { return nationalInsuranceNumber; } public void setNationalInsuranceNumber(String nationalInsuranceNumber) { this.nationalInsuranceNumber = nationalInsuranceNumber; } }