Heavy Metal Bake Sale

Level: Intermediate 30–60 min

Concepts: Algorithms

Solutions: C# | TypeScript | Python


It is 1999 and a local metal band is looking to have a bake sale to raise funds for their new album. They have promised you a free CD if you make software to help run the sale; they are musicians not mathematicians after all.

There are four items they would like to sell on this sale with specific prices and quantities of each:

ItemPriceQuantityPurchase Code
Brownie$0.7548B
Muffin$1.0036M
Cake Pop$1.3524C
Water$1.5030W

The application must calculate the correct change to give a person if they overpay. If you do not have stock of an item, you cannot make the sale.

Purchases are input as a comma delimited string. The Purchase Code from above will be used in the string to indicate which items are in the transaction.

  • If all items are in stock - respond with a total amount.
  • If an item is out of stock, respond with “X is out of stock” where X is the item out of stock.
  • After the total, prompt for amount paid.
  • If the amount is equal to or greater than the amount due display change due.
  • If the amount is less than the amount due it responds with “Not enough money.”

Examples

Items to Purchase > B,C,W
Total > $3.50
Amount Paid > $4.00
Change > $0.50
Items to Purchase > B
Total > $0.75
Amount Paid > $0.75
Change > $0.00
Items to Purchase > C,M
Total > $2.35
Amount Paid > $2.00
Change > Not enough money
Items to Purchase > W
Total > Water is out of stock

Hint

Look to use Stubs or Mocks or both when handling input and output concerns.

Bonus

Its many years later and your friends are once again looking to have a bake sale to make an album. You dust off your software from 1999 and refactor it to read the items, quantity and price all come from a configuration file. It must also now work as a Restful web service instead of writing to the console.

Solutions

You can find all language solutions here Heavy Metal Bake Sale Solutions

Or you can select a specific language below.

Reference Walkthrough

Agent Full-Bake mode — one commit per language, full domain design with ProductBuilder and OrderBuilder, design-rationale walkthroughs.


  • Katas Are Rehearsal, Not Performance
    You don't practice TDD on production code. You practice on katas and bring the muscle memory to production. The gap between knowing TDD and doing TDD is reps.
  • The Contract Test Is the Only Witness the Agent Cannot Author
    A test the agent wrote against code the same agent wrote shares the agent's blind spot: a misunderstanding in the implementation becomes a matching misunderstanding in the test, the bar goes green, and the bug ships certified. Consumer-driven contract tests are the only category where a second team publishes the assertion the first team has to satisfy. That separation of authorship is exactly what tamper-resistant test design demanded, applied at the integration seam.
  • Cohesion Is the Coverage of the Agent Era
    Agent-generated codebases pass every structural-modularity metric while fragmenting the domain concept the team was trying to encode. Coverage measured whether a line ran. Mutation score measured whether an assertion pinned behavior. Cohesion is the next axis, and it measures whether the codebase's concepts are traversable end-to-end.