I am learning to develop code using OOP. However I am having issues understanding when to use the __init__
constructor. Is __init__
mandatory in OOP? If so how would I use __init__
?
What the following code does is takes the users requested pizza size and toppings and returns and final total.
When I run the following code:
class Pizza:
""" customer orders pizza size and pizza toppings"""
def size_menu(self): # Provides user a menu
self.menu_s = """
What size pizza would you like?
_____________________________________________________________
| 1: Small | 2: Large | 3: Extra Large | 4: Party Size |
| $6.23 | $10.23 | $12.23 | $24.23 |
|___________|____________|__________________|________________|
"""
print(self.menu_s)
return self.menu_s
def size_order(self): # Gets size wanted and returns pizza total.
size_mappings = {
1: "Small",
2: "Large",
3: "Extra Large",
4: "Party Size"
}
cost_mappings = {
"Small": 6.23,
"Large": 10.23,
"Extra Large": 12.23,
"Party Size": 24.23
}
response = input('-') # user inters 1-4 for pizza size wanted and returns a size total.
self.size_wanted = float(response) # Turns response as a float
self.size_wanted = size_mappings[self.size_wanted] # Size requested
self.size_cost = cost_mappings[self.size_wanted] # Cost of size
print(f"Getting your {self.size_wanted} pizza ready.")
print(f"Your current total is: ${self.size_cost}")
return self.size_cost
def topping_menu(self): # Provides user with toppings menu
self.menu_t = """
What toppings do you want on your pizza?
_____________________________________________________
| 1:Bacon | 4:Anchovies | 7:Black Olives |
| 2:Pepperoni | 5:Spinach | 8:Chicken |
| 3:Mushrooms | 6:Onions | 9:Ground Beef |
|________________|__________________|_________________|
What toppings do you want on your pizza?
"""
print(self.menu_t)
return self.menu_t
def topping_order(self): # Gets toppings the user wants and returns a total of all toppings.
topping_mappings = {
1: 'Bacon',
2: 'Pepperoni',
3: 'Mushrooms',
4: 'Anchovies',
5: 'Spinach',
6: 'Onions',
7: 'Black Olives',
8: 'Chicken',
9: 'Ground Beef'
}
self.requested_toppings = []
while True:
response = input('-')
if response == 'q':
break
toppings_wanted = response
toppings_wanted = topping_mappings[int(toppings_wanted)]
self.requested_toppings.append(toppings_wanted)
if toppings_wanted in topping_mappings.values():
print(f"Adding: {toppings_wanted}")
else:
print(f"We do not have {toppings_wanted}")
self.topping_total = len(self.requested_toppings) * float(1.23)
print("\nWe are adding the requested toppings to your pizza.")
print(f"your topping total will be: ${self.topping_total}")
return self.topping_total
def final_total(self):
total = self.size_cost + self.topping_total
total = float(total)
print(f"\nYour final order total will be ${total}")
if __name__ == '__main__':
customer_order = Pizza()
customer_order.size_menu()
customer_order.size_order()
customer_order.topping_menu()
customer_order.topping_order()
customer_order.final_total()
I am wondering why would I use the __init__
constructor if the program is returning the information I am seeking?
Thank you for the assistance.