Exercise: The Shipping Rate Calculator
Take the operations manager's ticket: a fourth carrier, NightOwl, has written its own rules against the tracker's carrier promise. Change a working rate desk, whose carriers are tangled into one switch, so any carrier it's handed can be quoted, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
A new carrier is signing on, and its team has done half the work for you. NightOwl's integration team wrote their own carrier against the promise in Lakeside Parcel's code, and their code calls two things the rate desk hasn't finished. The rate desk works today; your job is to change it so any carrier can plug in, without editing the rules of the three carriers that already work.
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-shipping-rates, 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-shipping-rates.git
Git makes a folder, exercise-dp-shipping-rates, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the rate desk's code, as it is today: the carrier promise and the quote screen (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change, with NightOwl's carrier in them, as their team wrote it;
- a demo program: the quote board for zone 3, with NightOwl beside the others.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the operations manager, Lakeside Parcel
A fourth carrier, NightOwl, signs on next month: $9 plus $1 a zone, zones 1 to 3 only. Their team has written NightOwl as a carrier of their own, against our carrier promise (a name,
canDeliver(zone)andrate(zone)in cents), and their code calls two things we have to finish:
quoteFor(carrier, zone)must quote any carrier it's handed, by that carrier's own rules:NightOwl: $11.00for zone 2, andNightOwl: doesn't deliver to zone 4.standardCarriers()must hand back the three carriers we offer today, QuickShip, EcoFreight and SameDay, in that order, so the quote board can list NightOwl beside them.Nobody should have to edit QuickShip's, EcoFreight's or SameDay's rules to add NightOwl, or the next carrier after it. The counter's quote by name,
quote("QuickShip", 2), has to keep working exactly as it does today.
quoteFor and standardCarriers are already there, because NightOwl's code calls them, but they don't do anything yet.
| Carrier | Rate | Zones |
|---|---|---|
| QuickShip | $5 plus $2 a zone | 1 to 4 |
| EcoFreight | $7, plus a 10% fuel charge ($7.70) | 1 to 4 |
| SameDay | $15 | 1 and 2 only |
| NightOwl (the ticket) | $9 plus $1 a zone | 1 to 3 only |
What to do
- Run the tests first. Four pass: today's rates. Three fail: they're the ticket.
- Read the code and find the smell. Where do QuickShip's numbers live? What would the quote screen have to know about NightOwl to quote it?
- 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 seven are green and the demo prints what When you're done shows.
The README's Stuck? section names the pattern, 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 ShippingRates |
| 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: NightOwl to zone 2 is $9 + $2 = $11.00.
Hints
- Look at how NightOwl's team wrote their carrier. It's in the tests. What if QuickShip, EcoFreight and SameDay were written the same way?
- Each carrier's numbers can travel with it. QuickShip's $5 and $2 don't have to live in the quote screen.
- Start with
quoteFor. Once it can quote NightOwl, see whetherquoteby name can find a carrier and hand it toquoteFor. - The order matters on the quote board: QuickShip, EcoFreight, SameDay.
When you're done
All 7 tests are green, and the demo prints the same five lines in every language:
By name: QuickShip: $9.00
QuickShip: $11.00
EcoFreight: $7.70
SameDay: doesn't deliver to zone 3
NightOwl: $12.00
NightOwl was written outside the tracker's code, and nothing in the rate desk mentions it. EcoFreight's fuel charge going to 12% next month is one number in one place.
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-shipping-rates), 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-shipping-rates.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-shipping-rates, 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
Another team added a carrier to your app without asking you to edit a single rate. That's the payoff of keeping each rule with its own numbers behind one promise: the next carrier is someone else's class, not your afternoon.