Add a search by ID feature to the product viewer. This will require:
Adding another collection to the ProductCollectionsDAO
class that associates products with their IDs. You should use a map for this. Refer to the Java/Maps section of the reference for an example of how to use maps. You might want to take another look at lecture 5 if you are unsure of why a map is an appropriate data structure to use for this feature.
Adding code to the saveProduct
method that will also add the product being saved to the new collection (hint --- use the put
method).
Adding a new method to the DAO class for performing the search. It will take a product ID as a parameter and return a product. Name this method searchById
.
Implement the method to find the product that matches the given ID and return it.
The method should return null
if no matching product is found. This is useful for two reasons:
We can use this method to check if a product already exists. A null
result would indicate that the product does not exist.
The JList
will display nothing if you tell the model to display null
, so this means we don't have to explicitly handle the 'no product found' exceptional case in the GUI.
Adding components to the ProductViewer
dialog for the search feature (if you haven't already). Refer to the user interface mock-ups in the project specification if you are not sure what this should look like.
The search button click handler should take the ID entered by the user and use it to call the new method on the DAO to find the associated product.
Use the updateItems
method from the model (the SimpleListModel
object that the JList
is using) to display the product once you find it.