Amazing-Python-Scripts
96 строк · 3.6 Кб
1class CoffeeMachine:
2def __init__(self):
3# Initialize the quantities of resources and money
4self.water = 500
5self.milk = 500
6self.coffee_beans = 200
7self.cups = 10
8self.money = 0
9
10def check_resources(self, water_needed, milk_needed, coffee_beans_needed, cups_needed):
11# Check if there are enough resources to make the selected coffee
12if self.water < water_needed:
13return "Sorry, not enough water."
14elif self.milk < milk_needed:
15return "Sorry, not enough milk."
16elif self.coffee_beans < coffee_beans_needed:
17return "Sorry, not enough coffee beans."
18elif self.cups < cups_needed:
19return "Sorry, not enough cups."
20else:
21return "Enough resources. Enjoy your coffee!"
22
23def buy_coffee(self, choice):
24if choice == "espresso":
25# Set the requirements and price for espresso
26water_needed = 50
27milk_needed = 0
28coffee_beans_needed = 18
29cups_needed = 1
30price = 1.50
31coffee_type = "Espresso"
32elif choice == "latte":
33# Set the requirements and price for latte
34water_needed = 200
35milk_needed = 150
36coffee_beans_needed = 24
37cups_needed = 1
38price = 2.50
39coffee_type = "Latte"
40elif choice == "cappuccino":
41# Set the requirements and price for cappuccino
42water_needed = 250
43milk_needed = 100
44coffee_beans_needed = 24
45cups_needed = 1
46price = 3.00
47coffee_type = "Cappuccino"
48else:
49# Return an error message for invalid choices
50message = "Invalid choice. Please try again."
51return message
52
53# Check if there are enough resources to make the selected coffee
54message = self.check_resources(
55water_needed, milk_needed, coffee_beans_needed, cups_needed)
56if message == "Enough resources. Enjoy your coffee!":
57# Prompt for inserting coins and calculate the total amount
58print(f"Please insert coins for {coffee_type} (${price}):")
59quarters = int(input("How many quarters?: "))
60dimes = int(input("How many dimes?: "))
61nickels = int(input("How many nickels?: "))
62pennies = int(input("How many pennies?: "))
63
64total_amount = 0.25 * quarters + 0.10 * dimes + 0.05 * nickels + 0.01 * pennies
65if total_amount < price:
66return "Insufficient amount. Money refunded."
67else:
68# Calculate the change, update machine's properties, and return success message
69change = round(total_amount - price, 2)
70self.money += price
71self.water -= water_needed
72self.milk -= milk_needed
73self.coffee_beans -= coffee_beans_needed
74self.cups -= cups_needed
75return f"Here is ${change} in change. Here is your {coffee_type}. Enjoy!"
76
77# Return the error message if there are insufficient resources
78return message
79
80
81def main():
82# Create an instance of the CoffeeMachine class
83coffee_machine = CoffeeMachine()
84
85while True:
86print("============================================")
87choice = input("What would you like? (espresso/latte/cappuccino): ")
88
89if choice in ["espresso", "latte", "cappuccino"]:
90print(coffee_machine.buy_coffee(choice))
91else:
92print("Invalid choice. Please try again.")
93
94
95if __name__ == "__main__":
96main()
97