Day 16: Object Oriented Programming (OOP)

Day 16: Object Oriented Programming (OOP)

Coffee project, but using OOP for functionality.

ยท

3 min read

I'm Back!

It's been a little over 6 months since my last post. I, for the sake of my sanity, needed to focus on my new role and decided to pump the breaks on the blog. That didn't stop my learning though. I can't explain how much I've learned joining my new team and while I'm not a huge fan of PHP, I've been able to pick it up over time. Now that I'm a bit more comfortable with my new day-to-day, I'm not as exhausted by the evening and wanted to finish what I started here. My end goal is still to land a job or begin freelance work using Python, but I believe the experience I'm gaining right now I believe, is going to be a crucial tool for me to succeed in working in Python long term. With that said, let's get on to Day 16.

The Coffee Machine Project: Revised for OOP

This article is going to be decently quick since it's an addition to the last project from day 15. The objective is to convert the prompts and reporting to use OOP instead of Procedural programming. The solution to the code is below and was done in the main.py file.

from money_machine import MoneyMachine
from coffee_maker import CoffeeMaker
from menu import Menu, MenuItem

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? ({options}): ")
    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)

So to explain this code again but with OOP, we still need to import the necessary classes and instantiating objects for a money handler, a coffee maker, and a menu. Then, we create a loop that continues as long as the coffee machine is switched on. Inside the loop, the program asks the user to choose an option from the menu, to turn the machine off, or to print a report. If a drink is chosen, it checks if there are enough resources to make the drink and if the payment is sufficient. If both conditions are met, it proceeds to make the coffee. Regarding the resource report, the same logic that was set up before will spit out a message if there are no more ingredients to create the user's drink. If you want to see the entire project folder, headed over to the Github repo where I'm tracking each day. That sums it up!

EOD

I plan to be active again with this blog to get back in motion for my Python Journey. A lot has changed over the last 6 months and I have a few new goals given the current economic and technological environment we're currently in. Things in those respective spaces are getting rocky and shifting quickly. Take care โœŒ๐Ÿพ.

Did you find this article valuable?

Support Kyle Leonard by becoming a sponsor. Any amount is appreciated!

ย