Exercise: The Report Card Printer

Take the front office's ticket: some families want report cards by text message, the tests must stop going through the real email server, and one class is doing three people's jobs. Change the working report-card code so any sender can be handed in, with the tests green throughout.

Apprentice · C# · Java · Python · JavaScript · TypeScript · Updated

By

Northside High's front office has a ticket for you, and the parents' app team is waiting on it: they've built a text-message service, and their code already hands it to the report cards. Right now the report cards ignore it and email everyone. Your job is to change the code so the families who asked get a text, everyone else still gets email, and nothing the office relies on today breaks.

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-report-card, 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-report-card.git

Git makes a folder, exercise-dp-report-card, with the exercise code inside.

The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:

  • the school's code, as it is today: students and their scores, the school's email server, the parents' app team's text-message service, and the ReportCards class that does the rest (the repository's README.md names the files for each language);
  • the tests, which you don't need to change;
  • a demo program that sends this term's report cards: Maya's family by email, Sam's and Leo's by text.

In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.

The ticket

From: the front office, Northside High

  1. Some families want report cards by text message. The parents' app team has built a text-message service, TextMessages, and their code already creates report cards with it: new ReportCards(texts), then send(student) or sendAll(students). Report cards made that way must go by text, not email, and nothing should go through the school's email server.
  2. Families who didn't ask keep getting email. new ReportCards(), with nothing handed in, must still email every family through the school's server, the way it does today.
  3. The tests must stop going through the real email server. Any sender a test writes for itself (one that keeps the same promise as TextMessages) has to work too.

And one more thing, from all of us: the grading scale belongs to the district, the layout to this office, and sending to IT. Right now all three live in one class, and last term a layout change broke the grades.

The constructor that takes a sender is already there, because the parents' app team's code calls it, but it ignores the sender for now.

What to do

  1. Run the tests first. Four pass: they're what the app does today. Three fail: they're the ticket.
  2. Read the code and find the smells. What does ReportCards build for itself? Who would ask for each of its methods to change?
  3. Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
  4. Stop when all seven are green and the demo prints what When you're done shows.

The README's Stuck? section names the ideas that fit, 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 ReportCardPrinter
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: handed TextMessages, Sam's report card goes to his family by text.

Hints

  • Start with the sender. Where does ReportCards get the thing it sends with? What if it were given one instead?
  • The school's email server can keep the same promise that TextMessages keeps. Then the constructor with nothing handed in can hand itself the school's server.
  • Then the three jobs. Who asks for the grading scale to change? The layout? The sending? Each answer can be a small class of its own.
  • Keep letter and format where they are. Other code calls them; they can ask the new classes for the answer.

When you're done

All 7 tests are green, and the demo prints the same three lines in every language:

Email to Maya's family: Maya: Math A, Science B, English C
Text to Sam's family: Sam: Math C, Science D, English B
Text to Leo's family: Leo: Math F, Science A, English B

Maya's family gets the email they always have, and Sam's and Leo's families get texts. ReportCards never mentions text messages: it sends through whatever it's handed. The parents' app next spring, or a letter home, will be one more class that keeps the promise.

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.

  1. On GitHub, click +, then New repository. Name it (for example exercise-dp-report-card), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. 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-report-card.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-dp-report-card, 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.

What's next: the first pass

That's the end of Before You Build. You can read a class diagram, smell trouble in code that works, fence off what changes, ask for promises instead of classes, build from parts, and weigh five pieces of SOLID advice, including when to bend them.

Module 2 puts all of it to work on a real app. Pass 1 follows Sunrise Bakery's ordering app from its first plan to its first change requests: a new product every month, a new deal every week, a kitchen screen that wants to hear about every order, and one button for checkout. You'll plan it, build version 1, and meet the first design patterns the way teams found them, as the fixes that kept holding up.

So what

You handed a class the thing it used to build for itself, and split three people's jobs into three classes. The text messages, the tests' fake and next year's channel all plug in the same way, and that's SOLID doing the work it was written for.

Key terms

Related