Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Project to demonstrate classes and objects
class Menu:
# The constructor for the menu class.
# Define the initial values for the menu items
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
# Define the string representation for the menus
def __repr__(self):
return "The {name} menu is available from {start} to {end}".format(name=self.name, start=self.start_time, end=self.end_time)
# Define the method to calculate the total bill
def calculate_bill(self, purchased_items):
total_price = 0
# Loop through the list of keys to get the prices of the items
for purchased_item in purchased_items:
# Check to ensure the item is in the menu gefore calculating the price
if purchased_item in self.items:
# Add item to the total price
total_price += self.items.get(purchased_item)
return total_price
# Create business class
class Business:
def __init__(self, name, franchises):
self.name = name
self.franchises = franchises
# Define the franchise
class Franchise:
#menus = []
def __init__(self, address, menus):
self.address = address
self.menus = menus
def __repr__(self):
return "Our location {address}".format(address=self.address)
# Define available menus method
def available_menus(self, time):
# Define empty list for the available menus
available = []
# Loop through all of the menus and check
# if the time falls within the times available
for menu in self.menus:
if time >= menu.start_time and time <= menu.end_time:
available.append(menu)
# Handle the case where no items are available
if len(available) == 0:
return "No items available"
return available
# Create 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("brunch", brunch_items, 11, 16)
# Create 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("early_bird", early_bird_items, 15, 18)
# Create 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("dinner", dinner_items, 17, 23)
# Create kids menu
kids_items = {
'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}
kids = Menu("kids", kids_items, 11, 21)
# Print brunch menu to ensure proper function
print(brunch)
# Calculate the total bill and pass in items
# from different menus
print(brunch.calculate_bill(['pancakes', 'home fries', 'coffee']))
print(early_bird.calculate_bill(['salumeria plate', 'mushroom ravioli (vegan)']))
# Create more franchises
flagship_store = Franchise("1232 West End Road", [kids, brunch, early_bird, dinner])
new_installment = Franchise("12 East Mulberry Street", [kids, brunch, early_bird, dinner])
print(flagship_store)
# Print available menus by time
print(flagship_store.available_menus(12))
print(flagship_store.available_menus(17))
# Create business objects
# Create the first business (Basta)
Basta_Fazoolin = Business("Basta Fazoolin' with my Heart", [flagship_store, new_installment])
# Create the second business (Arepas)
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, 10, 20)
arepas_place = Franchise("189 Fitzgerald Avenue", [arepas_menu])
arepas = Business("Take a' Arepa", [arepas_place])
print(arepas.franchises[0].menus[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.