Exercise: The Library Checkout
Take a ticket from Riverside Community Library: the board wants every late-fee rule tested, and the test team's tests hand the checkout service a clock they can set and loans kept in memory. Change the working service so it uses what it's handed, opens no database in tests, and still runs the real library, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
The library's board has approved its late-fee rules, and nobody is allowed to start charging them until every rule has a test. The test team has already written their tests, and a clock and a set of loans to hand in. The checkout service that's there works; your job is to change it so it takes what it's handed, without breaking the library that runs on it today.
Before you start
You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-library-checkout, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:
git clone https://github.com/ask-codey/exercise-dp-library-checkout.git
Git makes a folder, exercise-dp-library-checkout, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the library's code, as it is today: a loan, the checkout service, a stand-in for the library's database, and
App, where the real app gets its checkout (the repository'sREADME.mdnames the files for each language); - the test team's clock and loans:
FixedClock, a clock they can set to any day, andInMemoryLoans, loans kept in a list; - the tests, which you don't need to change;
- a demo program that rehearses a month at the library with the test team's clock.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the head librarian, Riverside Community Library
- The test team's tests hand the checkout service its loans and its clock, with
new CheckoutService(loans, clock). The service has to use what it's handed: a loan is due 14 days after that clock's day, a late fee counts the days on that clock, and loans are kept in the repository it was given.- Tests must never open a connection to the library's database. It's the real one, with members' real loans in it.
- The library itself keeps working. The website, the kiosk and the nightly jobs get their checkout from
App.CreateCheckout(), which must keep loans in the library database and use the real date.Everything that works today has to keep working.
The service's second constructor, CheckoutService(loans, clock), is already there, because the test team's code calls it, but it doesn't use what it's handed yet.
What to do
- Run the tests first. Four pass: they're what the library does today. Four fail: they're the ticket.
- Read the code and find the smells. What does the service build for itself? What does it reach for? Which of its lines are about the library's rules, and which are about tables, rows and columns?
- Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
- Stop when all eight are green and the demo prints what When you're done shows.
The README's Stuck? section names the patterns, if you want them. Try a few minutes without it first: working out which fix fits is the skill.
Check your work
Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).
| Language | Run the tests | Run the demo |
|---|---|---|
| C# | dotnet test |
dotnet run --project LibraryCheckout |
| Java | ./mvnw test (.\mvnw test in PowerShell) |
java src/main/java/Demo.java |
| Python | python -m pytest |
python demo.py |
| JavaScript | npm test |
node demo.js |
| TypeScript | npm test |
node demo.ts |
A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: borrowed March 2, due March 16, returned March 22: 6 days late is $1.50 (150 cents).
Hints
- Start with the clock. Keep the clock the constructor is handed, and use it wherever the service asks what day it is. Three of the four failing tests turn green.
- Then the loans. Every line about tables, rows and columns could live in one class that keeps the loan repository's promise with the library database. Then the service only says "add", "find", "remove" and "count".
- Don't forget the real library. Once the service stops building its own database, something has to build the real parts for the app. There's already one place the app gets its checkout from.
- One step at a time. Run the tests after every change; a red "Keep this working" test means the last step went too far.
When you're done
All 8 tests are green, and the demo prints the same four lines in every language:
Loan 1: Maya, The Lighthouse Keeper, due 2026-03-16
Loan 2: Sam, Rivers of Glass, due 2026-03-16
Maya returns it on 2026-03-22: $1.50
Sam returns it on 2026-04-25: $5.00
Those dates are the test team's, not today's, and the demo never touched the library's database. That's the test of the fix: every rule can be checked on any day, in a few milliseconds, and the real library still runs on the real database and the real date.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-dp-library-checkout), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-dp-library-checkout.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-dp-library-checkout, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
You took a class that built its own world and taught it to accept the one it's handed. That's the change every framework in this pass expects of your code. Next, the library goes on the web, and you'll find the patterns the framework itself is made of.