Starting from:
$35

$29

Project 3 Vending Machine Solution


Write a simulation of a vending machine. This will be an interactive
program, where the user will enter commands on the console. For
example, the user could enter several coins, and then select an item by
saying 'press', like this:

vending
quarter // user input
balance: 0.25 // response printed out

nickel // input
balance: 0.30 // response

quarter quarter dime // put in several coins on one line
balance: 0.90

press coke

Suppose that a coke costs $0.70. Since the user entered $0.90,
the change would be $0.20, which would be dispensed as 2 dimes, hence
the following would print out:

dispensing: coke
change: dime
change: dime

The input from the user should consist of the following commands
(and anything else should be rejected):

[quarter|nickel|dime]+
press <item
change
quit

The 'change' command simply returns coins adding up to the
current balance.

Part of the challenge of this assignment
is figuring out the algorithm for making changes.
For example, if the user has put in $1.00,
and an item costs $0.65, which coins should be dispensed?

Of course, like a real vending machine, your simulated machine has
only a finite supply of each item. If an item is (or goes) out of
stock, your program should say something like "Item not available."
Similarly, there is a finite supply of coins in the machine. If you
are out of dimes, you should dispense nickels in their place, and so
on.

In order to specify the contents of the vending machine, this will be
done by reading in a database (flat file) that defines the name,
count, and cost/price/value of all items in the machine, including
coins. You will read in this file at the beginning of each run to
initialize the state of the machine. You do NOT have to write out
this file after each run (i.e. to update the state based on the items
that have been purchased). Here is an example of the vending database
(save it as something like 'vending1.txt'):

vending1.txt:
---------------
quarter 10 0.25
nickel 20 0.05
dime 20 0.10
coke 10 1.00
gum 5 0.50
chips 15 0.75

You may make up any items you want to put in your machine.
Doritos, snickers, toys... :-)


(Hint: since the contents of the vending machine could be arbitrary,
you will probably want to use vectors to store the information (names,
counts, cost). You will probably want to write a get_change()
function, and maybe a function for searching the vector of item names
to find its index.)

More products