Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
Entry and exit gates
Hardware, a lock per gate, and its limits
The point: a gate becomes a class when it owns something: a barrier, a printer, a lane with one car in it. Its lock protects that, and never the spaces.
Class
What a gate really does
| Step at the entry | Who decides | Can it fail? |
|---|---|---|
| Notice a car, read the plate | Gate | Yes: dirty plate, ask for a button press |
| Ask for a space | Lot | Full: show it, stay closed |
| Print the ticket | Gate | Paper out: show a code instead |
| Open | Gate | Motor stuck: call help, keep the ticket valid |
| Close after the car has passed | Gate | Sensor says still there: wait |
final class EntryGate { private final ParkingLot lot; private final Barrier barrier; private final Sign sign; private final Printer printer; private final ReentrantLock lane = new ReentrantLock(true); // fair: first come, first served Optional<Ticket> arrive(Vehicle vehicle) { lane.lock(); try { Optional<Ticket> ticket = lot.enter(vehicle); // no isFull() before it if (ticket.isEmpty()) { sign.show("FULL FOR " + vehicle.type()); return ticket; } printer.print(ticket.get()); sign.show("WELCOME, SPACE " + ticket.get().spot().id()); barrier.letOneCarPass(); return ticket; } finally { lane.unlock(); // also when the printer throws } }}Order
Three mistakes in gates
- 1
Asking isFull first
if (lot.isFull()) ... else lot.enter(v)is check then act, and it is also wrong: a lot can be full for cars and free for motorcycles. Callenterand look at the answer. - 2
Marking paid before the money came
A method that calculates, marks the ticket as paid and only then asks for the card. If the card is declined the ticket is already paid. Charge, then mark, then release, then open.
- 3
Believing that the gate lock protects the lot
Each gate has its own lock. Two gates have two locks, so both can be inside at the same time. What they share must be protected where it is shared.
Exit: the only safe order
Locks
synchronized or ReentrantLock?
synchronized | ReentrantLock | |
|---|---|---|
| Give up waiting | No | tryLock(2, SECONDS) |
| First come, first served | Not promised | Fair mode |
| Several waiting rooms | One | Several conditions |
| Can be forgotten | No, the block ends it | Yes: always finally |
| Is it held? | Cannot ask | isLocked(), for monitoring only |
tryLock() without a time jumps the queue, as its documentation says. Use the version with a time if the order matters.Slow
What if the card system takes ten seconds?
The payment provider is slow. What happens at the exit?
BreaksWait under the lot's lock
- How
- Charge inside the critical section.
- Limit
- Every entry and every exit waits for one card.
- Choose when
- Never.
WorksWait at this gate only, with a limit
- How
- Charge outside the lot's lock, timeout 8 seconds, same key on retry.
- Limit
- This lane is blocked. Others work.
- Choose when
- Card reader at the barrier.
StrongPay before you drive
- How
- Machines at the doors. The barrier only checks
paid_until, in milliseconds. - Limit
- A second state on the ticket, and people who overstay the 15 minutes.
- Choose when
- Any lot with more than one busy exit. It removes the wait instead of hiding it.