Loading...
Loading...
A shop's cart log has n lines id name price, with the price in whole paise. The same product (same id) can appear again with a newer price or a differently typed name. Write Product with a final id and equals() and hashCode() that use only the id. Print each distinct product in order of first appearance with its first name and its latest price, then the total of the latest prices.
#<id> <name> Rs. <rupees>.<paise> per distinct product (paise always two digits), then Total: Rs. <rupees>.<paise>.Example
Input
4 501 Notebook 6000 502 Pen 1050 501 NotebookA4 5500 503 Eraser 500
Output
#501 Notebook Rs. 55.00 #502 Pen Rs. 10.50 #503 Eraser Rs. 5.00 Total: Rs. 70.50
Product 501 keeps its first name and its latest price, Rs. 55.00. Total 5500 + 1050 + 500 = 7050 paise.
Try the problem first. Open one hint at a time.
Equality by a final id means a new price never changes which product it is.
Keep an array of distinct products; for each line, find an equal one with equals() and update its price, or add the new product.
Rupees = paise / 100; the rest = paise % 100, with a leading 0 when it is below 10.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor