Loading...
Loading...
A railway counter books tickets. If the passenger does not name a coach, the ticket is for Sleeper (SL). The fare is the distance times the rate per km of the coach, plus a booking charge of 20: SL 1, 3A 2, 2A 3 and 1A 5 rupees per km.
Write a class Ticket with fields passenger, distance, coach and fare. The constructor Ticket(String passenger, int distance, String coach) works out the fare, and Ticket(String passenger, int distance) calls it with "SL".
passenger coach fare.Example
Input
3 Asha 100 Ravi 250 3A Meena 40 1A
Output
Asha SL 120 Ravi 3A 520 Meena 1A 220
Asha named no coach, so she travels Sleeper: 100 × 1 + 20.
Try the problem first. Open one hint at a time.
The two-argument constructor is one line: this(passenger, distance, "SL");.
Work out the fare inside the full constructor, so every ticket gets it however it was built.
A small method int ratePerKm() keeps the rate table in one place.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor