/
11 - Collections - Exercise

11 - Collections - Exercise

Overview

The aim for this exercise is to replace the Array in our Employee application with a Collection. We will also implement multiple ways of sorting the collection.

Task 1 - Replace Array with a Collection

  1. Reopen your EmployeeManagement project we created (06 - OOP Intro - Exercise 1 ) previously.

  2. In the EmployeeApplication class, locate the main method and replace the Array of three employees with a suitable Collection object. (Hint: maybe one of the List implementation?)

  3. Use the enhanced for loop when iterating through your collection.

  4. In the Manager class, amend the addEmployee method to NOT allow a manager to manage more than 5 employees.

  5. Add the jar dependencies to your project as we did before and unit test your Employee and Manager classes. Which methods need special attention?

Task 2 - Sorting a collection of employees

  1. Create a new class called EmployeeNameComparator, which implements java.util.Comparator interface.

  2. Provide the public int compare(Employee e1, Employee e2) method. You can use String's compareTo method on the employee's name.

  3. Sort the list of employees from the previous task. Use the class method defined on Collections class and provide a new instance of your comparator class.

  4. Print out the content of the list and check that the employees are indeed sorted.

  5. Create a unit test for this functionality.