labs / tiddlers / content / labs / lab04 / _Labs_04_Search by

Add a search by ID feature to the product viewer. This will require:

  1. 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.

  2. Adding code to the saveProduct method that will also add the product being saved to the new collection (hint --- use the put method).

  3. 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.

  4. 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.

  5. 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.

  6. Use the updateItems method from the model (the SimpleListModel object that the JList is using) to display the product once you find it.