Exercise: The Bike Rental Desk

Take a ticket from Pedal & Go's manager: scooters this week, a partner shop's kayaks next month, and weekend discounts that marketing keeps changing. Change the working rental desk so new kinds of rental and new deals arrive from outside, with the tests green throughout.

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

By

This chapter handed you the building blocks every pattern is made of: promises, one call with many behaviors, parts, delegation and functions as values. Here they meet a ticket. Pedal & Go's desk works today; the manager's three requests each need it to price or discount something it has never heard of. Your job is to change it so they fit, without breaking anything it already does.

Before you start

You need one language set up, as in Setup: VS Code and Your Language, and the workflow from Exercise: Green Before, Green After: green before, one small step at a time, green after.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-bike-rental, 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-bike-rental.git

Git makes a folder, exercise-dp-bike-rental, with the exercise code inside.

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

  • the desk's code, as it is today: the vehicles, a rental, and the desk that prices rentals and adds up the day (the repository's README.md names the files for each language);
  • the tests, which you don't need to change;
  • a demo program that prints a Saturday at the desk, including a kayak written outside the desk's code.

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

The ticket

From: the manager, Pedal & Go

  1. Scooters start this week, at $8 an hour. The stock team has already added a Scooter class, but the desk prices every scooter rental at $0.
  2. A partner shop will rent its kayaks through our desk from next month. Their developers write their own classes, outside our code, and every one of them keeps our Rentable promise: a name, and a price for some hours. The desk has to price whatever they send, by its own price, without anyone editing the desk for each new thing they rent.
  3. Weekend discounts. The weekend team already calls dayTotal(rentals, discount), handing in a function that takes the day's total and gives back the discounted total: 20% off this summer. Marketing will invent other deals, so the desk mustn't know any deal's rule.

Everything that works today has to keep working.

The Rentable promise and the second dayTotal are already there, because the other teams' code uses them, but the desk doesn't use either yet.

What to do

  1. Run the tests first. Four pass: they're what the desk does today. Four fail: they're the ticket.
  2. Read the code and find the trouble. How does the desk work out a rental's price? What would it have to know to price a kayak that another team wrote? Where does the discount that's handed in go?
  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 eight are green and the demo prints what When you're done shows.

The README's Stuck? section names the building blocks, if you want them. Try a few minutes without it first: working out which one fits is the skill.

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 RentalDesk
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 scooter for 5 hours costs $40, at $8 an hour.

Hints

  • The desk can't know the partner's classes. They don't exist yet when you write the desk. So the desk can't check for them; something else has to know a kayak's price.
  • The Rentable promise already says what every rentable thing can do. Who should keep it?
  • The discount is a function you're handed. You don't have to write any discount yourself.
  • One step at a time. Give one vehicle its own price first and run the tests (still four green). Then the others. Then change how the desk asks. Then the discount.

When you're done

All 8 tests are green, and the demo prints the same six lines in every language:

Bike for 2 hours: $20
E-bike for 3 hours: $45
Scooter for 5 hours: $40
Kayak for 5 hours: $60
Weekday total: $165
Weekend total: $132

The kayak on that list was written outside the desk's code, and nothing in the desk mentions it. That's the test of the fix: next month's rental and next week's deal need no edit to anything you wrote today.

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-bike-rental), 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-bike-rental.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-bike-rental, 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

You used a promise, one call with many behaviors and a function passed in to let new things arrive from outside, which is what most design patterns are for. Next, how to draw a design like this one on paper, before you write it.

Key terms

Related