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

1a. 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 and/or add attributes as needed.
1b. 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 add_item 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:
{QUANTITY}{ITEM_NAME}(s) added to inventory
Here, {QUANTITY} and {ITEM_NAME} 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 f-strings.
Testing recommendations for add_item:
Adding a brand new item
Updating the quantity of an existing item
U1c. 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 get_item_price 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 get_item_price:
Get price of a valid item
Get price of an invalid item
1d. 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 get_item_quantity 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 get_item_quantity:
Get quantity of a valid item
Get quantity of an invalid item
1e. 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 list_items that prints the following string if there are no items in the machine (i.e., 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.
1f. 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 insert_money that takes as parameter the dollar amount (float). For simplicity, assume the user will only enter $1, $2, or $5 bills (no coins). The method needs to check if the dollar amount is 1.0,2.0, or 5.0. If so, it needs to add this amount to the customer's balance, which should be rounded to 2 decimal places. When successfully added, it should print the current balance in the following format:
Balance: {BALANCE}
If not successful (i.e., dollar amount is not 1.0,2.0, or 5.0), it should print this message:
Invalid amount
Testing recommendations for insert_money:
Inserting $1, $2, or $5
Inserting some other amount
1g. 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 (i.e, quantity is 0), 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 1 and the price of the item should be deducted from the custumer balance (rounded by 2 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 dollars)of the vending machine should be incremented by the price of the iteam sold (rounded by to 2 decimal places).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!