Gojilla
Gojilla

Reputation: 83

Question regarding classes / objects in Python

class Franchise:

  def __init__(self, address, menus):
    self.address = address
    #self stands in for the object getting created
    #when we create an object, we call on init
    #when the init constructor gets called, it returns a "Franchise" object
    #self is the object getting returned from the constructor
    #we assign self."address" of the object from the constructor equal to the address that got passed into the ( )
    self.menus = menus

  def __repr__(self):
    return self.address

  def available_menus(self, time):
    available_menu = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:  <---- QUESTION HERE
        #menu.start_time, menu is defined in Class Menu
        available_menu.append(menu)
    return available_menu

class Menu:
  def __init__(self, name, items, start_time, end_time):
    self.name = name
    self.items = items
    self.start_time = start_time
    self.end_time = end_time

  def __repr__(self):
    return self.name + ' menu available from ' + str(self.start_time) + ' to ' + str(self.end_time)

  def calculate_bill(self, purchased_items):
    bill = 0
    for purchased_item in purchased_items:
      if purchased_item in self.items:
        item = self.items.get(purchased_item)
        #another way is:
        #item = self.items[purchased_item]
        bill += item
    return bill


#Brunch Menu
brunch_items = {
  'pancakes': 7.50, 'waffles': 9.00, 'burger': 11.00, 'home fries': 4.50, 'coffee': 1.50, 'espresso': 3.00, 'tea': 1.00, 'mimosa': 10.50, 'orange juice': 3.50
}

brunch_menu = Menu("Brunch", brunch_items, 1100, 1600) 

print(brunch_menu.calculate_bill(['pancakes', 'home fries', 'coffee']))

#print(brunch_menu)

#Early Bird Menu
early_bird_items = {
  'salumeria plate': 8.00, 'salad and breadsticks (serves 2, no refills)': 14.00, 'pizza with quattro formaggi': 9.00, 'duck ragu': 17.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 1.50, 'espresso': 3.00,
}

early_bird_menu = Menu('Early Bird', early_bird_items, 1500, 1800)

print(early_bird_menu.calculate_bill(['salumeria plate', 'mushroom ravioli (vegan)']))

#Dinner Menu
dinner_items = {
  'crostini with eggplant caponata': 13.00, 'ceaser salad': 16.00, 'pizza with quattro formaggi': 11.00, 'duck ragu': 19.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00,
}

dinner_menu = Menu('Dinner', dinner_items, 1700, 2300)

#Kids Menu
kids_items = {
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}

kids_menu = Menu('Kids', kids_items, 1100, 2100)

menus = [brunch_menu, early_bird_menu, dinner_menu, kids_menu]

flagship_store = Franchise("1232 West End Road", menus)

new_installment = Franchise("12 East Mulberry Street", menus)

I have started to learn about classes and object and am a bit confused with regards to the code above.

This was the prompt: "Let’s tell our customers what they can order! Give Franchise an .available_menus() method that takes in a time parameter and returns a list of the Menu objects that are available at that time."

"if time >= menu.start_time and time <= menu.end_time"

Why are we allowed to use menu.start_time and menu.end_time.

Is it because we defined 'class Menu' ?

I'm a bit confused as to why we are allowed to use menu.start_time and menu.end_time because I didn't define that anywhere.

For example: self.menus was defined under def init(self, address, menus)

But nowhere was menu.start_time or menu.end_time defined.

Upvotes: 0

Views: 131

Answers (1)

chepner
chepner

Reputation: 530872

When you created each Franchise, you gave it a list of Menu instances:

menus = [brunch_menu, early_bird_menu, dinner_menu, kids_menu]
flagship_store = Franchise("1232 West End Road", menus)
new_installment = Franchise("12 East Mulberry Street", menus)

Each call to Franchise saves a reference to that list of menus:

class Franchise:
    def __init__(self, address, menus):
        self.address = address
        self.menus = menus

so that when you call the available_menus method, you can iterate over that list.

def available_menus(self, time):
    available_menu = []
    for menu in self.menus:
        if time >= menu.start_time and time <= menu.end_time:
            available_menu.append(menu)
    return available_menu

and each of those instances had its start_time and end_time attributes defined when it was created.

class Menu:
    def __init__(self, name, items, start_time, end_time):
        self.name = name
        self.items = items
        self.start_time = start_time
        self.end_time = end_time

    ...

early_bird_menu = Menu('Early Bird', early_bird_items, 1500, 1800)

Upvotes: 1

Related Questions