Loading...
Loading...
A cart holds normal items, sale items (a percent off the line, rounded down), combo items (buy 2, get 1 free: pay for qty − qty / 3 pieces) and gift cards (Rs. 10 fee per card). Write Item with long total() and long saving(), where a saving is the full price × qty minus the total. A gift card never counts as a saving. Print each line, the bill and the savings.
NORMAL name price qty, SALE name price qty percent, COMBO name price qty or GIFT name price qty.<name> <total> for each line, then Total <sum of totals>, then Saved <sum of savings>.Example
Input
4 NORMAL Notebook 60 5 SALE Shoes 1500 1 20 COMBO Soap 40 7 GIFT Card 500 2
Output
Notebook 300 Shoes 1200 Soap 200 Card 1020 Total 2720 Saved 380
Soap: pay for 7 − 2 = 5 pieces = 200, a saving of 80. The gift card adds Rs. 20 of fees but no saving.
Try the problem first. Open one hint at a time.
Put saving() in Item as price * qty - total(); only GiftCard overrides it to return 0.
Each child overrides total() with its own offer; saving() then works for them without changes.
SaleItem needs one extra field, the percent, passed through its constructor.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor