Exercise: The Weather Station
Take Maya's next ticket: SkyCast now bills every call, so the hub must ask it once and share the answer, and guests must stop changing the thermostat. Add both without touching SkyCast's client or the thermostats, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
SkyCast sent its first bill, and Maya's sister has found the thermostat. Both problems are about what stands between the hub's screens and the things they call. The code works today; your job is to put the right thing in between, without changing SkyCast's client or either thermostat.
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-weather-station, 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-weather-station.git
Git makes a folder, exercise-dp-weather-station, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the hub's code, as it is today: SkyCast's client, the phone app's weather card, the hallway thermostat, and the two setup functions everything gets its weather and its thermostat from (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that runs one morning: three asks for the weather, then Sam and a guest at the thermostat.
SkyCast's client here doesn't go across the internet: it counts its calls instead, so the tests can check how often the hub asked.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: Maya
- SkyCast bills every call now. The wall panel, the phone app's weather card and the morning routine each ask for today's weather, and each ask reaches SkyCast (only the card remembers its own answer). The hub should ask SkyCast once and share that answer with everything that asks. The setup code builds the hub's weather with
weatherFor(source). Next month a backup weather service goes in for the days SkyCast is down, and it needs the same treatment with no extra work.- Guests are changing the thermostat. The wall panel and voice get their thermostat from
thermostatFor(person, thermostat), and today anyone can set it to anything. A guest may still read it, but only Maya and Sam may change it: on the hallway thermostat, and on the bedroom one coming next week.Don't change SkyCast's client or the thermostats themselves. Everything that works today has to keep working.
What to do
- Run the tests first. Four pass: they're what the hub does today. Four fail: they're the ticket.
- Read the code and find the smells. Where does the weather get remembered today, and who can use that? Who checks whether a guest may change the thermostat?
- 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 WeatherStation |
| 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 asks for today's weather reach SkyCast once.
Hints
- Both fixes go in the setup functions. Everything already gets its weather from
weatherForand its thermostat fromthermostatFor, so what those hand back is what everyone gets. - The tests hand in sources and thermostats the hub has never seen (a backup weather service, a bedroom thermostat). Whatever you hand back has to work in front of any of them.
- Keep the promise. The wall panel and voice must not be able to tell what you hand back from the real thing: same methods, same answers, unless the answer is "no".
- The card's own cache won't be needed once the hub's weather remembers. Taking it out is a good last step, with the tests green before and after.
When you're done
All 8 tests are green, and the demo prints the same seven lines in every language:
Wall panel: 72F, sunny
Phone app: Weather: 72F, sunny
Morning routine: 72F, sunny
SkyCast calls: 1
Sam sets 68: thermostat at 68
Guests can't change the thermostat
A guest tries 80: thermostat at 68
Three parts of the hub asked for the weather, and SkyCast heard once. The guest was told no by the thermostat the wall panel was handed, and neither SkyCast's client nor the thermostat changed.
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-weather-station), 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-weather-station.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-weather-station, 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
One wrapper added a feature, and another decided who gets in, and neither touched the thing it wrapped. When the backup weather service and the bedroom thermostat arrive, they get both for free.