Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Write the requirements (20 minutes)
Close the page and write seven numbered requirements for a parking lot, plus three questions you would ask.
Done when: seven numbered rules and three questions exist, and two things are marked out of scope.
Hint
The ask-list in section 1 is a model, but write your own.
Easy 2: Run the lot (20 minutes)
Type the code into one file with a main that parks bike, car and truck, then exits the car after 130 minutes.
Done when: it prints the three spots and a fee of 120 for the car.
Hint
Use the fake clock from Level 5, module 5.
Easy 3: Fee by hand (20 minutes)
Work out by hand the hourly fee for a car staying 59, 60, 61 and 0 minutes at 40 per hour.
Done when: four answers are written (40, 40, 80 and 40) with the rule used.
Hint
Divide minutes by 60, round up, and use at least one.
Medium
Medium 1: First 30 minutes free (45 minutes)
Write FreeFirstHalfHour as a FeeStrategy and test it at 20, 31 and 90 minutes.
Done when: three tests pass and the lot class is not edited.
Hint
Subtract 30 minutes from the stay, then use the hourly rule.
Medium 2: Weekend decorator (45 minutes)
Wrap HourlyFee in a decorator that doubles the fee when the entry day is Saturday or Sunday. Use the injected clock.
Done when: a Friday stay and a Saturday stay of the same length differ by exactly double.
Hint
LocalDate.atZone(ZoneOffset.UTC).getDayOfWeek().
Medium 3: Concurrency test (45 minutes)
Run 50 threads parking different cars into 10 spots of the right size. Count tickets.
Done when: exactly 10 tickets are issued, 40 calls get LotFullException, and no spot holds two cars.
Hint
A start latch makes all threads begin together.
Hard
Hard 1: Free lists (90 minutes)
Replace the full scan with a free list per spot type. Keep all tests passing and explain the new cost of park.
Done when: the old tests pass unchanged, and a note gives the cost before and after.
Hint
Map<SpotType, Deque<Spot>>.
Hard 2: Display board (90 minutes)
Add an observer so that a Display shows the free count per floor and type after every park and exit. Do not let the lot know about the display class.
Done when: the board prints correct counts after a series of calls, and the lot has no reference to Display.
Hint
The lot holds a list of listeners and calls them after the change.
Hard 3: Reservations (90 minutes)
Add reserve(vehicleType, from, to) so a spot is held for a time window. Decide which classes change, and what a vehicle without a reservation sees during a held window.
Done when: a written list names every changed class, and tests cover an overlapping reservation being refused.
Hint
A reservation is a value with a slot, like the slot of Level 1.