Exercise: The Bakery Order Counter

Take a ticket from Sunrise Bakery's owner: pies launch Monday and a second shop opens with its own tax rate. Change the working order code so new products are added from outside and each shop charges its own tax, with the tests green throughout.

Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated

By

This is how work arrives at a real job: not as "use pattern X", but as a ticket from someone who runs the business. Sunrise Bakery's owner has two requests for next month. The code that's there works; your job is to change it so the new requests fit, 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-orders, 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-orders.git

Git makes a folder, exercise-dp-bakery-orders, 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, the shop's settings, the menu, taking an order, the price board and checkout (the repository's README.md names the files for each language);
  • the tests, which you don't need to change;
  • a demo program that runs a day at the bakery, including a pie 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

  1. Pies launch Monday, at $15. The menu team adds products from their own code by calling menu.add("pie", ...) with a function that makes a pie. From then on a pie has to be orderable on the counter tablet and show up on the price board. After pies we add something new every month, so this should never need the order code edited again.
  2. We're opening a second shop where the sales tax is 10% instead of 8%. Each shop's checkout is made with its own settings, new Checkout(settings), and the two shops must never charge each other's tax.

Everything that works today has to keep working.

menu.add and the Checkout constructor are already there, because the other teams' code calls them, but they don't do anything yet.

What to do

  1. Run the tests first. Four pass: they're what the bakery does today. Four fail: they're the ticket.
  2. Read the code and find the smells. Where is the decision "which product goes with this word?" made? How many places? Where does checkout get its tax rate from, and who else shares it?
  3. Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
  4. 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 BakeryOrders
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: the second shop's 10% tax makes $50 into $55.

Hints

  • Count the if chains. If the same decision is made in two places, a new product means two edits, and the menu team can't make either of them.
  • The menu is passed to takeOrder and the price board already. It's a good home for the decision.
  • Checkout is handed its settings. Look at what it uses instead.
  • One step at a time. Move the decision into one place first, and run the tests (still 4 green). Then let menu.add add to it. Then fix checkout.

When you're done

All 8 tests are green, and the demo prints the same four lines in every language:

Order: Cake, Cupcakes, Cookie box
Price board: Cake: $30, Cupcakes: $12, Cookie box: $8, Pie: $15
First shop's total: $54
Second shop's total: $55

The pie on that price board was written outside the bakery's code, and nothing in the order code mentions it. That's the test of the fix: next month's product needs no edit to anything you wrote today.

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.

  1. On GitHub, click +, then New repository. Name it (for example exercise-dp-bakery-orders), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. 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-orders.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-dp-bakery-orders, 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 ticket, found the smells behind it, and changed working code without breaking it. That's the job. Next, the owner's wish list: a new deal every week, a kitchen screen, and one button for checkout.

Key terms

Related