Loading...
Loading...
A clinic's token machine gives at most 5 tokens a day, numbered 1, 2, 3… in order. The doctor calls patients in token order.
Write a class Token with static final int DAILY_LIMIT = 5, a static int issued counter, and final fields number and patient set by the constructor. Keep the tokens in an array of size DAILY_LIMIT and handle the commands.
take name, next or status.take: TOKEN k name, or NO TOKENS LEFT FOR name after 5 tokens. For next: CALLING k name or NO ONE WAITING. For status: ISSUED a SERVED b WAITING c LEFT d.Example
Input
5 take Asha take Ravi next status next
Output
TOKEN 1 Asha TOKEN 2 Ravi CALLING 1 Asha ISSUED 2 SERVED 1 WAITING 1 LEFT 3 CALLING 2 Ravi
Asha is called first; after Ravi is called nobody is waiting.
Try the problem first. Open one hint at a time.
issued is shared by the class, so the constructor can take the next number without help from main.
Check Token.issued == Token.DAILY_LIMIT before calling new Token(...).
Keep a served count in main: the next patient to call is tokens[served].
Waiting is issued - served, and left is DAILY_LIMIT - issued.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor