Exercise: The Snack Inventory
Finish a snack stand's inventory code in your language: a Snack class's value method, then find a snack by name, add up the stock's value, and list what's running low, using loops, lists, classes and references.
Apprentice · C# · Java · Python · JavaScript · TypeScript · Updated
The snack stand from this chapter needs its inventory code: what each snack's stock is worth, a way to find a snack by name, the value of everything on hand, and a list of what's running low before the weekend rush. It's every piece of The Code You Need in one small program: loops, a list, a class with a method, and a reference handed back from a search.
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-ds-snack-inventory, 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-ds-snack-inventory.git
Git makes a folder, exercise-ds-snack-inventory, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- your code: the
Snackclass and three functions to finish (the repository'sREADME.mdnames the files for each language); - the tests that check your code. You don't need to change them;
- a demo program that tries your code on the snack stand's four snacks.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
What to do
The Snack class is ready, with its name, price and count, except for one method. Here is the start of the Python file, as the starter has it (the other languages say the same in their own syntax):
class Snack:
"""One snack at the stand: its name, its price in whole dollars, and how many are left."""
def __init__(self, name, price, count):
self.name = name
self.price = price
self.count = count
def value(self):
"""
What this snack's stock is worth: its price times how many are left.
Snack("Pretzel", 3, 12).value() -> 36
Snack("Trail Mix", 4, 0).value() -> 0
"""
return 0
- Finish
value(Valuein C#): the snack's price times how many are left. - Find a snack. Finish
findSnack(find_snackin Python,FindSnackin C#): loop through the snacks and return the one whose name matches exactly. If none does, returnnull(Nonein Python). Return the snack itself, not a new one. - Add up the stock. Finish
totalValue: the sum of every snack's value. - Spot what's running low. Finish
lowStock: the names of the snacks with fewer than the given number left, in the list's order.
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 SnackInventory |
| 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 |
At first all 7 tests fail. That's expected: each piece you finish turns its tests green. A failing test's message starts with Not yet: and says what it expected, for example Not yet: $36 + $40 + $32 + $30 = $138.
Hints
- Start with
value. It's one line, andtotalValuecan call it for each snack. - One test changes a snack through what
findSnackreturns and checks the list's snack changed too. That only works if you return the snack from the list itself, as References: Two Names, One Object shows. - Capital letters count.
"trail mix"isn't"Trail Mix", so a plain equality check is right. - Fewer than, not up to. A snack with exactly the limit left isn't low: use
<, not<=. - Build the low-stock list as you go: start with an empty list, and add a name each time a snack is low.
When you're done
Every test is green, and the demo prints the same six lines in every language:
Snacks: Pretzel, Granola Bar, Trail Mix, Apple Chips
Trail Mix is worth: $32
Found: Apple Chips, $2, 15 left
Popcorn found? no
Total value: $138
Low stock (under 10): Trail Mix
Now look at findSnack. For a stand with 4 snacks, it looks at 4 at most. How many could it look at for a warehouse with 40,000? That question, how the work grows, is the next chapter.
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-ds-snack-inventory), 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-ds-snack-inventory.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-ds-snack-inventory, 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've written a class, searched a list with a loop, added up values with a method, and handed back a reference. Every structure in the rest of the course is built from exactly these pieces, starting with the next chapter's question: when the list gets long, how much more work is it?