← Back to Week 3 Hub
Workbook 3a — Exercise

Search Inventory

Store products in an ArrayList and display a formatted inventory  |  Workbook p.49

The Exercise

Create a program called search-inventory that holds a list of products. Each product has three pieces of data: an id (int), a name (String), and a price (float). Store these products inside an ArrayList so the list can grow and shrink. Start by hardcoding five products directly in the code. When the program runs, loop through the list and print each product in a neat, aligned format using printf.

Tip: Use a Product class with a constructor and getters. A separate method like getInventory() can build and return the ArrayList<Product> so main stays clean.
Example Runs
Run 1 — Five hardcoded products
id: 1234 Hammer - Price: $19.49
id: 2345 Screwdriver Set - Price: $24.99
id: 3456 Cordless Drill - Price: $89.00
id: 4567 10' 2x4 (grade B) - Price: $9.99
id: 5678 Box of Nails (100) - Price: $6.49
Run 2 — Bonus: loading from inventory.csv
(inventory.csv loaded — 14 products)

id: 1234 Hammer - Price: $19.49
id: 2345 Screwdriver Set - Price: $24.99
id: 3456 Cordless Drill - Price: $89.00
id: 4567 10' 2x4 (grade B) - Price: $9.99
id: 5678 Box of Nails (100) - Price: $6.49
id: 6789 Paint Roller - Price: $12.75
... (more products)
Run 3 — Bonus: menu prompt
What would you like to do?
1 - List all products
2 - Lookup by id
3 - Find in price range
4 - Add new product
5 - Quit

Enter your choice: 2
Enter product id: 3456

id: 3456 Cordless Drill - Price: $89.00
Concepts You'll Use
Flow
Create Product class id, name, price + getters
getInventory() Returns ArrayList<Product>
Loop + printf One formatted line per product
Bonus: load from CSV BufferedReader, split on "|"
Bonus: sort by name Collections.sort + Comparator
Bonus: menu loop switch on 1-5 until Quit

Workbook 3a, p.49 — Exercise: Search Inventory

← Search Engine Logger Search Inventory (HashMap) →