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

UML design

Draw it in the order you think it

The point: draw the coordinator first, then the owner of the scarce thing, then records, then the two edges of the system.

The design to reach: one coordinator, one owner of free spaces, plain records

ParkingLot- active: Map<id, Ticket>- inside: Set<plate>- lock: Lock+ enter(vehicle): Ticket?+ leave(id): Receipt+ free(): Map<Size, int>

1/6Start from the two verbs

The requirements have two sentences with a verb: a vehicle enters, a vehicle leaves. They become the two methods of one coordinator, ParkingLot. Everything a gate needs is there.

owns (dies together)usesimplementshas (lives on)

A question mark means that the answer may be nothing: a full lot is an ordinary result, not an error.

Read it

Arrows that people mix up

ArrowMeansHere
Filled diamondOwns. They live and die togetherParkingLot and SpotPool
Empty diamondHas. The part lives onSpotPool and Spot
Dashed line, empty triangleImplements an interfaceHourlyPricing and Pricing
Solid line, empty triangleExtends a classNowhere, and that is a choice
Plain arrowUses, knows aboutTicket and Spot

Compare

The same picture as a tree

This is the diagram most textbooks draw: a Singleton lot, levels or floors, entry and exit panels, a tree of vehicles, a tree of spaces (motorcycle, compact, large, accessible, electric), a ticket with a status, a payment tree (cash, card, UPI) and a sign. Know it, because an interviewer may draw it. Then say what each box would do in code.

The large version

Box in the large versionKeep?What we do
Entry and exit panelWhen they own hardwareCallers of enter and leave; see Entry and exit gates
Floor with a map of spacesWhen it owns a lockSee Many gates and floors
Display boardAs a listenerSee Display board
Space tree: compact, large, accessible, electricNosize and a set of features; see Extending it
Electric car with battery capacityNoA need: must have a charger
Payment tree: cash, card, UPIYes, behind one interfaceImplementations of Payments
Payment status: pending, completed, failed, refundedYes, outside the lotSee Payment and lost tickets
Ticket statusYes, for pay first and drive laterSee Payment and lost tickets

Size

How much to draw

How much detail belongs on the board?

BreaksEverything, with getters
How
Every field, every getter, both directions of each relation.
Limit
Fifteen minutes gone and no code. The important line drowns.
Choose when
Never in an interview.
WorksBoxes with names and arrows
How
Seven boxes, seven lines.
Limit
Looks like a concept map. The interviewer cannot see who holds what.
Choose when
The first two minutes.
StrongState and public verbs
How
For each class with changing state: its fields and its public methods. Records get one line.
Limit
You must decide names early.
Choose when
Here. The fields show who owns what, the methods show the conversation.