Exercise: The Food Truck Menu
Take Rosa's ticket: she wants to set the food truck's specials herself from the tablet, starting with taco Tuesday. Change the working menu code so a new special is a new piece of data, not an edit to the price code, with the tests green throughout.
Apprentice · C# · Java · Python · JavaScript · TypeScript · Updated
Rosa, who owns the Taco Tuesday food truck, is tired of waiting for a developer every time she changes a special. The tablet team has already built a screen where she can save one. Your job is to make the truck's code accept what she saves, without breaking the prices, the menu board or the receipts that work 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-food-truck-menu, 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-food-truck-menu.git
Git makes a folder, exercise-dp-food-truck-menu, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the truck's code, as it is today: the menu, the prices, the menu board and the receipt, in one
FoodTruckclass (the repository'sREADME.mdnames the file for each language); - the tests, which you don't need to change;
- a demo program that runs the first Taco Tuesday the way the tablet will.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: Rosa, owner of the Taco Tuesday food truck
- I want to set the specials myself, from the tablet. The tablet team's code already calls
addSpecial(day, item, price)whenever I save one. Every special I save has to show on the menu board with "(special!)" beside it, and be charged at the window, without anyone editing the price code.- Taco Tuesday starts next week: tacos are $3 on Tuesdays. Some days I'll run two specials at once.
- Monday's burrito special ($7) stays, and so do the regular prices: taco $4, burrito $9, quesadilla $7, drink $2.
Everything that works today has to keep working.
addSpecial is already there, because the tablet's code calls it, but it doesn't do anything yet.
What to do
- Run the tests first. Four pass: they're what the truck does today. Four fail: they're the ticket.
- Read the code and find the smells. Where does the code know what the specials are? How many places would you edit to add one?
- 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 says which idea fits, if you want it. 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 FoodTruckMenu |
| 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: three $3 tacos and a drink come to $11 on Tuesday.
Hints
- Ask what changes, and how often. The regular prices change a few times a year. The specials change whenever Rosa likes. Which of the two is written into the code?
- A special is three facts that belong together: a day, an item and a price. Where could all three live, side by side?
- Monday's burrito doesn't have to be special code. What if it were the first special saved, when the truck starts?
- Count the places that ask "is this a special?" When you're done, they should all ask the same one.
When you're done
All 8 tests are green, and the demo prints the same six lines in every language:
Tuesday's menu:
taco: $3 (special!)
burrito: $9
quesadilla: $7
drink: $2
Receipt: 4 items: $11
Taco Tuesday came from the tablet, and the price code never mentions it. Next week's special will be one more addSpecial call, and nobody opens the code.
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-food-truck-menu), 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-food-truck-menu.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-food-truck-menu, 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 found the part of the app that changes most, gave it one home, and the next change became data instead of code. That's the move behind most of the patterns ahead, made here with nothing more than a small class.