Loading...
Loading...
A sleeper bus has seats 1 to n. Bus books and cancels seats at a base fare; final class SleeperBus extends Bus charges an extra amount for lower berths (odd seat numbers). A cancellation refunds half the fare (rounded down).
Write BookingException extends Exception and three children: NoSuchSeatException, SeatTakenException and SeatFreeException. book and cancel throw them; main catches the parent BookingException once.
book seat NAME, cancel seat or show.BOOKED seat NAME Rs. f, CANCELLED seat REFUND r, or ERROR message with message NO SEAT s, SEAT s TAKEN BY NAME or SEAT s IS FREE. show prints 1:NAME 2:- … on one line. At the end, COLLECTED total (fares minus refunds).Example
Input
4 800 200 6 book 1 Asha book 2 Ravi book 1 Meena cancel 1 cancel 3 show
Output
BOOKED 1 Asha Rs. 1000 BOOKED 2 Ravi Rs. 800 ERROR SEAT 1 TAKEN BY Asha CANCELLED 1 REFUND 500 ERROR SEAT 3 IS FREE 1:- 2:Ravi 3:- 4:- COLLECTED 1300
Seat 1 is a lower berth (1000); its refund is 500. Seat 3 was never booked.
Try the problem first. Open one hint at a time.
Each child exception builds its message and passes it to super(...); the parent passes it on to Exception.
Check the seat range first, in one helper method both book and cancel call.
SleeperBus.fareFor(seat) returns super.fareFor(seat) plus the extra for odd seats; main uses it for the bill and the refund.
Keep String[] passengers; null means a free seat.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor