A ticket machine gives every new ticket the next number: the first ticket gets 1, the next one 2, and so on. The counter belongs to all the tickets together, not to any one of them. Go has no classes and no static: as in the Static Members lesson, the shared counter is a package-level variable, one copy that every function in the package sees, and the "static methods" are plain functions. Each ticket keeps its own number, copied from the counter when the ticket is created.
Write:
- The
Ticketstruct, with two fields:name(astring) andnumber(anint). - The package-level counter
ticketCount, anint. NewTicket(name): it adds 1 toticketCountand returns a newTicketwith that name and the new value ofticketCountas its number.issued(): returns how many tickets were created since the counter was last reset.resetCounter(): sets the counter back to 0, so the next ticket gets number 1 again.
For each test case, the test code calls resetCounter(), then creates one ticket per name with NewTicket, in order. Once all the tickets of the case exist, it prints each ticket as its name, a space, # and its number, one ticket per line, and then the value of issued() on a line of its own.
Example 1:
Input:
NewTicket("Ann")
NewTicket("Bob")
NewTicket("Cy")
Output:
Ann #1
Bob #2
Cy #3
3
Explanation: resetCounter() set the counter to 0. Each new ticket adds 1 to it and keeps the new value,
so the tickets get 1, 2 and 3, and issued() returns 3.
Example 2:
Input:
NewTicket("Mia")
NewTicket("Mia")
Output:
Mia #1
Mia #2
2
Explanation: The number comes from the shared counter, not from the name, so the two tickets for Mia
get different numbers. The numbers start at 1 again because resetCounter() ran before this case.
Example 3:
Input:
(no tickets)
Output:
0
Explanation: resetCounter() set the counter to 0 and no ticket was created, so issued() returns 0.
Constraints:
- A test case creates from 0 to 1000 tickets.
- A name is one word of 1 to 20 English letters. Two tickets can have the same name.
- The tickets are printed only after all of them were created, so every ticket must store its own number: reading
ticketCountwhen printing would give every ticket the last number.
Free plan: 30 tutor messages. PRO is unlimited.
