Heavy Metal Bake Sale


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:

Item Price Quantity Purchase Code
Brownie $0.75 48 B
Muffin $1.00 36 M
Cake Pop $1.35 24 C
Water $1.50 30 W

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 indicates 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.