Skip to content

Instantly share code, notes, and snippets.

@QuantVI
Created November 6, 2018 12:34
Show Gist options
  • Save QuantVI/ca782a65f418e48b787064ca566f7ea7 to your computer and use it in GitHub Desktop.
Save QuantVI/ca782a65f418e48b787064ca566f7ea7 to your computer and use it in GitHub Desktop.
OOP with Restaurants, Franchises, and Businesses
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):
sent = "{} Menu: served {} to {}"
return sent.format(self.name,self.start_time,self.end_time)
def calculate_bill(self, purchased_items):
#by using get we don't care it the item isn't in this menu
charges = [self.items.get(this,0) for this in purchased_items]
return sum(charges)
class Franchise:
def __init__(self,address,menus):
self.address = address
self.menus = menus
def __repr__(self):
return "Address: {}".format(self.address)
def available_menus(self,time):
x = [menu for menu in self.menus if (menu.start_time <= time and
menu.end_time >= time)]
return x
class Business:
def __init__(self,name,franchises):
self.name = name
self.franchises = franchises
def __repr__(self):
sent1 = "Business: {}\n"
sent1 = sent1.format(self.name)
sent2 = []
for franc in self.franchises:
sent2.append(str(franc))
sent3 = '\n'.join(sent2)
sent4 = sent1+sent3
return sent4
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)
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)
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_items = {
'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}
kids_Menu = Menu("Kids",kids_items,1100,2100)
arepas_items = {
'arepa pabellon': 7.00, 'pernil arepa': 8.50,
'guayanes arepa': 8.00, 'jamon arepa': 7.50
}
arepas_Menu = Menu("Take a' Arepa", arepas_items,1000,2000)
# Tests
all_menus = [brunch_Menu,early_bird_Menu,dinner_Menu,kids_Menu]
for menu in all_menus:
print(menu,sep='\n')
print()
order1 = ['pancakes','home fries','coffee']
print(brunch_Menu.calculate_bill(order1),'\n')
order2 = ['salumeria plate','mushroom ravioli (vegan)']
print(early_bird_Menu.calculate_bill(order2),'\n')
# Franchising
flagship_store = Franchise("1232 West End Road",all_menus)
new_installment = Franchise("12 East Mulberry Street",all_menus)
franchises1 = [flagship_store,new_installment]
print(flagship_store,new_installment,'\n',sep='\n')
print(flagship_store.available_menus(1200),'\n')
print(new_installment.available_menus(1700),'\n')
# Businesses
biz1 = Business("Basta Fazoolin' with my Heart", franchises1)
# 3rd frnachise
arepas_place = Franchise("189 Fitzgerald Avenue",[arepas_Menu])
# New business
biz2 = Business("Take a' Arepa",[arepas_place])
print(biz1,'\n')
print(biz2,'\n')
@QuantVI
Copy link
Author

QuantVI commented Nov 6, 2018

From Codecademy's Intensive course - Programming with Python.
Basta Fazoolin'

We create a Menu object, and override the repr.

  • instances such ad dinner_Menu and brunch_Menu
  • each can calculate its own bill

We create a Franchises, with address and Menus

  • You can find out what Menu is available at this Franchise at a specific time (0 to 2400 as int)

We create Businesses that hold Franchises

  • added a business __repr__, though it wasn't asked for

Compared to the "help" video, I've used list comprehensions in many more places, and a .get() with a default value in .calculate_bill() in order to ignore the case when the food item isn't a part of that menu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment