CS303E Assignment 13
Due: Monday, July 29 by 11 pm
Submit to Assignment 13 on Gradescope via Canvas
Purpose: To practice creating a class (new
data type) from a given specification.
Limitations: You may only use the Python syntax and
language features covered in the book chapters 1 - 10.
Do not alter the provided testing code in any way.
Review the assignment guidelines.
Implement a class that models a
vending machine.
The vending machines we model shall be quite simple. The vending machines hold a single kind
of drink, assume a bottle of water. They can accept money (US, coins, but not
pennies) and can dispense drinks and return change. The machine can also be
refilled with more drinks.
Download the provided VendingMachine.py file.
This file contains the assignment header for you to fill in and the code that
will test the VendingMachine class after you complete that class.
At the spot indicated with a comment in VendingMachine.py, implement the VendingMachine
class with the following methods (All methods, must of course have the self
parameter as the first parameter.). Note, you must decide what are the necessary
attributes (aka instance variables, fields, internal data) for object of the VendingMachine class.
Keep the number of attributes to the minimum necessary.
The instance variables (attributes, fields) in the VendingMachine class shall use the naming convention
of a single underscore to start the variable name. So for example the variable
to represent the number of drinks present in the machine would be
_num_drinks
. Of course to refer to that attribute (instance
variable) for the current Vending machine we would have the code
self._num_drinks
The methods you must have for the VendingMachine class are:
- An __init__ method with parameters for the initial number of drinks
in the machine, the
price per drink, and the capacity of the machine which is maximum number of drinks the vending
machine can hold. The
initial number of drinks shall be less than or equal to the capacity. The
price shall be a positive integer. The price per drink for a machine is
expressed as number of US cents. Our VendingMachine class shall use integers
when expressing money, not real numbers. When a machine is created, no money
is present for the current transaction. Likewise the the total money the machine has
collected since the last refill is also 0. Realize there are attributes (instance
variables) you will have to create and initialize in this method even though
there are no parameters for those variables.
- A method named insert_money that inserts a given amount of money to the vending machine.
Assume the amount of money inserted is greater than 0. This money is added
to the total money in for the current transaction.
- A method named can_dispense_one_drink that returns true if the vending machine can dispense one
drink, false otherwise. The machine can dispense a drink if there is at
least 1 drink present in the machine and the amount of money that has been inserted
for the current transaction is
greater than or equal to the price of a drink for this machine.
- A method named dispense_one_drink that dispenses a
single drink if possible. This method returns a String. The
possibilities are 'EMPTY' if there are no drinks left, 'INSUFFICIENT FUNDS'
if not enough money has been inserted, and 'ENJOY' if enough money has been
inserted and there is at least one drink in the machine. If a drink is
dispesned (ENJOY) then the number of the drinks present in the machine goes
down by one. Note, if the
machine is empty and not enough money has been inserted the method returns
'EMPTY'. Note, when a drink is dispensed the price of the drink is added to
the total money collected by the machine and the money for the current
transaction goes down by the price of one drink. The method does NOT
automatically return any money in the machine that is more than the price of
a drink. In other words the machine does not automatically give change.
If a drink is not dispensed then the money for the current transaction is
unchanged.
- A method named get_change that returns the money in the
machine for the current transaction.
After this method is called the money for the current transaction is reset
to 0. Note,
this might (but not always) be called after a drink has been dispensed by
the user or if the user changes their mind and simply wants all their money
back. If a drink is dispensed and the user does not get their change then
money for the current transaction stays the same. This simulates forgetting
to get one's change and the money for the current transaction being present
for the next person that uses the machine. Note, this method does
not dispense a drink.
- A method named refill that refills the vending machine.
This method simulates what happens when a worker comes to refill the machine
and pull out the money that has been collected. After this method is called
the number of drinks in the vending machine is the same as the capacity of
the machine. The machine has been completely refilled with drinks. Any money
in the machine for the current transaction is added to the total money
collected for the machine and the money for the current transaction is reset
to 0. The method returns the amount of money the worker pulls out from the
machine, in other words the total money collected by the machine since its
creation or the last refill (and any
money left over from the previous transactions) before resetting the total
money in the machine to 0.
The provided file includes the tests for your VendingMachine class. Do not alter
the provided tests.
Here is a file with multiple
runs of the program. Given the same inputs and the same seed your output
shall match this exactly.
I strongly recommend you check your output and the expected output with
a diff program such as https://www.diffchecker.com/.
Recall, you must
complete the header included in VendingMachine.py