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.
Add a major
field to the Student
domain class. Generate getter/setter methods for this field.
Initialise the field in the constructor that initialises the other fields.
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).
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:
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.
Modify the save
method so that the student is saved in the multimap in addition to the existing map.
Modify the delete
method so that the student is removed from the multimap.
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.
Add the getMajors
method to the class. It should return the keySet()
of the multimap.
Add the filterByMajor
and getMajor
methods to the StudentJdbiDAO
interface. Add appropriate annotations.
You will also need to add a major
field to your student table in the database.
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