Loading...
Loading...
Java · Abstraction & Interfaces
A student wallet pays a list of bills in order. Electricity and water are utility bills: units × rate + a meter charge. A library fine is Rs. 2 per late day, at most Rs. 50. A bill the wallet cannot cover is skipped; after a top-up, the skipped bills are tried again in the same order.
Write an interface Payable (String label(), int amountDue()), an abstract class UtilityBill implements Payable with a final amountDue(), the classes Electricity (rate 6, meter 50) and Water (rate 2, meter 20), and LibraryFine implements Payable directly. The wallet's balance is private and changes only through boolean pay(Payable p).
electricity units, water units or fine days. The last line has the top-up T.PAID label Rs. x, left y or SKIPPED label Rs. x. After the first round print TOPUP T, balance b. At the end print UNPAID k (bills still skipped) and BALANCE b. Labels are Electricity, Water and Library fine.Example
Input
500 3 electricity 100 water 50 fine 7 600
Output
SKIPPED Electricity Rs. 650 PAID Water Rs. 120, left 380 PAID Library fine Rs. 14, left 366 TOPUP 600, balance 966 PAID Electricity Rs. 650, left 316 UNPAID 0 BALANCE 316
Electricity (650) is skipped, water (120) and the fine (14) are paid, then the top-up covers electricity.
Try the problem first. Open one hint at a time.
pay returns false and changes nothing when amountDue() is more than the balance.
Keep the bills in a Payable[] and the skipped ones in a boolean[] of the same size.
The second round visits only bills that are still marked skipped.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor