Loading...
Loading...
Railway tickets are printed in one fixed format, but each class prices the trip differently. The fare is km × rate + a reservation charge, and no ticket costs less than Rs. 30.
Write an abstract class Ticket with a constructor for the passenger and km, abstract int rate(), int reservation() that returns 0, and a final String print(). General has rate 1, Sleeper rate 2 plus Rs. 20 reservation, and Ac3 rate 5 plus Rs. 40 reservation.
general, sleeper or ac3), a one-word passenger name and km.NAME CLASS Rs. fare, with CLASS as GENERAL, SLEEPER or AC3.Example
Input
3 sleeper Arjun 600 ac3 Diya 600 general Kavya 12
Output
Arjun SLEEPER Rs. 1220 Diya AC3 Rs. 3040 Kavya GENERAL Rs. 30
Kavya's 12 km general ticket is raised to the Rs. 30 minimum.
Try the problem first. Open one hint at a time.
print() does the fixed steps: work out the fare, apply the minimum, build the line.
Only Sleeper and Ac3 override reservation(); General keeps the parent's 0.
Use Math.max(30, fare) or an if for the minimum.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor