Exercise: The Bakery's Wish List
Take the owner's next ticket: the marketing team writes its own weekly deals and a delivery app wants to hear about every order. Change the working checkout and order desk so both plug in from outside, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
The owner is back with the next ticket, and this time two other teams are waiting on you. The marketing team has started writing its own deals, and the delivery service's app wants to hear about every order. The code that's there works; your job is to change it so their code can plug in from outside, without breaking anything that works 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-bakery-wish-list, 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-bakery-wish-list.git
Git makes a folder, exercise-dp-bakery-wish-list, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the bakery's code, as it is today: the products, checkout with this week's deals, the kitchen screen, the customer's texts and the order desk (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that runs a day at the bakery, with a deal and a delivery app written outside the bakery's code.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the owner, Sunrise Bakery
- New deals every week, written by the marketing team. They write each deal as a small function that says how much comes off an order, and hand it to checkout with
totalWith(order, deal). Checkout must work with any deal they write, without anyone editing checkout. The counter tablet still picks this week's deal by its name, and that has to keep working.- A delivery service starts next month. Its app calls
desk.subscribe(...)with a function to call for every new order, and a loyalty-points screen will do the same in the spring. The kitchen screen and the customer's text must still get every order, exactly once.Everything that works today has to keep working.
totalWith and subscribe are already there, because the other teams' code calls them, but they don't do anything yet.
What to do
- Run the tests first. Four pass: they're what the bakery does today. Four fail: they're the ticket.
- Read the code and find the smells. What does checkout have to know about each deal? Who does the order desk know by name?
- 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.
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 WishList |
| 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: $5 off orders over $60 makes the $78 order $73.
Hints
- Start with checkout. Make the version that's handed a deal work first; then see whether the by-name version can use it.
- The deals the tablet picks by name can be data: a name, and the function that goes with it. You've seen that shape before, with products.
- The order desk knows the kitchen and the texts by name. What if they were two listeners among many?
- Order matters for the listeners: the tests check that they hear an order in the order they subscribed.
When you're done
All 8 tests are green, and the demo prints the same five lines in every language:
This week (cupcakes-2-off): $74
Next week's deal ($5 off orders over $60): $73
Delivery app: 6 items to deliver
Kitchen screen: 6 items to bake
Text to customer: We got your $78 order
The $73 deal and the delivery app were both written outside the bakery's code, and nothing in checkout or the order desk mentions them. Next spring's loyalty-points screen will be one more subscribe call.
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-bakery-wish-list), 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-bakery-wish-list.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-bakery-wish-list, 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
Two other teams can now add to the bakery without asking you to edit checkout or the order desk. That's what a pattern buys when it fits: the next change is someone else's one line, not your afternoon.