编辑
2026-04-01
undefined
00

目录

menu.py
class MenuItem: """Models each Menu Item.""" def __init__(self, name, water, milk, coffee, cost): self.name = name self.cost = cost self.ingredients = { "water": water, "milk": milk, "coffee": coffee } class Menu: """Models the Menu with drinks.""" def init(self): self.menu = [ MenuItem(name="espresso", water=50, milk=0, coffee=18, cost=1.5), MenuItem(name="latte", water=200, milk=150, coffee=24, cost=2.5), MenuItem(name="cappuccino", water=250, milk=50, coffee=24, cost=3), ] def get_items(self): &quot;&quot;&quot;Returns all the names of the available menu items&quot;&quot;&quot; options = &quot;&quot; for item in self.menu: options += f&quot; - {item.name}t${item.cost}n&quot; return options def find_drink(self, order_name): &quot;&quot;&quot;Searches the menu for a particular drink by name. Returns that item if it exists, otherwise returns None&quot;&quot;&quot; for item in self.menu: if item.name == order_name: return item print(&quot;Sorry that item is not available.&quot;)</code></pre><h2>coffee_maker.py</h2><pre><code class="lang-python">class CoffeeMaker: &quot;&quot;&quot;Models the machine that makes the coffee&quot;&quot;&quot; def __init__(self): self.resources = { &quot;water&quot;: 300, &quot;milk&quot;: 200, &quot;coffee&quot;: 100, } def report(self): &quot;&quot;&quot;Prints a report of all resources.&quot;&quot;&quot; print(f&quot;Water: {self.resources['water']}ml&quot;) print(f&quot;Milk: {self.resources['milk']}ml&quot;) print(f&quot;Coffee: {self.resources['coffee']}g&quot;) def is_resource_sufficient(self, drink): &quot;&quot;&quot;Returns True when order can be made, False if ingredients are insufficient.&quot;&quot;&quot; can_make = True for item in drink.ingredients: if drink.ingredients[item] &gt; self.resources[item]: print(f&quot;Sorry there is not enough {item}.&quot;) can_make = False return can_make def make_coffee(self, order): &quot;&quot;&quot;Deducts the required ingredients from the resources.&quot;&quot;&quot; for item in order.ingredients: self.resources[item] -= order.ingredients[item] print(f&quot;Here is your {order.name} ☕️. Enjoy!&quot;) print('*' * 30)</code></pre><h2>money_machine.py</h2><pre><code class="lang-python">class MoneyMachine: CURRENCY = &quot;$&quot; COIN_VALUES = { &quot;quarters&quot;: 0.25, &quot;dimes&quot;: 0.10, &quot;nickles&quot;: 0.05, &quot;pennies&quot;: 0.01 } def __init__(self): self.profit = 0 self.money_received = 0 def report(self): &quot;&quot;&quot;Prints the current profit&quot;&quot;&quot; print(f&quot;Money: {self.CURRENCY}{self.profit}&quot;) def process_coins(self): &quot;&quot;&quot;Returns the total calculated from coins inserted.&quot;&quot;&quot; print(&quot;Please insert coins.&quot;) for coin in self.COIN_VALUES: self.money_received += int(input(f&quot;How many {coin}?: &quot;)) * self.COIN_VALUES[coin] return self.money_received def make_payment(self, cost): &quot;&quot;&quot;Returns True when payment is accepted, or False if insufficient.&quot;&quot;&quot; self.process_coins() if self.money_received &gt;= cost: change = round(self.money_received - cost, 2) print(f&quot;Here is {self.CURRENCY}{change} in change.&quot;) self.profit += cost self.money_received = 0 return True else: print(&quot;Sorry that's not enough money. Money refunded.&quot;) self.money_received = 0 return False</code></pre><h2>main.py</h2><pre><code class="lang-python">from menu import Menu from coffee_maker import CoffeeMaker from money_machine import MoneyMachine money_machine = MoneyMachine() coffee_maker = CoffeeMaker() menu = Menu() is_on = True while is_on: options = menu.get_items() choice = input(f"What would you like?n{options} I like: ") if choice == "off": is_on = False elif choice == "report": coffee_maker.report() money_machine.report() else: drink = menu.find_drink(choice) if coffee_maker.is_resource_sufficient(drink) and money_machine.make_payment(drink.cost): coffee_maker.make_coffee(drink)</code></pre>

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!