AlgoViz

Design a parking lot

medium 90 min

Model 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 entryWho decidesCan it fail?
Notice a car, read the plateGateYes: dirty plate, ask for a button press
Ask for a spaceLotFull: show it, stay closed
Print the ticketGatePaper out: show a code instead
OpenGateMotor stuck: call help, keep the ticket valid
Close after the car has passedGateSensor says still there: wait
Java
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        }    }}
A gate with hardware (the constructor is left out). Note what is under the gate's lock (one lane) and what is not (the lot has its own).

Order

Three mistakes in gates

  1. 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. Call enter and look at the answer.

  2. 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. 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?

synchronizedReentrantLock
Give up waitingNotryLock(2, SECONDS)
First come, first servedNot promisedFair mode
Several waiting roomsOneSeveral conditions
Can be forgottenNo, the block ends itYes: always finally
Is it held?Cannot askisLocked(), for monitoring only
The trap in fair mode: the plain 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.