Exercise: The Coupon Rules
Take a ticket from Trailhead Outfitters' marketing team: they want to build their own coupon rules on an admin screen and read each rule in words. Change the working coupon code so any rule built from the screen's pieces can be checked and described, with the tests green throughout.
Freelancer · C# · Java · Python · JavaScript · TypeScript · Updated
Trailhead Outfitters' marketing team is tired of waiting for a developer every time they want a new coupon. The admin screen's team has already built the screen; what's missing is the code that makes its rules work. The coupon code that's there works for this week's three coupons; your job is to change it so rules nobody has written yet work too, 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-coupon-rules, 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-coupon-rules.git
Git makes a folder, exercise-dp-coupon-rules, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the store's code, as it is today: the products, an order,
checkwith this week's three coupons, and the five pieces the admin screen builds rules from (the repository'sREADME.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that checks three customers' orders and one rule marketing built on the screen.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the marketing team, Trailhead Outfitters
- We want to build our own coupon rules. The admin screen snaps a rule together from pieces: "total over" an amount, "has a" kind of product (
shoe,jacket,bottle,socks), and "and", "or" and "not" to join them, in any shape we like. It builds each rule fromTotalOver,Has,And,OrandNot, and callsqualifies(rule, order). It has to work for rules nobody has written yet.- The screen shows every rule in words, so we can read what we built:
describe(rule)should give text such astotal over $50 and (has a shoe or has a jacket), with brackets around an "and" or an "or" inside another piece.This week's three coupons, TRAIL10, BOTTLE and BOOTS20, are checked at checkout with
check(code, order), and they have to keep working exactly as they do.
qualifies, describe and the five pieces are already there, because the screen's code calls them, but the pieces only hold what marketing picked, and the two functions don't do anything yet.
What to do
- Run the tests first. Four pass: they're what the store does today. Four fail: they're the ticket.
- Read the code and find the smells. What does
checkhave to know about each coupon? What would it take to add a fourth, or a rule with an "or" inside a "not"? - 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 CouponRules |
| 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: "has a bottle and total over $20" applies to Leo's $27 order with a water bottle.
Hints
- Start with one piece. Make
Hasanswer "does this order have one?" by itself, thenTotalOver. Then the joins: what does an "and" need to ask? - Once the pieces can answer, the three coupons can be pieces too. Build TRAIL10 from pieces, and see whether
checkstill needs itsifs. - Describing is a second job for every piece. Decide where it lives: in each piece, or in one place that knows every kind of piece.
- Brackets: only an "and" or an "or" inside another piece gets them. A "has a" or a "total over" never does.
When you're done
All 8 tests are green, and the demo prints the same five lines in every language:
Maya: TRAIL10, BOOTS20
Sam: TRAIL10, BOTTLE
Leo: none
New rule: has a bottle and total over $20
Leo qualifies: yes
The new rule was built outside the store's code, the way the admin screen builds them, and nothing in check mentions it. Next week's coupon is a new rule, not a new if.
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-coupon-rules), 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-coupon-rules.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-coupon-rules, 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
The marketing team can now write next week's coupon themselves, and the screen can read it back to them. That's what a little language buys: the people who own the rules get to change them, and the code that runs them stays the same.