Newer
Older
labs / tiddlers / content / labs / lab07 / _Labs_07_Filtering by Major.md

Let's add the ability to filter the students in the view-students.jsp page by their major. We haven't incuded the major in earlier versions of the student system since we were trying to keep the system as simple as possible --- we need to add the major to the system now.

  1. Add a major field to the Student domain class. Generate getter/setter methods for this field.

  2. Initialise the field in the constructor that initialises the other fields.

  3. Add the following methods to the StudentDAO interface:

    filterByMajor --- it should take a major as a parameter and return a collection of students.
    getMajors --- it should return a collection of majors (strings).

  4. The easiest way to do this is use a <>.

    Most maps are one-to-one --- one key maps to one value. A multimap can map a single key to multiple values. This is the perfect data structure for mapping students to majors (or products to categories).

    The lab07 project that you were given this week has a filterByMajor method that uses a multimap. Refer to the StudentCollectionsDAO class for reference:

    https://isgb.otago.ac.nz/info202/shared/lab07/blob/master/src/main/java/dao/StudentCollectionsDAO.java

  5. If you haven't done so already then add the Guava dependency that was mentioned in the <> section to your <> file. Guava is Google's utility library that contains all sorts of things including a multimap implementation.

  6. Modify the save method so that the student is saved in the multimap in addition to the existing map.

  7. Modify the delete method so that the student is removed from the multimap.

  8. Add the filterByMajor method to the class. It should get the values (students) from the map for the given key (major) and return the collection.

  9. Add the getMajors method to the class. It should return the keySet() of the multimap.

  10. Add the filterByMajor and getMajor methods to the StudentJdbiDAO interface. Add appropriate annotations.

  11. You will also need to add a major field to your student table in the database.

  12. Add tests to your DAO test to test the filterByMajor and getMajors methods. Test both of your DAO classes to make sure that both implementations work. Refer lab 7 again:

    https://isgb.otago.ac.nz/info202/shared/lab07/blob/master/src/test/java/dao/StudentDAOTest.java