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

Requirements

Sentences that become methods

The point: a requirement is useful when you can point at the method that fulfils it and the test that checks it.

Functional

What it must do

RequirementBecomesTest
Several levels, three sizes of spaceSpot(id, level, size)Build a lot from a list
Motorcycle, car, bus, each with a smallest sizeVehicleType.min_sizeA bus never gets a medium space
Park: find a fitting space and take itenter(vehicle) returns a ticketThe smallest free size is used
Full is an answerenter returns nothingSecond bus: nothing, and no number is used up
Leave: price, charge, free the spaceleave(ticket_id) returns a receiptThe space can be taken again
Know what is free, per sizefree()Counts after each step
Different price modelsPricingSwap the rule, same lot
Follow the state of spaces and ticketsQueue and map membershipA used ticket is refused

Non-functional

How well it must do it

QualityTargetHow
Safe with many threadsNo space and no ticket used twiceFind and take in one step, pop before pay
FastO(1) claim, release and lookupQueues by size, maps by id and plate
GrowsMore levels or spaces without new codeThe layout is data
ExtendsA new vehicle is one enum line, a new price one classData before subclasses, one interface
TestableAny minute of any dayThe clock and the card machine are passed in

Ask

Seven questions, and a default if nobody answers

QuestionDefault you may assume
How many levels and spaces?3 levels, about 300 spaces, read from configuration
Which vehicles?Motorcycle, car, bus
Payment and tickets?Yes: priced at exit, the card machine is an interface
Several entrances and exits?Yes, so it must be safe with threads
Is the time of the stay tracked?Yes, entry and exit as instants
Reservations?Later. Say how they would fit
Accessible spaces?Later, as a feature of a space, kept for those who need it

Derive

State and behaviour come from the sentences

SentenceSomebody must knowSomebody must do
"A ticket can be used once"Which tickets are activeTake it out when it is used
"The same car cannot be inside twice"Plates insideCheck and add in one step
"The smallest fitting space"Free spaces by sizeSearch upwards and take
"Every started hour counts"Entry time, ratesRound up
"You cannot leave without paying"Nothing newCharge, then release
Two columns: fields and methods. This table is the class diagram, only not drawn yet.

Which requirement forces which part