Loading...
Loading...
A ride app offers auto, cab and bike rides. Each ride type has its own per-km rate, booking fee and minimum fare, and at busy times a surge rule changes the fare.
Write @FunctionalInterface interface Surge { int apply(int fare); }. Write an abstract class Ride with a constructor for km, abstract perKm(), bookingFee(), minFare() and type(), and a final int fare(Surge s) that returns the larger of minFare() and s.apply(km × perKm() + bookingFee()). Auto: Rs. 15/km, Rs. 10, minimum 30. Cab: Rs. 20/km, Rs. 30, minimum 80. Bike: Rs. 8/km, Rs. 5, minimum 25. Surge none keeps the fare, peak multiplies it by 3/2 (integer maths) and rain adds Rs. 30.
auto, cab or bike) and km.TYPE km km: Rs. fare, then TOTAL x.Example 1
Input
peak 3 auto 4 cab 10 bike 1
Output
AUTO 4 km: Rs. 105 CAB 10 km: Rs. 345 BIKE 1 km: Rs. 25 TOTAL 475
Auto: (60 + 10) × 3 / 2 = 105. Bike: (8 + 5) × 3 / 2 = 19, raised to the minimum 25.
Example 2
Input
rain 2 auto 0 cab 2
Output
AUTO 0 km: Rs. 40 CAB 2 km: Rs. 100 TOTAL 140
A 0 km auto: 10 + 30 = 40, above the minimum 30.
Try the problem first. Open one hint at a time.
Build the three surge rules as lambdas and pick one before the loop.
Apply the surge first, then the minimum; the minimum is a fixed step, so it lives in the final method.
Use Math.max(minFare(), ...) or an if.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor