From f462cc5ae075f1b46a4deb6dc48e466b950fcc34 Mon Sep 17 00:00:00 2001 From: rzmk Date: Fri, 6 Aug 2021 18:01:35 -0400 Subject: [PATCH] Simulated Coffee Machine with OOP! Add GIF demo --- .gitignore | 9 ++++- Day 1/band-name-generator.py | 2 +- Day 16/README.md | 6 ++++ Day 16/another_module.py | 1 + Day 16/main.py | 21 ++++++++++++ Day 16/oop-coffee-machine/coffee_maker.py | 29 ++++++++++++++++ Day 16/oop-coffee-machine/main.py | 28 ++++++++++++++++ Day 16/oop-coffee-machine/menu.py | 34 +++++++++++++++++++ Day 16/oop-coffee-machine/money_machine.py | 39 ++++++++++++++++++++++ 9 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 Day 16/README.md create mode 100644 Day 16/another_module.py create mode 100644 Day 16/main.py create mode 100644 Day 16/oop-coffee-machine/coffee_maker.py create mode 100644 Day 16/oop-coffee-machine/main.py create mode 100644 Day 16/oop-coffee-machine/menu.py create mode 100644 Day 16/oop-coffee-machine/money_machine.py diff --git a/.gitignore b/.gitignore index 600d2d3..600bbb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,8 @@ -.vscode \ No newline at end of file +# Ignore Markdown formatting being inconsistent with markdownlint +.prettierignore + +# Ignore virtual environments +venv + +# Ignore random cache +__pycache__ \ No newline at end of file diff --git a/Day 1/band-name-generator.py b/Day 1/band-name-generator.py index 4eb9e7a..4fd2300 100644 --- a/Day 1/band-name-generator.py +++ b/Day 1/band-name-generator.py @@ -3,7 +3,7 @@ print("Welcome to the band name generator!") #2. Ask the user for the city that they grew up in. city_name = input("What's the name of the city you grew up in?\n") #3. Ask the user for the name of a pet. -_name = input("What's your pet's name?\n") +pet_name = input("What's your pet's name?\n") #4. Combine the name of their city and pet and show them their band name. print("Your band name could be " + city_name + " " + pet_name + "!") #5. Make sure the input cursor shows on a new line, see the example at: diff --git a/Day 16/README.md b/Day 16/README.md new file mode 100644 index 0000000..5b3c949 --- /dev/null +++ b/Day 16/README.md @@ -0,0 +1,6 @@ +# Day 16 - Learning OOP with Python + +- OOP concepts were taught in the source main.py file, such as classes, attributes, and methods. The Turtle and prettytable packages were also used to explore these concepts, and I installed prettytable in a virtual environment for this. +- The main objective of today is to simulate a coffee machine using OOP in the oop-coffee-machine folder. The coffee_maker.py, menu.py, and money_machine.py were given as abstract modules to work from in order to understand OOP. + +![Day_16_OOP_Coffee_Machine](https://user-images.githubusercontent.com/30333942/128575429-7173d86e-7ae5-4bf0-909a-34e6e37dfc70.gif) diff --git a/Day 16/another_module.py b/Day 16/another_module.py new file mode 100644 index 0000000..abb42de --- /dev/null +++ b/Day 16/another_module.py @@ -0,0 +1 @@ +another_variable = 12 diff --git a/Day 16/main.py b/Day 16/main.py new file mode 100644 index 0000000..dd57265 --- /dev/null +++ b/Day 16/main.py @@ -0,0 +1,21 @@ +# from another_module import another_variable +# print(another_variable) + +# from turtle import Turtle, Screen +# buddy = Turtle() # creating class object +# print(buddy) +# buddy.shape("turtle") # calling class methods +# buddy.color("blue") +# buddy.forward(100) + +# my_screen = Screen() +# print(my_screen.canvheight) # getting class attribute +# my_screen.exitonclick() + +from prettytable import PrettyTable + +table = PrettyTable() +table.add_column("Pokemon Name", ["Pikachu", "Squirtle", "Charmander"]) +table.add_column("Type", ["Electric", "Water", "Fire"]) +table.align = "l" +print(table) diff --git a/Day 16/oop-coffee-machine/coffee_maker.py b/Day 16/oop-coffee-machine/coffee_maker.py new file mode 100644 index 0000000..fad03d9 --- /dev/null +++ b/Day 16/oop-coffee-machine/coffee_maker.py @@ -0,0 +1,29 @@ +class CoffeeMaker: + """Models the 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!") diff --git a/Day 16/oop-coffee-machine/main.py b/Day 16/oop-coffee-machine/main.py new file mode 100644 index 0000000..ee4dbe2 --- /dev/null +++ b/Day 16/oop-coffee-machine/main.py @@ -0,0 +1,28 @@ +from menu import Menu, MenuItem +from coffee_maker import CoffeeMaker +from money_machine import MoneyMachine + +# Instantiating objects from imported classes +my_coffee_menu = Menu() +my_coffee_maker = CoffeeMaker() +my_money_machine = MoneyMachine() + +# Begin coffee machine with OOP! +response = "" +while response != "off": + menu_items = my_coffee_menu.get_items() + response = input(f"What would you like? ({menu_items}): ") + if response == "off": + break + elif response == "report": + my_coffee_maker.report() + my_money_machine.report() + else: + drink = my_coffee_menu.find_drink(response) + drink_cost = drink.cost + if not my_coffee_maker.is_resource_sufficient(drink): + print(f"Sorry there is not enough ingredients for {response}.") + continue + if not my_money_machine.make_payment(drink_cost): + continue + my_coffee_maker.make_coffee(drink) diff --git a/Day 16/oop-coffee-machine/menu.py b/Day 16/oop-coffee-machine/menu.py new file mode 100644 index 0000000..549d5b4 --- /dev/null +++ b/Day 16/oop-coffee-machine/menu.py @@ -0,0 +1,34 @@ +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=2.5), + MenuItem(name="espresso", water=50, milk=0, coffee=18, cost=1.5), + MenuItem(name="cappuccino", water=250, milk=50, coffee=24, cost=3), + ] + + 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.") diff --git a/Day 16/oop-coffee-machine/money_machine.py b/Day 16/oop-coffee-machine/money_machine.py new file mode 100644 index 0000000..09ca70a --- /dev/null +++ b/Day 16/oop-coffee-machine/money_machine.py @@ -0,0 +1,39 @@ +class MoneyMachine: + + CURRENCY = "$" + + COIN_VALUES = { + "quarters": 0.25, + "dimes": 0.10, + "nickles": 0.05, + "pennies": 0.01 + } + + def __init__(self): + self.profit = 0 + self.money_received = 0 + + def report(self): + """Prints the current profit""" + print(f"Money: {self.CURRENCY}{self.profit}") + + def process_coins(self): + """Returns the total calculated from coins inserted.""" + print("Please insert coins.") + for coin in self.COIN_VALUES: + self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin] + return self.money_received + + def make_payment(self, cost): + """Returns True when payment is accepted, or False if insufficient.""" + self.process_coins() + if self.money_received >= cost: + change = round(self.money_received - cost, 2) + print(f"Here is {self.CURRENCY}{change} in change.") + self.profit += cost + self.money_received = 0 + return True + else: + print("Sorry that's not enough money. Money refunded.") + self.money_received = 0 + return False