Loading...
Loading...
A college canteen takes UPI, card and cash. Only a UPI payment earns cashback: 1% of the amount, rounded down. Store every payment in one Payment[]; only UpiPayment has a cashback() method. Print the total paid and the total cashback, reaching cashback() through an instanceof pattern.
UPI amount, CARD amount or CASH amount.Paid <total> on the first line and Cashback <total> on the second.Example
Input
4 UPI 250 CARD 1200 UPI 99 CASH 40
Output
Paid 1589 Cashback 2
Rs. 250 earns 2; Rs. 99 earns 0, because 99 / 100 rounds down.
Try the problem first. Open one hint at a time.
Create the right child for each line and store it in the Payment[]; CASH can be a plain Payment.
Inside the loop, if (p instanceof UpiPayment u) gives you a UpiPayment reference only for UPI payments.
Add every amount to the total paid, but only u.cashback() to the cashback total.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor