1
1
class CoffeeMachine :
2
2
def __init__ (self ):
3
+ # Initialize the quantities of resources and money
3
4
self .water = 500
4
5
self .milk = 500
5
6
self .coffee_beans = 200
6
7
self .cups = 10
7
8
self .money = 0
8
9
9
10
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
10
12
if self .water < water_needed :
11
13
return "Sorry, not enough water."
12
14
elif self .milk < milk_needed :
@@ -20,32 +22,38 @@ def check_resources(self, water_needed, milk_needed, coffee_beans_needed, cups_n
20
22
21
23
def buy_coffee (self , choice ):
22
24
if choice == "espresso" :
25
+ # Set the requirements and price for espresso
23
26
water_needed = 50
24
27
milk_needed = 0
25
28
coffee_beans_needed = 18
26
29
cups_needed = 1
27
30
price = 1.50
28
31
coffee_type = "Espresso"
29
32
elif choice == "latte" :
33
+ # Set the requirements and price for latte
30
34
water_needed = 200
31
35
milk_needed = 150
32
36
coffee_beans_needed = 24
33
37
cups_needed = 1
34
38
price = 2.50
35
39
coffee_type = "Latte"
36
40
elif choice == "cappuccino" :
41
+ # Set the requirements and price for cappuccino
37
42
water_needed = 250
38
43
milk_needed = 100
39
44
coffee_beans_needed = 24
40
45
cups_needed = 1
41
46
price = 3.00
42
47
coffee_type = "Cappuccino"
43
48
else :
49
+ # Return an error message for invalid choices
44
50
message = "Invalid choice. Please try again."
45
51
return message
46
52
53
+ # Check if there are enough resources to make the selected coffee
47
54
message = self .check_resources (water_needed , milk_needed , coffee_beans_needed , cups_needed )
48
55
if message == "Enough resources. Enjoy your coffee!" :
56
+ # Prompt for inserting coins and calculate the total amount
49
57
print (f"Please insert coins for { coffee_type } (${ price } ):" )
50
58
quarters = int (input ("How many quarters?: " ))
51
59
dimes = int (input ("How many dimes?: " ))
@@ -56,6 +64,7 @@ def buy_coffee(self, choice):
56
64
if total_amount < price :
57
65
return "Insufficient amount. Money refunded."
58
66
else :
67
+ # Calculate the change, update machine's properties, and return success message
59
68
change = round (total_amount - price , 2 )
60
69
self .money += price
61
70
self .water -= water_needed
@@ -64,10 +73,12 @@ def buy_coffee(self, choice):
64
73
self .cups -= cups_needed
65
74
return f"Here is ${ change } in change. Here is your { coffee_type } . Enjoy!"
66
75
76
+ # Return the error message if there are insufficient resources
67
77
return message
68
78
69
79
70
80
def main ():
81
+ # Create an instance of the CoffeeMachine class
71
82
coffee_machine = CoffeeMachine ()
72
83
73
84
while True :
0 commit comments