THINK FIRST·CODE LATER

← All labs

Product catalog: an array of objects

Problem

Write a class Product (name, category, price, stock; toString formatted %-10s %-8s %8.2f stock %d) and a public class ProductCatalog.

main reads n, then n products (name category price stock), then a name to search for and a category. It then:

  1. sorts the array by price with your own selection sort sortByPrice(Product[] items) (swap the references);
  2. prints the sorted list, the stock value (Σ price × stock), the number of products in stock in the category, and the result of findByName (ignore case; Found: … or … : not found).
Stock value: 6620.00
In stock in office: 3
Found: Lamp       home        89.00  stock 0

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l13-5-data-1.txt

5
Pen office 3.5 200
Lamp home 89 0
Stapler office 25 40
Mug home 18 60
Chair office 320 12
lamp office
What your program must print for l13-5-data-1.txt
By price:
  Pen        office       3.50  stock 200
  Mug        home        18.00  stock 60
  Stapler    office      25.00  stock 40
  Lamp       home        89.00  stock 0
  Chair      office     320.00  stock 12
Stock value: 6620.00
In stock in office: 3
Found: Lamp       home        89.00  stock 0

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l13-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l13-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.