Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

coded in python [5 pts] Write the class Vending Machine that will represent a typical vending machine (item number, price, stock). An instance of Vending

image text in transcribedimage text in transcribed

coded in python

[5 pts] Write the class Vending Machine that will represent a typical vending machine (item number, price, stock). An instance of Vending Machine has access to five methods, new Stock, getStock, purchase, deposit and restock that will describe its interaction with the user returning strings. This vending machine does not give you money back if you don't make a purchase. Tip: use the string format method to construct the strings Vending Machine notes: When creating an instance of this class, the machine is empty, thus, calling getStock, purchase and deposit simply return out of stock messages: >>> x=VendingMachine () >>> x.purchase (215) 'Machine out of stock' >>> x.deposit (10) 'Machine out of stock. Take your $10 back' >>> x.getStock() Machine out of stock" >>> x. restock (215, 9) 'Invalid item' new Stock is used only when the registering a new item. If an item is out of stock or still has stock, using new Stock will return a message to the user. For already registered items, you use the restock method. Restock returns an error when the item is not in the dictionary >>> x.newStock (215, 2.50,2) Current 215 stock: 2 >>> x.restock (215, 1) "Current item stock: 3 >>> x.restock(5, 1) "Invalid item' >>> X.getStock() [215: [2.5, 3]} # User deposits 7.5 and purchases 3 items of 215 >>> x.getStock() {215: [2.5, 0]} >>> x.newStock (215,2.50,2) 'Item already registered >>> x.newStock (156,3,3) "Current 156 stock: 31 >>> x.getStock() {215: [2.5, 0], 156: [3, 3]} . If all products are out of stock, deposit returns an out of stock message >>> x.getStock() {215: [2.5, 0], 156: [3, 0]} >>> x.deposit (10) 'Machine out of stock. Take your $10 back' purchase uses quantity=1 as a default value, which can be overwritten by providing the second argument. Before completing a purchase, machine has to check if the item is valid, if it is on stock and if the user has enough balance. >>> x.getStock() {215: [3, 1], 156: [3.5, 4]} >>> x.purchase (56) 'Invalid item' >>> x.purchase (215) Please deposit $3' >>> x.deposit (1) >>> X.purchase (215) Please deposit $2' >>> x.deposit (10) >>> X. purchase (215) 'Item dispensed, take your $5' >>> x.purchase (215) 'Item out of stock' >>> X.getStock() (215: [3, 0], 156: [3.5, 4]} getStock returns a dictionary where the key is the item and the value is the list [price, stock] purchase will ask for the needed money to complete a purchase if enough items are in stock, otherwise, it returns the current stock of the item (for example 3 items in stock and attempting to purchase 4) Once the user completes one purchase, machine returns remaining balance For more examples of functionality, check the doctest in the starter code Quotes mean method returned a string, no need to append them in your code Return output with the sentences provided. Solution is not case sensitive, which means Balance: $10 is the same as balance: $10, but is not the same as Balance=$10

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions