0% found this document useful (0 votes)
60 views2 pages

Vending Machine Py Code

The document defines a VendingMachine class that allows users to add items, display them, calculate total prices, and process payments. It includes methods for welcoming users, handling cancellations, and providing goodbye messages. The class can be instantiated and used to simulate a vending machine experience with item selection and payment processing.

Uploaded by

mhdshan8705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

Vending Machine Py Code

The document defines a VendingMachine class that allows users to add items, display them, calculate total prices, and process payments. It includes methods for welcoming users, handling cancellations, and providing goodbye messages. The class can be instantiated and used to simulate a vending machine experience with item selection and payment processing.

Uploaded by

mhdshan8705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class VendingMachine:

def __init__(self):
self.items = {}

def add_item(self, code, name, price):


self.items[code] = {'name': name, 'price': price}

def display_items(self):
print("Code | Item Name | Price")
for code, item in self.items.items():
print(f"{code:<6}| {item['name']:<12}| ${item['price']:.2f}")

def calculate_total_price(self, code, quantity):


if code in self.items:
item_price = self.items[code]['price']
total_price = item_price * quantity
return total_price
else:
return None

def process_payment(self, total_price):


payment_method = input("Please select payment method (cash or card): ").lower()
if payment_method == "cash":
amount_paid = float(input("Enter the amount paid: "))
change = amount_paid - total_price
if change >= 0:
print(f"Change: ${change:.2f}")
else:
print("Insufficient amount paid.")
elif payment_method == "card":
print("Payment successful.")
else:
print("Invalid payment method.")

def welcome_message(self):
print("Welcome to the Vending Machine!")

def goodbye_message(self):
print("Thank you for using the Vending Machine. Have a good day!")

def cancel_message(self):
print("Sorry, we could not provide you with what you would like today.")
print("We hope to be seeing you again soon. Wish you a Good day!")
def start(self):
self.welcome_message()
while True:
self.display_items()
code = input("Enter the code of the item you wish to buy: ")
quantity = int(input("Enter the number of items: "))
total_price = self.calculate_total_price(code, quantity)
if total_price is not None:
print(f"Item: {self.items[code]['name']} | Quantity: {quantity} | Individual Price:
${self.items[code]['price']:.2f} | Total Price: ${total_price:.2f}")
option = input("Options: (add another, finish and pay, cancel): ").lower()
if option == "add another":
continue
elif option == "finish and pay":
self.process_payment(total_price)
break
elif option == "cancel":
self.cancel_message()
break
else:
print("Invalid option.")
else:
print("Invalid item code.")
self.goodbye_message()

# Example usage:
vending_machine = VendingMachine()
vending_machine.add_item("A1", "Soda", 1.50)
vending_machine.add_item("B1", "Chips", 1.00)
vending_machine.start()

You might also like