Skip to content

Commit 9441705

Browse files
Merge pull request #1671 from preetimahto17/master
Coffee Machine Game in Python
2 parents cbe16aa + 6153a3f commit 9441705

File tree

2 files changed

+116
-0
lines changed

2 files changed

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

Coffee-Machine-py-game/readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Coffee Machine Game in Python
2+
3+
This is a Python program that simulates a coffee machine. It allows you to choose from three types of coffee: espresso, latte, and cappuccino. You can check the availability of resources (water, milk, coffee beans, and cups) and buy a coffee by inserting the required amount of coins.
4+
5+
## Prerequisites
6+
7+
- Python 3.x
8+
9+
## Usage
10+
11+
1. Clone the repository or download the `coffee-machine-py-game.py` file.
12+
2. Open a terminal or command prompt and navigate to the directory where the file is located.
13+
3. Run the following command to start the program:
14+
15+
```shell
16+
python coffee-machine-py-game.py
17+
4. Follow the prompts to choose your desired coffee and enter the number of coins for payment.
18+
5. Enjoy your coffee!
19+
20+
## Output
21+
![output](https://i.postimg.cc/pdx4x7SN/output.png)

0 commit comments

Comments
 (0)