|
| 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() |
0 commit comments