Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The Vendor and VendingMachine classes (4 pts) These classes will represent a vendor (someone that makes goods and services available to companies or consumers) and
The Vendor and VendingMachine classes (4 pts) These classes will represent a vendor (someone that makes goods and services available to companies or consumers) and a vending machine that gives the user money back only when a transaction is cancelled, after making a purchase (change back), or if the machine is out of all stock. Instances of VendingMachine are created through an instance of the Vendor class (already implemented for you in the starter code) using the install method. 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 VendingMachine, the machine starts out with 3 items of each product. A VendingMachine object returns strings describing its interactions. All numbers representing money must be displayed as floats. VendingMachine -Your attributes herepurchase(item: int, qty=1: int) -> str deposit(amount: int/float) -> str _restock(item: int, stock: int) -> str isStocked() -> bool getStock() -> dict cancelTransaction() -> None, str Class diagram for VendingMachine Note: Read the description of all methods first, then outline your strategy. In this section, you are expected to reduce the amount of repeated code by calling methods in the class to reuse code. Both functionality and design are part of the grade. 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. Output (in order of priority) str "Invalid item" if the item id is invalid (Highest priority) "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 "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 deposit(self, amount) Deposits money into the vending machine, adding it to the current 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) A protected method that adds stock to the vending machine. Note that when the VendingMachine runs out of a product, a Vendor object will trigger the operation using its restock method (already implemented for you) Output str "Current item stock: stock" for existing id "Invalid item" is returned if the item id is invalid. isStocked(self) A property method (behaves like an attribute) that returns True if any item has any nonzero stock, and False if all items have no stock. Output bool Status of the vending machine. getStock(self) A property method (behaves like an attribute) that gets the current stock status of the machine. Returns a dictionary where the key is the item and the value is the list [price, stock]. Output dict Current stock status represented as a dictionary. cancelTransaction(self) Returns the current balance to the user Output None Nothing is returned if balance is 0 str "Take your $amount back" if there is money to give back Examples: >>> vendor1 = Vendor('John Doe') >>> x = vendor1.install() >>> x.getStock {156: [1.5, 3], 254: [2.0, 3], 384: [2.5, 3], 879: [3.0, 3]} >>> x.purchase(56) 'Invalid item' >>> x.purchase(879) 'Please deposit $3.0' >>> x.deposit(10) 'Balance: $10.0' >>> x.purchase(156, 3) 'Item dispensed, take your $5.5 back' >>> x.isStocked True
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started