Change calculator

How to write a change calculator for a coffee shop menu?
). The cashier will enter the size of
the beverage ordered (example: ‘S’ – small, ‘M’ – medium, ‘L’ - large).
We will assume the customers will only order one type and size of beverage, but any quantity. For
example, 5 Small Flat White, or 3 Large Tea, or 6 Medium Long Black, etc. The cashier will enter the
number of drinks being ordered. Your program will then calculate the total cost based on the item
number, beverage size and number of drinks.
For simplicity, we will assume the customer will always pay more than the total cost of the order.
The cahier will enter the amount paid by the customer and your program will calculate the amount
of change to give. Lastly, your program will calculate and display the number of each denomination
to give as the change. Assume the shop only has $10 and $5 notes, and $2, $1, 50c, 20c, 10c and 5c
coins for giving out change.

Menu
Beverage Small Medium Large
Long Black $3.50 $3.70 $4.00
Flat White $3.80 $4.20 $4.50
Cappuccino $4.00 $4.30 $5.00
Tea $3.00 $3.20 $3.50

I am so confused and I don't know where to start. Can someplease please explain the first few steps???
Last edited on
you have 2 pieces when you break it down ... I/O fluff (show them the menu, get their order, get their payment, etc)
and the change machine.
the change machine can be done in any of many ways.
the problem didn't say not to (this is a MISSED REQUIREMENT, a common thing in real world problems too), so you COULD just compute the change and give them rolls of dimes or nickels :P so if they buy a 3.50 tea (is this a 2 gallon jug?! nice prices!) and give you 5 bucks, you can say 500-350 = 150, /10 is 15 10 cent coins returned in change. (notice that I did this without decimals. decimals will cause weird problems, just use integers such that 1 = 1 penny).
ok, so assuming the teacher had asked it more sensibly, though? You typically want to give back the least amount of each type of money in change, so that 150 cents above should be 1 dollar bill and 1 50 cent piece.
one simple (and inefficient) way to do it is a bunch of while loops... while change >= $10 give them $10 bills. When that loop ends, while change >= $5 give them %5 bills (oh, did we notice that this can only happen once? If they needed 2 5s, they should have gotten another $10 instead!) so its not a loop, but an if-statement here. Follow this idea on down to the lowest denomination -- some will be loops, some will be one-shots due to the previous denomination. Only 10 can be very large numbers, eg they gave you a wilson ($100k bill ?!).

Last edited on
Hello vaturogo,

As jonnin started out and the way I see it:

How to write a change calculator for a coffee shop menu?

Part 1.

  a). The cashier will enter the size of
      the beverage ordered (example: ‘S’ – small, ‘M’ – medium, ‘L’ - large).

  b). We will assume the customers will only order one type and size of beverage, but any quantity. For
      example, 5 Small Flat White, or 3 Large Tea, or 6 Medium Long Black, etc.

 c). The cashier will enter the
     number of drinks being ordered.

Part 2.

Your program will then calculate the total cost based on the item
number, beverage size and number of drinks.

For simplicity, we will assume the customer will always pay more than the total cost of the order.

The cahier will enter the amount paid by the customer and your program will calculate the amount
of change to give. Lastly, your program will calculate and display the number of each denomination
to give as the change.

Assume the shop only has $10 and $5 notes, and $2, $1, 50c, 20c, 10c and 5c
coins for giving out change.

Menu
Beverage Small Medium Large
Long Black $3.50 $3.70 $4.00
Flat White $3.80 $4.20 $4.50
Cappuccino $4.00 $4.30 $5.00
Tea $3.00 $3.20 $3.50


             MENU
--------------------------------

   Beverage   Small  Medium Large
--------------------------------
1. Long Black $3.50  $3.70  $4.00
2. Flat White $3.80  $4.20  $4.50
3. Cappuccino $4.00  $4.30  $5.00
4. Tea        $3.00  $3.20  $3.50
5. Exit (optional)
  Enter Choice: 

  Enter Size (S, M or L): 

  Enter Qty.: 


The second menu is just an option.

I worked on the program for awhile an came up with this:

                 MENU
--------------------------------------

    Beverage     Small   Medium  Large
--------------------------------------

 1. Long Black   $3.50   $3.70   $4.00
 2. Flat White   $3.80   $4.20   $4.50
 3. Cappuccino   $4.00   $4.30   $5.00
 4. Tea          $3.00   $3.20   $3.50
 5. Exit (optional)
    Enter Selection: 4

 Enter Size (S - M - L): l

 Enter Quantity: 1

 Your order is:
--------------------------------------
   1  Tea         L    $3.50

 Press Enter to continue:


One note: I used an "int" for the prices. And an array of structs to hold the data.

See what you can come up with for part 1.

Andy
Topic archived. No new replies allowed.