Skip to content

Instantly share code, notes, and snippets.

@Fitzy1293
Created October 7, 2021 14:30
Show Gist options
  • Save Fitzy1293/ddfb1991b9e123cb4e9da6c9d3238d44 to your computer and use it in GitHub Desktop.
Save Fitzy1293/ddfb1991b9e123cb4e9da6c9d3238d44 to your computer and use it in GitHub Desktop.
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="latte", water=200, milk=150, coffee=24, cost=30),
MenuItem(name="espresso", water=50, milk=0, coffee=18, cost=20),
MenuItem(name="cappuccino", water=250, milk=50, coffee=24, cost=25),
MenuItem(name="americano", water=60, milk=0, coffee=60, cost=25)
]
def get_items(self):
"""Returns all the names of the available menu items"""
options = ""
for item in self.menu:
options += f"{item.name}/"
return options
def find_drink(self, order_name):
"""Searches the menu for a particular drink by name. Returns that item if it exists, otherwise returns None"""
for item in self.menu:
if item.name == order_name:
return item
print("Sorry that item is not available.")
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------
class CoffeeMaker:
"""Model of coffee machine that makes the coffee"""
def __init__(self):
self.resources = {
"water": 300,
"milk" : 200,
"coffee" : 100,
}
def report(self):
"""Prints a report of all resources."""
print(f"Water: {self.resources['water']}ml")
print(f"Milk: {self.resources['milk']}ml")
print(f"Coffee: {self.resources['coffee']}g")
def is_resource_sufficient(self, drink):
"""Returns True when order can be made, False if ingredients are insufficient."""
can_make = True
for item in drink.ingredients:
if drink.ingredients[item] > self.resources[item]:
print(f"Sorry there is not enough {item}.")
can_make = False
return can_make
def make_coffee(self, order):
"""Deducts the required ingredients from the resources."""
for item in order.ingredients:
self.resources[item] -= order.ingredients[item]
print(f"Here is your {order.name} ☕. Enjoy!")
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Cashier:
CURRENCY = "DKK"
COIN_VALUES = {"DKK"} # Not used right now, confusing being in a loop.
# I understand accepting diff. denominations, but user isn't prompted for that.
def __init__(self):
self.profit = 0
self.physical_profit = 0
self.card_profit = 0
self.valid_payment = None
def report(self):
"""Print profit"""
print(f"Total profit:\t\t{self.CURRENCY}{self.profit}")
print(f"Physical profit:\t{self.CURRENCY}{self.physical_profit}")
print(f"Card profit:\t\t{self.CURRENCY}{self.card_profit}")
def check_payment(self):
return self.valid_payment
def process_payment(self, cost, payment):
# Add to all profit, and card profit or physical profit based on payment method.
# Save state of payment as valid or not in self.valid_payment
if payment == 'card':
# Credit card automatically takes out correct amount (ignoring overdrafting and invalid cards)
self.profit += cost
self.card_profit += cost
self.valid_payment = True
#PHYSICAL CURRENCIES:
elif payment.replace('.', '').isdigit(): # If they entered a number.
payment = eval(payment)
# Payment equals cost of coffee.
if payment == cost:
self.profit += cost
self.physical_profit += cost
self.valid_payment = True
# Overpayment
elif payment > cost:
change = round(payment - cost, 2)
stored_minus_payment = self.physical_profit - change
# If it's less than 0, machine doesn't have enough physical money to make change.
if stored_minus_payment < 0:
print(f"Not enough {self.CURRENCY} to make change.")
self.valid_payment = False
else:
print(f"Here is {self.CURRENCY}{change} in change.")
self.profit += cost
self.physical_profit += cost
self.valid_payment = True
# Didn't put enough money in.
elif payment < cost:
print("Sorry, that's not enough money. Proccesing refund.")
self.valid_payment = False
else:
print('Invalid number')
self.valid_payment = False
else:
print('Invalid option')
self.valid_payment = False
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------
def welcome():
print('''\033[33m
)))
(((
+-----+
| |] - WELCOME TO THE KEA COFFEE MACHINE!
`-----'
------ MENU ------
Latte (30 DKK)
Espresso (20 DKK)
Cappuccino (25 DKK)
Americano (25 DKK)
------------------
PS: Type "report" at any moment
to check our resources available.
Type "off" to log out from the machine.\033[m
''')
if __name__=='__main__':
menu = Menu()
finalcashier = Cashier()
finalcoffeemaker = CoffeeMaker()
welcome()
while True:
options = menu.get_items()
user_choice = (input(f'What would you like?\nOptions ({options}): ')).strip().lower()
if user_choice == 'off':
print("Have a KEAlicious day!")
break
elif user_choice == 'report':
print()
finalcoffeemaker.report()
print()
finalcashier.report()
print()
elif menu.find_drink(user_choice) is None:
print("Please choose an available option.")
else:
beverage = menu.find_drink(user_choice)
sufficient_resources = finalcoffeemaker.is_resource_sufficient(beverage)
payment = input(f"How many {finalcashier.CURRENCY}? Or enter 'card' to pay by credit/debit: ") # input shouldn't be part of the class, moved it outside of Cashier
print()
finalcashier.process_payment(beverage.cost, payment=payment)
if sufficient_resources and finalcashier.check_payment():
print('Payment accepted! Making your beverage now')
finalcoffeemaker.make_coffee(beverage)
print('-' * 120 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment