Building Restaurant Menu using Class in Python

Python | Building Restaurant Menu: Here, we are implementing a Python program to build menu for restaurant using class and object concept.
Submitted by Anuj Singh, on May 05, 2020

Here, we try to use class in python to build a Menu for the restaurant. The Menu will contain the Food Item and its corresponding price. This program aims to develop an understanding of using data abstraction in general application.

Program:

# Definig a class food, 
# which contain name and price of the food item

class Food(object):
    def __init__(self, name, price):
        self.name = name
        self.price = price
    
    def getprice(self):
        return self.price
    
    def __str__(self):
        return self.name + ' : ' + str(self.getprice())
  
# Defining a function for building a Menu 
# which generates list of Food    
def buildmenu(names, costs):
    menu = []
    for i in range(len(names)):
        menu.append(Food(names[i], costs[i]))
    return menu

# items
names = ['Coffee', 'Tea', 'Pizza', 'Burger', 'Fries', 'Apple', 'Donut', 'Cake']

# prices
costs = [250, 150, 180, 70, 65, 55, 120, 350]

# building food menu
Foods = buildmenu(names, costs)

n = 1
for el in Foods:
    print(n,'. ', el)
    n = n + 1

Output

1 .  Coffee : 250
2 .  Tea : 150
3 .  Pizza : 180
4 .  Burger : 70
5 .  Fries : 65
6 .  Apple : 55
7 .  Donut : 120
8 .  Cake : 350





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.