Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedin

in python

15 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, newStock, 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=Vending Machine () >>> 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 newStock will return a message to the user. For already registered items, you use the restock method. Restock retums 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.get Stock() 215: [2.5, 3]) # User deposits 7.5 and purchases 3 items of 215 >>> x.get Stock() {215: [2.5, 0]} >>> x.newStock (215,2.50,2) 'Item already registered >>> x.newStock (156,3,3) Current 156 stock: 3 >>> x.getStock() {215: [2.5, 0], 156: [3, 3]) If all products are out of stock, deposit retums 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.get Stock() {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 retums remaining balance

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_2

Step: 3

blur-text-image_3

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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions