Park and unpark, step by step
After this: Follow a park and an unpark through the objects with sequence diagrams, and predict the lot after a run of events.
On this page · 6 sections · 3 checks
The program in lesson 4 printed thirteen lines. Behind each line, several objects called each other: the lot asked a level, the level asked its spots, the lot asked the fee rule. Is that the right flow? To check a design, follow one action through the objects, one call at a time.
Reading a sequence diagram
A draws one action as calls between objects. How to read it:
- The objects stand in a row across the top. Each has a dashed line going down, called its .
- Time runs down the page. The first call is at the top.
- A solid arrow is a call. A dashed arrow coming back is the value that the call returns.
- An arrow that loops back to the same lifeline is an object calling its own method, or making something new.
The driver stands on the left. The driver is outside the system (lesson 2), but drawing them shows where each action starts. The labels follow the language you have chosen.
Car C1 parks at minute 20
Three bikes are already parked: B1 on S1, B2 on S2, B3 on M1. Spot M2 is free. Press Run it to see the calls one at a time.
Press Run it to send the messages one at a time.
- messages
- 0
- The driver calls
parkon the lot with the car and the minute. - The lot asks level 1 for a spot. Level 1 asks its spots whether C1 fits. S1, S2 and M1 already hold the three bikes, so only M2 answers true.
- Level 1 returns M2 to the lot.
- The lot puts C1 on M2 and makes ticket 4 for C1, M2 and minute 20 (in Python, Ticket(4, C1, M2, 20), without the word new)(new Ticket(4, C1, M2, 20)).
- The lot returns ticket 4 to the driver.
Each object does its own job. The lot does not search the spots itself; it asks the level to find one. The level never makes tickets.
Ticket 4 leaves at minute 170
Press Run it to send the messages one at a time.
- messages
- 0
- The driver hands ticket number 4 to the lot at minute 170.
- The lot asks the fee rule for the fee. The car stayed 170 − 20 = 150 minutes, which is 3 started hours, and a car pays 20 per hour: 60.
- The lot frees spot M2 and returns 60 to the driver.
The lot does not know how fees are worked out. That is the aggregation line from lesson 3 at work: hand the lot a different fee rule, and these arrows stay the same.
The whole run
Here are the events of lesson 4's run in order, with what happens at each one except the three that the checks ask about. The checks below ask you to work out the rest before you compare with lesson 4's output.
| Minute | Event | What happens |
|---|---|---|
| 0 | bike B1 arrives | It takes S1, the first small spot: ticket 1. |
| 5 | bike B2 arrives | S2: ticket 2. |
| 10 | bike B3 arrives | No small spot is free, so it takes M1, the smallest spot that fits: ticket 3. |
| 20 | car C1 arrives | M2, as in the diagram: ticket 4. |
| 30, 40 | cars C2 and C3 arrive | Check 1. |
| 45 | B1 leaves | It stayed 45 minutes: 1 started hour, 10 rupees. |
| 50 | truck T1 arrives | Check 2. |
| 101 | C3 leaves | It stayed 61 minutes: 2 started hours, 40 rupees. |
| 110 | T1 tries again | Check 3. |
| 170 | C1 leaves | 60 rupees, as in the diagram. |
The three fees add up to 10 + 40 + 60 = 110, which is the collected 110 line.
Check yourself
The lot starts empty. B1, B2 and B3 (bikes) park at minutes 0, 5 and 10; C1, C2 and C3 (cars) at 20, 30 and 40. Which vehicle is on which spot after these six parks?
The six vehicles above have parked. At minute 45 bike B1 leaves, and at minute 50 truck T1 arrives. What is the last line this prints?
lot = ParkingLot( [ Level(1, [Spot("S1", Size.SMALL), Spot("S2", Size.SMALL), Spot("M1", Size.MEDIUM), Spot("M2", Size.MEDIUM)]), Level(2, [Spot("M3", Size.MEDIUM), Spot("L1", Size.LARGE)]), ], FeeCalculator(),)
b1 = lot.park(Bike("B1"), 0)lot.park(Bike("B2"), 5)lot.park(Bike("B3"), 10)c1 = lot.park(Car("C1"), 20)lot.park(Car("C2"), 30)c3 = lot.park(Car("C3"), 40)total = lot.unpark(b1.ticket_id, 45)lot.park(Truck("T1"), 50)ParkingLot lot = new ParkingLot( List.of( new Level(1, List.of(new Spot("S1", Size.SMALL), new Spot("S2", Size.SMALL), new Spot("M1", Size.MEDIUM), new Spot("M2", Size.MEDIUM))), new Level(2, List.of(new Spot("M3", Size.MEDIUM), new Spot("L1", Size.LARGE)))), new FeeCalculator());
Ticket b1 = lot.park(new Bike("B1"), 0);lot.park(new Bike("B2"), 5);lot.park(new Bike("B3"), 10);Ticket c1 = lot.park(new Car("C1"), 20);lot.park(new Car("C2"), 30);Ticket c3 = lot.park(new Car("C3"), 40);int total = lot.unpark(b1.id, 45);lot.park(new Truck("T1"), 50);The run goes on. At minute 101 C3 leaves L1. At minute 110 T1 tries again. At minute 170 C1 leaves M2. What does the last line print: how many small, medium and large spots are free?
What this design does not handle
- Read a sequence diagram: objects across the top, lifelines down, calls as solid arrows and returns as dashed ones.
- Follow a park call: the lot asks a level, the level asks its spots, the lot assigns the spot and makes a ticket.
- Follow an unpark call: the lot asks the fee rule for the fee and frees the spot, without working out the fee itself.
- Predict which spot each vehicle gets in a run of events, the fee each one pays, and how many spots of each size are free at the end.
- Name what the design leaves out: two gates assigning the same spot at the same moment.