Loading...
Loading...
A checkout pays through whichever payment method is selected, and the method can be changed between payments. Each method keeps its own state.
Write an interface PaymentMethod with boolean pay(int amount) and String label(), and three classes: UpiPayment (refuses any single payment above Rs. 1000), CardPayment (a credit limit that goes down with each payment) and WalletPayment (a balance that goes down). Checkout holds one PaymentMethod and delegates to it; it starts with UPI.
use upi|card|wallet or pay AMOUNT.pay: PAID A BY LABEL or DECLINED A BY LABEL (labels UPI, CARD, WALLET). use prints nothing. At the end: TOTAL PAID T.Example
Input
3000 500 6 pay 800 pay 1500 use card pay 1500 use wallet pay 600
Output
PAID 800 BY UPI DECLINED 1500 BY UPI PAID 1500 BY CARD DECLINED 600 BY WALLET TOTAL PAID 2300
UPI refuses 1500, the card accepts it, and the wallet has only Rs. 500.
Try the problem first. Open one hint at a time.
Create the three payment objects once in main and pass the chosen one to checkout.setMethod(...), so each keeps its state.
Checkout.pay only calls method.pay(amount) and adds to the total when it returns true.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor