Exercise: The Shipment Builder
Take the operations desk's next ticket: a new checkout screen makes shipments one step at a time, and pallets are now stacked three deep. Change the working shipment and weight code so both work, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
Lakeside Parcel's operations desk has a ticket, and the web team is waiting on half of it. Their new checkout screen makes a shipment one choice at a time, and a big customer has started stacking pallets three deep, which the tracker weighs wrong. The code that's there works for today's shipments; your job is to change it so both new things work, 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-shipment-builder, 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-shipment-builder.git
Git makes a folder, exercise-dp-shipment-builder, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the tracker's code, as it is today: boxes, pallets, the weight code, shipments and the builder the web team calls (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that makes two shipments the way the new screen will, and weighs a pallet three deep.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the operations desk, Lakeside Parcel
- A new checkout screen, from the web team. It makes a shipment one step at a time, as the customer picks each extra:
new ShipmentBuilder(43, "Sam", "Pine Rd").by("EcoFreight").withInsurance().withGiftWrap().holding(box).build(). Whatever the customer doesn't pick is left out: no extras they didn't ask for, and the carrier is QuickShip unless they choose another. The phone desk still makes shipments withnew Shipment(...)and all nine values, and that has to keep working.- Pallets on pallets on pallets. A customer has started stacking pallets three deep, and the tracker's weight comes out wrong, so the carriers' quotes are wrong too. The weight must count every box, however deep the pallets go.
Everything that works today has to keep working.
ShipmentBuilder is already there, because the web team's screen calls it, but it doesn't build anything yet.
What to do
- Run the tests first. Four pass: they're what the tracker does today. Four fail: they're the ticket.
- Read the code and find the smells. How many values does a shipment's constructor take, and can you tell them apart at the call? How many times does the weight code ask "box or pallet?", and how deep can it go?
- 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 Shipments |
| 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: a 2-pound box at the bottom of ten pallets still weighs 2.
Hints
- Start with the builder. It needs somewhere to keep each choice until
build, and a default for every choice nobody makes. - Each step hands back the builder itself, so the next step can be called on it.
buildcan call the constructor that's already there. The phone desk's code doesn't need to change.- For the weights, count the loops. One more loop passes three deep and fails at four. What if a pallet could be asked its weight the same way a box can?
When you're done
All 8 tests are green, and the demo prints the same three lines in every language:
Package 42 to Maya at Oak Ave by QuickShip, 22 lb: insurance, signature required, gift wrap ($9 extra)
Package 43 to Sam at Pine Rd by EcoFreight, 7 lb: insurance, gift wrap ($7 extra)
Pallet three deep: 15 lb
The web team's screen now names each choice it makes, and a "fragile" sticker next month is one new step on the builder. A pallet ten deep weighs the same way as a single box, and nothing that asks for a weight had to change.
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-shipment-builder), 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-shipment-builder.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-shipment-builder, 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
A shipment with many optional parts, and a shipment of things inside things, are two of the shapes real data takes. Name each step of making the first, and let the second answer for itself at every level, and both stop being the code everyone is afraid to touch.