Amazing-Python-Scripts

Форк
0
96 строк · 3.6 Кб
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(
55
            water_needed, milk_needed, coffee_beans_needed, cups_needed)
56
        if message == "Enough resources. Enjoy your coffee!":
57
            # Prompt for inserting coins and calculate the total amount
58
            print(f"Please insert coins for {coffee_type} (${price}):")
59
            quarters = int(input("How many quarters?: "))
60
            dimes = int(input("How many dimes?: "))
61
            nickels = int(input("How many nickels?: "))
62
            pennies = int(input("How many pennies?: "))
63

64
            total_amount = 0.25 * quarters + 0.10 * dimes + 0.05 * nickels + 0.01 * pennies
65
            if total_amount < price:
66
                return "Insufficient amount. Money refunded."
67
            else:
68
                # Calculate the change, update machine's properties, and return success message
69
                change = round(total_amount - price, 2)
70
                self.money += price
71
                self.water -= water_needed
72
                self.milk -= milk_needed
73
                self.coffee_beans -= coffee_beans_needed
74
                self.cups -= cups_needed
75
                return f"Here is ${change} in change. Here is your {coffee_type}. Enjoy!"
76

77
        # Return the error message if there are insufficient resources
78
        return message
79

80

81
def main():
82
    # Create an instance of the CoffeeMachine class
83
    coffee_machine = CoffeeMachine()
84

85
    while True:
86
        print("============================================")
87
        choice = input("What would you like? (espresso/latte/cappuccino): ")
88

89
        if choice in ["espresso", "latte", "cappuccino"]:
90
            print(coffee_machine.buy_coffee(choice))
91
        else:
92
            print("Invalid choice. Please try again.")
93

94

95
if __name__ == "__main__":
96
    main()
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.