top of page

Coffee machine coding with OOP

  • Writer: 라임 샹큼
    라임 샹큼
  • Mar 9
  • 3 min read

OOP is object oriented programming which helps shorten code so that it is more legible. OOP organizes coding in classes, attributes and methods.

By using OOP, I could simplify the coffee machine project.

Usually when using premade classes to simplify code, there is a manual that instructs you on how to use each method and attribute.

The coffee machine project also came with a bunch of classes and their attributes and methods.

I did find these a bit hard to understand at first though.


Class definitions

MenuItem Class Attributes:

  • -  name

    (str) The name of the drink. e.g. “latte”

  • -  cost

    (float) The price of the drink. e.g 1.5

  • -  ingredients

    (dictionary) The ingredients and amounts required to make the drink. e.g. {“water”: 100, “coffee”: 16}


    Menu Class Methods:

  • -get_items()Returns all the names of the available menu items as a concatenated string. e.g. “latte/espresso/cappuccino”

  • - find_drink(order_name)

    Parameter order_name: (str) The name of the drinks order.Searches the menu for a particular drink by name. Returns a MenuItem object if it exists, otherwise returns None.


In the Menu class method it said that it would return a menuitem object. I didn't understand that it meant that the order_name put into the method would be evaluated and judged if it was in the list of orderable menus.

I first made a variable to ues of the menuitem class which was a mistake.

It turned out the class has only attributes, so it can be only be called by the method in the menu class.


CoffeeMaker Class Methods:

  • -  report()

    Prints a report of all resources. e.g.Water: 300mlMilk: 200ml

    Coffee: 100g

  • -  is_resource_sufficient(drink)

    Parameter drink: (MenuItem) The MenuItem object to make.Prints a message if ingredients are insufficient.Returns True when the drink order can be made, False if ingredients are insufficient. e.g.True


Here, the input 'drink' was also going back to refer to the menuitem. Before, I needed to specify what I was talking about depending on the variable but all I needed to do here was input the drink by also referring to it as menu.find_drink(input('order drink:'))

(of course

menu = Menu()

must be specified beforehand)

Before I fully understood all classes in the directions, I tried to complete the latter code with longer code:


from menu import Menu, MenuItem

from coffee_maker import CoffeeMaker

from money_machine import MoneyMachine


menu= Menu()

coffee_maker = CoffeeMaker()

money_machine = MoneyMachine()


is_on = True

money = 0

while is_on:

    choice = input('What would you like?: espresso, latte, cappuccino?').lower()

    if choice == 'off':

        is_on = False

    elif choice == 'report':

        coffee_maker.report()

        money_machine.report()

    elif choice in ['espresso', 'latte', 'cappuccino']:

        drink = menu.find_drink(choice)

        sufficiency = coffee_maker.is_resource_sufficient(drink)

        if sufficiency:

            cost = 0

            if money_machine.make_payment(drink.cost):


                print('Here is your drink')

            else:

                print('Insufficient funds')

        else:

            print('Insufficient resources')


When all I needed to do was this:


from menu import Menu, MenuItem

from coffee_maker import CoffeeMaker

from money_machine import MoneyMachine


menu= Menu()

coffee_maker = CoffeeMaker()

money_machine = MoneyMachine()


is_on = True

money = 0

while is_on:

    choice = input('What would you like?: espresso, latte, cappuccino?').lower()

    if choice == 'off':

        is_on = False

    elif choice == 'report':

        coffee_maker.report()

        money_machine.report()

    elif choice in ['espresso', 'latte', 'cappuccino']:

        drink = menu.find_drink(choice)

        sufficiency = coffee_maker.is_resource_sufficient(drink)

        if sufficiency:

            if money_machine.make_payment(drink.cost):

                coffee_maker.make_coffee(drink)


What I learned

  • Fundamentals of OOP

    • Classes

    • Attributes(how to use attributes as inputs in methods)

    • Methods

    OOP:

    class is the blueprint for actual intended object

    ex) car blueprint and actual car


    car = CarBlueprint() (object and class respectively)


    attributes = data that an object has

    ex) car.speed(object, attribute)


    methods= can do things

    ex)car.stop() (object, method)

Recent Posts

See All
Reading and writing files

I learned how to read write txt files. I learned how to navigate files. I always wondered how people sent automated emails to so many...

 
 
 
Ping-Pong game

This project really helped me understand OOP more and grasp the concept of inheritance in classes. screen = Screen() screen.setup(height...

 
 
 
Making the well-known Snake game

Everyone has atleast had one instance in which they downloaded snake.io on their phone and had a few goes at it. I wanted to replicate...

 
 
 

1 Comment


COP
COP
May 10

Calculate Your Age Instantly with Our Smart Age Calculator! 🧮

Ever wondered exactly how old you are — not just in years, but in months, days, hours, or even seconds? Our Age Calculator is the easiest and most accurate way to find out!

Whether you're planning a birthday party, tracking milestones, or just having fun with friends, our tool delivers precise results in real time. It's more than just a number — it's your life's timeline!

🔍 Why Use Our Online Age Calculator?

Instant & Accurate: Just input your date of birth, and get a detailed breakdown of your age in years, months, days, hours, minutes, and seconds — updated live.✅ No Hassle, No Sign-Up: Use it right away without creating…

Like

© 2024 by GifTED. Powered and secured by Wix

bottom of page