11class CoffeeMachine :
22 def __init__ (self ):
3+ # Initialize the quantities of resources and money
34 self .water = 500
45 self .milk = 500
56 self .coffee_beans = 200
67 self .cups = 10
78 self .money = 0
89
910 def check_resources (self , water_needed , milk_needed , coffee_beans_needed , cups_needed ):
11+ # Check if there are enough resources to make the selected coffee
1012 if self .water < water_needed :
1113 return "Sorry, not enough water."
1214 elif self .milk < milk_needed :
@@ -20,32 +22,38 @@ def check_resources(self, water_needed, milk_needed, coffee_beans_needed, cups_n
2022
2123 def buy_coffee (self , choice ):
2224 if choice == "espresso" :
25+ # Set the requirements and price for espresso
2326 water_needed = 50
2427 milk_needed = 0
2528 coffee_beans_needed = 18
2629 cups_needed = 1
2730 price = 1.50
2831 coffee_type = "Espresso"
2932 elif choice == "latte" :
33+ # Set the requirements and price for latte
3034 water_needed = 200
3135 milk_needed = 150
3236 coffee_beans_needed = 24
3337 cups_needed = 1
3438 price = 2.50
3539 coffee_type = "Latte"
3640 elif choice == "cappuccino" :
41+ # Set the requirements and price for cappuccino
3742 water_needed = 250
3843 milk_needed = 100
3944 coffee_beans_needed = 24
4045 cups_needed = 1
4146 price = 3.00
4247 coffee_type = "Cappuccino"
4348 else :
49+ # Return an error message for invalid choices
4450 message = "Invalid choice. Please try again."
4551 return message
4652
53+ # Check if there are enough resources to make the selected coffee
4754 message = self .check_resources (water_needed , milk_needed , coffee_beans_needed , cups_needed )
4855 if message == "Enough resources. Enjoy your coffee!" :
56+ # Prompt for inserting coins and calculate the total amount
4957 print (f"Please insert coins for { coffee_type } (${ price } ):" )
5058 quarters = int (input ("How many quarters?: " ))
5159 dimes = int (input ("How many dimes?: " ))
@@ -56,6 +64,7 @@ def buy_coffee(self, choice):
5664 if total_amount < price :
5765 return "Insufficient amount. Money refunded."
5866 else :
67+ # Calculate the change, update machine's properties, and return success message
5968 change = round (total_amount - price , 2 )
6069 self .money += price
6170 self .water -= water_needed
@@ -64,10 +73,12 @@ def buy_coffee(self, choice):
6473 self .cups -= cups_needed
6574 return f"Here is ${ change } in change. Here is your { coffee_type } . Enjoy!"
6675
76+ # Return the error message if there are insufficient resources
6777 return message
6878
6979
7080def main ():
81+ # Create an instance of the CoffeeMachine class
7182 coffee_machine = CoffeeMachine ()
7283
7384 while True :
0 commit comments