Loading...
Loading...
At checkout each item can carry one coupon. The rules are tiny, so each one is a lambda of one functional interface.
Write @FunctionalInterface interface Discount { int apply(int price); } and a method static int pay(int price, Discount d) that never returns less than 0. Coupons: FLAT50 (Rs. 50 off), TEN (10% off, integer maths), BIG (Rs. 100 off when the price is 1000 or more, else no change) and NONE.
price -> final, then TOTAL x.Example
Input
4 300 FLAT50 2000 TEN 1200 BIG 999 BIG
Output
300 -> 250 2000 -> 1800 1200 -> 1100 999 -> 999 TOTAL 4149
999 is below 1000, so BIG changes nothing.
Try the problem first. Open one hint at a time.
Write each rule once as a lambda, for example Discount flat = p -> p - 50;.
The "never below 0" rule belongs in pay, so no lambda has to repeat it.
A rule with an if needs a block body: p -> { if (p >= 1000) { return p - 100; } return p; }.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor