Exercise: Polymorphism

Write a café Order that holds any mix of menu items in one List<MenuItem>, and works out the total, a meal-deal discount and the receipt, letting each item price itself.

Journeyman · C# · OOP · Updated

The café's menu from Exercise: Inheritance is finished: MenuItem, Drink and Pastry. Now customers need to order. You'll write the Order class, which holds any mix of items in one list and adds them up.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-polymorphism, 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 CafeMenu.slnx in Visual Studio.

File What's in it
CafeMenu/MenuItem.cs, Drink.cs, Pastry.cs The menu, finished
CafeMenu/Order.cs The order, for you to finish. Its requirements are in the comment at the top.
CafeMenu.Tests/OrderTests.cs Tests for Order

What to do

Finish each member of Order:

Member What it does
Add(item) Puts an item on the order
Total Every item's price, added up
DrinkCount(), PastryCount() How many drinks, and how many pastries
Discount $0.50 off for each pastry that comes with a drink
AmountDue The total, less the discount
ReceiptLines() Each item's menu line, then the discount if there is one, then the amount due

Run the program with Ctrl+F5 to see a receipt, and the tests with Test > Run All Tests.

Hints

  • Let each item price itself. Loop over the items as MenuItems: item.Price and item.ToString() run each item's own override, so a large latte comes to $5.00 without a single if.
  • Counting is the one place you need the kind: if (item is Drink).
  • The meal deal pairs drinks with pastries: two drinks and three pastries make two deals. Math.Min finds the smaller count.
  • One test brings a stranger: a Soup class that Order has never heard of. If your Total uses Price, it passes with no change to Order.

When you're done

Every test passes, and the program prints:

Large Latte, $5.00
Croissant, $3.75 (warmed)
Blueberry Muffin, $2.50
Meal deal: -$0.50
Amount due: $10.75

Look at your Total: one loop, no if, and it prices every kind of item correctly, including ones that don't exist yet.

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-polymorphism), 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. 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-polymorphism.git
    git push -u origin main
    

    If 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-polymorphism.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-polymorphism, 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

That's polymorphism earning its keep. The order works with "menu items", and each item knows its own price, so a new kind of item on the menu needs no change to the order at all.

Key terms

Related