Loading...
Loading...
Java · Abstraction & Interfaces
A shop accepts UPI, card and net banking. Every payment adds its own fee, and any amount outside Rs. 1 to Rs. 100000 is refused.
Write an interface Payment with abstract String name() and int fee(int amount), a default int total(int amount) that returns amount + fee, and a static boolean isValid(int amount). Upi has no fee, Card charges 2% (integer maths) and NetBanking charges Rs. 10.
upi, card or netbanking) and an amount.NAME amount + fee = total, or INVALID NAME amount. Then COLLECTED x, the sum of all valid totals.Example
Input
4 upi 500 card 2000 netbanking -20 netbanking 1500
Output
UPI 500 + 0 = 500 CARD 2000 + 40 = 2040 INVALID NETBANKING -20 NETBANKING 1500 + 10 = 1510 COLLECTED 4050
The card fee is 2000 × 2 / 100 = 40; -20 is refused.
Try the problem first. Open one hint at a time.
A default method can call the abstract fee(); each object supplies its own.
Call the static check as Payment.isValid(amount), never through an object.
Names in the output are UPI, CARD and NETBANKING.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor