Question: 1 a . Implement the constructor Create a class named VendingMachine and a constructor that creates and initializes attributes. Other than self, the constructor should
a Implement the constructor
Create a class named VendingMachine and a constructor that creates and initializes attributes. Other than self, the constructor should NOT take any additional parameters. As mentioned in the Important Notes section, its up to you to decide what variables to create in the constructor. If you followed the suggestion in the Important Notes section, you should already have an idea. You can always come back to the constructor to modify andor add attributes as needed.
b Add items to the vending machine
Periodically, we may want to restock the current snacks in the vending machine, or add brand new snacks. Your class should have the method additem that takes an items name, price and quantity as parameters dont forget self too this will be your last reminder about self The method should add the item to the vending machine. Note that if the item already exists, the method should update the price and add the quantity to the existing stock. At this point, you should think deeply about how you want to hold several pieces of information about every item currently in the vending machine. Setting up an organized way to store and handle this data now will help you tremendously going forwards. When successfully added, the method should print the quantity and the item name in the following format:
QUANTITYITEMNAMEs added to inventory
Here, QUANTITY and ITEMNAME are the actual quantity and name of the item, respectively. Going forwards, we will always use VARIABLE to indicate the actual value of the VARIABLE, just like fstrings.
Testing recommendations for additem:
Adding a brand new item
Updating the quantity of an existing item
Uc Get the price of an item in the vending machine
Before making a purchase, the customer may need to check the price of an item. Your class should have a method called getitemprice that takes an items name as a parameter and returns the price of that item. If the item does not exist in the vending machine, it should print the following and return None:
Invalid item
Testing recommendations for getitemprice:
Get price of a valid item
Get price of an invalid item
d Get the quantity of an item in the vending machine
The owner may want to periodically check the quantity of a snack to decide if it needs to be restocked. Your class should have a method called getitemquantity that takes an items name as a parameter, and returns the quantity of that item. If the item does not exist in the vending machine, it should print the following and return None:
Invalid item
Testing recommendations for getitemquantity:
Get quantity of a valid item
Get quantity of an invalid item
e Display the list of snacks and their quantities
A vending machine should be able to display the list of stacks, including their names, costs and quantities. Your class should have a method called listitems that prints the following string if there are no items in the machine ie an empty vending machine:
No items in the vending machine
Otherwise, it should print the list of snacks with their respective quantities in the following format, sorted alphabetically based on the item name.pdating the cost of an existing item
Hint: Python's sorted function can be used to sort sequences and containers like lists and dictionaries. How to use sorted is dependent on how you chose to store your vending machine items.
f Insert money into the vending machine
When making a purchase, the user needs to insert money into the vending machine to build up a balance. Implement a method called insertmoney that takes as parameter the dollar amount float For simplicity, assume the user will only enter $ $ or $ bills no coins The method needs to check if the dollar amount is or If so it needs to add this amount to the customer's balance, which should be rounded to decimal places. When successfully added, it should print the current balance in the following format:
Balance: BALANCE
If not successful ie dollar amount is not or it should print this message:
Invalid amount
Testing recommendations for insertmoney:
Inserting $ $ or $
Inserting some other amount
g Purchase a snack from the vending machine
Perhaps the most important aspect of a vending machine is to be able to sell snacks. Implement a method called purchase that takes as parameter the name of an item. If the item is not in the vending machine, it should print the following message:
Invalid item
If the item is supposed to be in the vending machine but its not currently in stock ie quantity is it should print the following message:
Sorry ITEM is out of stock
If the item is in stock but the customer does not have enough balance, it should print the following message:
Insufficient balance. Price of ITEM is PRICE
Otherwise, the purchase is successful. The quantity of the purchased item should decrease by and the price of the item should be deducted from the custumer balance rounded by decimal places It should then print the name of the purchased item and the custumers remaining balance in the fallowing format:
Purchased ITEM
Balance: BALANCE
Finally, the total sales in dollarsof the vending machine should be incremented by the price of the iteam sold rounded by to decimal places
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
