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
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
| Requirement | Becomes | Test |
|---|---|---|
| Several levels, three sizes of space | Spot(id, level, size) | Build a lot from a list |
| Motorcycle, car, bus, each with a smallest size | VehicleType.min_size | A bus never gets a medium space |
| Park: find a fitting space and take it | enter(vehicle) returns a ticket | The smallest free size is used |
| Full is an answer | enter returns nothing | Second bus: nothing, and no number is used up |
| Leave: price, charge, free the space | leave(ticket_id) returns a receipt | The space can be taken again |
| Know what is free, per size | free() | Counts after each step |
| Different price models | Pricing | Swap the rule, same lot |
| Follow the state of spaces and tickets | Queue and map membership | A used ticket is refused |
Non-functional
How well it must do it
| Quality | Target | How |
|---|---|---|
| Safe with many threads | No space and no ticket used twice | Find and take in one step, pop before pay |
| Fast | O(1) claim, release and lookup | Queues by size, maps by id and plate |
| Grows | More levels or spaces without new code | The layout is data |
| Extends | A new vehicle is one enum line, a new price one class | Data before subclasses, one interface |
| Testable | Any minute of any day | The clock and the card machine are passed in |
Ask
Seven questions, and a default if nobody answers
| Question | Default 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
| Sentence | Somebody must know | Somebody must do |
|---|---|---|
| "A ticket can be used once" | Which tickets are active | Take it out when it is used |
| "The same car cannot be inside twice" | Plates inside | Check and add in one step |
| "The smallest fitting space" | Free spaces by size | Search upwards and take |
| "Every started hour counts" | Entry time, rates | Round up |
| "You cannot leave without paying" | Nothing new | Charge, then release |
Which requirement forces which part