Exercise: SnackShack
Answer a snack bar owner's questions with LINQ. What's cheap, what's in each category, what sells best, what never sells, how much came in each day, and one page of the menu at a time, each in one chain of methods.
Journeyman · C# · Updated
The Snack Shack sells chips, cookies and apples from a counter. Its owner keeps the menu and every sale in lists, and has questions: what's cheap, what sells best, what never sells, and how much money came in on Wednesday. You'll answer each one with LINQ.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-snackshack, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git. Then open SnackShack.slnx in Visual Studio.
| File | What's in it |
|---|---|
SnackShack/Snack.cs, Sale.cs |
A snack on the menu, and one sale of it |
SnackShack/SampleData.cs |
The menu and a week of sales, for the program |
SnackShack/SnackReport.cs |
The owner's questions, for you to answer. Its requirements are in the comment at the top. |
SnackShack.Tests |
Tests for SnackReport, with their own small menu |
What to do
Write each method of SnackReport with LINQ. Most are a single chain of methods, without a foreach.
| Method | Answers |
|---|---|
UnderPrice(maxPrice) |
Snacks at or under a price, cheapest first, ties by name |
NamesInCategory(category) |
The names in one category, alphabetical |
FindByName(name) |
One snack, whatever the capitals, or null |
AllUnder(calories) |
Is every snack under that many calories? |
TotalItemsSold(), Revenue(), RevenueOn(day) |
Items sold, and money taken, overall or on one day |
BestSeller() |
The snack with the most items sold |
NeverSold() |
Snacks on the menu nobody bought |
CategorySummary() |
One line per category: items sold and money taken |
Page(pageNumber, pageSize) |
One page of the menu, alphabetical |
Run the program with Ctrl+F5 to see the week's report, and the tests with Test > Run All Tests.
Hints
- Start with the one-step answers:
NamesInCategoryisWhere,Select,OrderBy,ToList;TotalItemsSoldis oneSum. - Money from a sale is
sale.Snack.Price * sale.Quantity. Put that in aSum. FindByNameisFirstOrDefault, comparing names withEquals(name, StringComparison.OrdinalIgnoreCase)so capitals don't matter.BestSellerandCategorySummarystart withGroupBy. Then each group'sSum(sale => sale.Quantity)is its items sold, andgroup.Keyits name or category.NeverSoldasks, for each snack on the menu, whether no sale is of it:!sales.Any(...)inside aWhere.Page: page 3, with 4 snacks to a page, skips the first 8 snacks, then takes 4.
When you're done
Every test passes, and the program prints:
$1.50 or less:
Banana $0.75
Apple $0.90
Cookie $1.25
Chips $1.50
Granola Bar $1.50
Sweet snacks: Brownie, Candy Bar, Cookie
Items sold: 26
Revenue: $40.65 (Wednesday: $13.25)
Best seller: Chips
Never sold: Granola Bar, Pretzels
Healthy: 4 sold, $4.65
Salty: 13 sold, $22.50
Sweet: 9 sold, $13.50
Count the foreach loops in SnackReport. If the answer is zero, every question became one readable sentence.
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-snackshack), 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. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-snackshack.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-snackshack.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-snackshack, 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
Each question the owner asked became a single chain: where, order, select, sum, group. That's LINQ's promise: say what you want, and let the methods do the looping.