Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Lab 2 (10 points) Due date: September 18
Goal: The goal of this assignment is to familiarize with object-oriented programming and provide
functionality to custom classes
General instructions:
? The work in this assignment must be your own original work and be completed alone.
? The instructor and course assistants are available on Piazza and with office hours to answer
any questions you may have. You may also share testing code on Piazza.
? A doctest is provided to ensure basic functionality and may not be representative of the full
range of test cases we will be checking. Further testing is your responsibility.
? Debugging code is also your responsibility.
? You may submit more than once before the deadline; only the final submission will be
graded.
Assignment-specific instructions:
? Download the starter code file from Canvas. Do not change the function names or given
starter code in your script.
? You can assume the objects will be created providing the adequate input. There is no
need for input validation in the constructor.
? If you are unable to complete a method, leave the pass statement
Submission format:
? Submit your LAB2.py file to the Lab 2 Gradescope assignment before the due date.
Section 1: The VendingMachine class (5 pts)
This class will represent a vending machine that only gives the user back money after making a
purchase (or if the machine is out of stock).
This vending machine will sell four different products:
Product ID Price
156 1.5
254 2.0
384 2.5
879 3.0
When creating an instance of this class, the machine starts out with 3 items of each product. A
dictionary could be useful here to keep track of the stock. A VendingMachine object returns strings
describing its interactions.
Tip: Python’s string formatting syntax could be useful
>>> item, price, stock = 'Potatoes', 3.5, [20]
>>> '{} cost {} and we have {}'.format(item, price, stock)
'Potatoes cost 3.5 and we have [20]'
Methods
Type Name Description
str purchase(self, item, qty=1) Attempts to buy something from the machine.
str deposit(self, amount) Deposits money into the vending machine.
str restock(self, item, stock) Adds stock to the vending machine
bool isStocked(self) A property method that checks for the stock status.
dict getStock(self) A property method that gets the current stock status
of the machine.
None setPrice(self, item, new_price) Changes the price of an item in the vending machine
Section 1: The VendingMachine class
Preconditions:
purchase(self, item, qty=1)
Attempts to buy something from the vending machine. Before completing the purchase, check to
make sure the item is valid, there is enough stock of said item, and there is enough balance.
Input (excluding self)
int item An integer that might represent item ID of item to purchase
int qty The desired quantity of said item. Defaults to 1
Output
str
“Item dispensed” if there is no money to give back to the user
“Item dispensed, take your $change back” if there is change to give back
“Invalid item” if the item id is invalid
“Machine out of stock” is returned if the machine is out of stock for all items
“Item out of stock” is returned if there is no stock left of requested item.
“Current item_id stock: stock, try again” if there is not enough stock
“Please deposit $remaining” if there is not enough balance
deposit(self, amount)
Deposits money into the vending machine, adding it to the current balance.
Input (excluding self)
int or float amount Amount of money to add to the balance.
Output
str
“Balance: $balance” when machine is stocked
“Machine out of stock. Take your $amount back” if the machine is out of stock.
restock(self, item, stock)
Adds stock to the vending machine.
Input (excluding self)
int item Item ID of item to restock.
int stock The amount of stock to add to the vending machine.