Loading...
Loading...
A railway station has k booking counters. Every ticket gets a PNR from one shared sequence starting at 1001, whichever counter sells it. The base fare is ₹500: children under 5 travel free, children under 12 pay half, and passengers aged 60 or more pay 60%. A cancelled ticket refunds half its fare (rounded down) from the counter that sold it.
Write a class Ticket (static nextPnr, final fields pnr, name, fare and soldBy, plus cancelled) and a class Counter (static final int BASE_FARE, static long totalCollected, a final int id, and instance fields sold and collected).
book c name age, cancel pnr or report.book: PNR p name FARE f AT COUNTER c, or NO COUNTER c. cancel: CANCELLED p REFUND r, or INVALID PNR p (unknown or already cancelled). report: one line COUNTER i SOLD s COLLECTED x per counter, then TOTAL x. SOLD counts tickets that are not cancelled.Example
Input
2 6 book 1 Asha 30 book 2 Ravi 8 book 1 Dadi 65 cancel 1002 cancel 1002 report
Output
PNR 1001 Asha FARE 500 AT COUNTER 1 PNR 1002 Ravi FARE 250 AT COUNTER 2 PNR 1003 Dadi FARE 300 AT COUNTER 1 CANCELLED 1002 REFUND 125 INVALID PNR 1002 COUNTER 1 SOLD 2 COLLECTED 800 COUNTER 2 SOLD 0 COLLECTED 125 TOTAL 925
PNRs continue across counters; Ravi's ₹250 ticket refunds ₹125, and a second cancel is refused.
Try the problem first. Open one hint at a time.
Shared things are static (nextPnr, totalCollected, BASE_FARE); each counter's own sold and collected are instance fields.
Give Counter a method Ticket book(String name, int age) that passes this as the ticket's soldBy.
PNR p is stored at index p - 1001 of a Ticket[] of size q.
On cancel, update the selling counter and the static total together, in one method.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor