Problem overview
A ticket machine gives every new ticket the next number: 1, then 2, and so on. The counter belongs to the class Ticket, not to any one ticket, so there is one copy shared by all of them. Each ticket keeps its own name and its own number.
The class has three parts to write:
new Ticket(name)adds 1 to the shared counter and stores the new value as the ticket'snumber.- The static method
Ticket.issued()returns how many tickets were created since the last reset. - The static method
Ticket.resetCounter()sets the counter back to 0, so the next ticket is number 1 again.
After a reset, tickets for Ann, Bob and Cy get the numbers 1, 2 and 3, and issued() returns 3. Two tickets for the same name still get different numbers, because the number comes from the counter, not from the name.
Approach
Most fields belong to an object: every Ticket has its own name. A static member belongs to the class itself. There is exactly one Ticket.count, no matter how many tickets exist, even none.
The constructor does three things in order: store the name, increase the shared counter, and copy the counter's value into this.number. The copy matters. The counter keeps moving as more tickets are made, but a number is a plain value, so once it is copied into the ticket it stays there. Every operation takes O(1) time.
Step by step
- Declare the counter. At the top of the class, write
static count = 0;. - Store the name. In the constructor, assign the parameter to
this.name. - Count the ticket. Increase
Ticket.countby 1. A static field is always reached through the class name, even inside the class:this.countinside the constructor looks on the new ticket, not on the class. - Keep the number. Set
this.numbertoTicket.count. The first ticket after a reset finds the counter at 1, so numbers start at 1. issued()is a static method that returnsTicket.count. Code calls it on the class, with no ticket at all, so it can only use static members.resetCounter()assigns 0 toTicket.count.
The solution in JavaScript
class Ticket {
static count = 0;
constructor(name) {
this.name = name;
Ticket.count++;
this.number = Ticket.count;
}
static issued() {
return Ticket.count;
}
static resetCounter() {
Ticket.count = 0;
}
}
Using the class
Ticket.resetCounter();
const ts = ["Ann", "Bob", "Cy"].map(n => new Ticket(n));
for (const t of ts) console.log(t.name + " #" + t.number);
console.log(Ticket.issued());
Ticket.resetCounter();
const m = [new Ticket("Mia"), new Ticket("Mia")];
for (const t of m) console.log(t.name + " #" + t.number);
console.log(Ticket.issued());
Ticket.resetCounter();
console.log(Ticket.issued());
This prints:
Ann #1
Bob #2
Cy #3
3
Mia #1
Mia #2
2
0
The tickets for Ann, Bob and Cy still show 1, 2 and 3 after all of them exist. Each ticket holds its own copy of the number.
Static members and instances
A static member lives on the class and is not found on the objects. With the tickets above, ts[0].count is undefined, and so is ts[0].issued. The class name is the way in: Ticket.count and Ticket.issued().
Common mistakes
- Reading the counter later instead of copying it. If a ticket looked up
Ticket.countwhenever its number is needed, for example in a getter, every ticket would show the last number once all of them were created. With three tickets, a getter that reads the counter returns 3 for all of them. Copying the value in the constructor avoids this. - Using
this.count. Inside the constructor,thisis the new ticket.this.count++would create a separate field on that ticket instead of changing the shared counter. - Increasing the counter after copying it. If the number is copied first and the counter is increased second, the first ticket gets 0.
- Forgetting
resetCounter(). The counter is shared by everything in the program, so without a reset a second group of tickets continues from where the first one stopped.
Testing
With no tickets created after a reset, Ticket.issued() returns 0. With one ticket created after a reset, its number is 1. Creating a ticket never changes the numbers of the tickets that already exist.
