Coffee & Commits

Python In 30 Days - Day 6

Day six is one of my favorites thus far. I am a big fan of Object Oriented Programming and have always enjoyed it when I get to use it. This was just fun the whole time.

The Challenge:

Convert your restaurant system into a class called Restaurant. It should:

1. Have an __init__ that takes a name and menu (dictionary) and sets up:

2. Have a method add_item(self, item_name) that:

3. Have a method get_total(self) that:

4. Have a __str__ method that returns something like:

"Mario's Kitchen | Menu items: 5 | Current order: 3 items"

Create an instance of your Restaurant, add some items (including a bad one), print the total, and print the object itself

My Code:

class Restaurant:
    def __init__(self, name, menu):
        self.name = name
        self.menu = menu
        self.order = []

    def add_item(self, item_name):
        
        try:
            price = self.menu[item_name]
        except TypeError:
            print(f"Sorry, {item_name} is not a string.\n")
        except KeyError:
            print(f"Sorry, {item_name} is not on the menu!\n")
        else:
            self.order.append(item_name)

           
            print(f"Added {item_name} to order.")
           
                    
    def get_total(self):
        if not self.order:
            print("\nWarning: Your order is empty.\n")
            return 0
    
        total = 0
            
        for i in self.order:
            try:
                total += self.menu[i]
            except KeyError:
                print(f"Warning: {i} is not on the menu. Skipping item.")
        return total
    
        
    def __str__(self):
        menu_len = len(self.menu)
        order_len = len(self.order)
        return f"{self.name} | Menu Items: {menu_len} | Current Order: {order_len}"              


menu = {
    'Burger': 15,
    'Fries': 5,
    'Milkshake': 9,
    'Salad': 6,
    'Brownie': 3
}

order = []
total = 0

mario = Restaurant(
    "Mario's Restaurant",
    {
       'Pizza': 12,
       'Breadsticks': 8,
       'Salad': 6,
       'Tiramisu': 4,
       'Wine': 18 
    }
)
mario.add_item('Pizza')
mario.add_item('Beer')
mario.add_item('Tiramisu')
mario.add_item(['Breadsticks', 'Wine'])
mario_total = mario.get_total()
print(mario)
print(f"Your total is: ${mario_total}")

ian = Restaurant(
    "Ian's BBQ",
    {
        'Pulled Pork': 22,
        'Brisket': 30,
        'Baked Beans': 14,
        'Banana Pudding': 12,
        'Sweet Tea': 5
    }
)

ian_total = ian.get_total()
print(ian)       
print(f"Your total is: ${ian_total}")

Running the Code:

Added Pizza to order.
Sorry, Beer is not on the menu!

Added Tiramisu to order.
Sorry, ['Breadsticks', 'Wine'] is not a string.

Mario's Restaurant | Menu Items: 5 | Current Order: 2
Your total is: $16

Warning: Your order is empty.

Ian's BBQ | Menu Items: 5 | Current Order: 0
Your total is: $0

While it's not the most useful code yet, it works as intended, so that's a win in my book.

Untile next time, keep learning, friends.

#AI #Challenge #Claude #Coding #Growth #OOP #Python