承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601
当前位置:首页  >  软件开发  >  python 点餐系统

python 点餐系统

管理员 2023-09-06 08:05:34 软件开发 0 ℃ 0 评论 2251字 收藏

python 点餐系统

Python是一种经常使用的编程语言,它的简洁性和易读性在开发大型利用程序中得到了广泛的利用。在餐厅点餐系统中,Python被广泛利用。

class Meal:
def __init__(self, id, name, price, category):
self.id = id
self.name = name
self.price = price
self.category = category

上面的代码中,我们定义了一个名为“Meal”的类,它包括了菜品的编号、名称、价格和种别等属性。接着,我们可以根据需要添加各种实例方法,比如获得一道菜品详情、添加新的菜品、更新菜品信息等。

class Order:
def __init__(self):
self.meals = []
self.total_price = 0
def add_meal(self, meal):
self.meals.append(meal)
self.total_price += meal.price
def remove_meal(self, meal):
self.meals.remove(meal)
self.total_price -= meal.price

上面的代码块定义了一个名为“Order”的类,它包括了菜品列表和定单总价等属性。接着,我们可以添加相关方法,比如将菜品添加到定单中,从定单中删除某道菜品等。

class Restaurant:
def __init__(self, meals):
self.meals = meals
def get_menu(self):
menu = {}
for meal in self.meals:
if meal.category in menu:
menu[meal.category].append(meal)
else:
menu[meal.category] = [meal]
return menu
def place_order(self):
order = Order()
print("Welcome to our restaurant!")
print("Here is the menu:")
menu = self.get_menu()
for category in menu:
print(f"{category}:")
for meal in menu[category]:
print(f"{meal.name} - ${meal.price}")
print()
while True:
choice = input("Please choose a meal by typing its name, or choose 'done' if you are finished: ")
if choice == "done":
break
for meal in self.meals:
if meal.name == choice:
order.add_meal(meal)
print(f"{meal.name} added to your order.")
print(f"Your total is ${order.total_price}. Thank you for dining with us!")
return order

最后,我们可以定义一个名为“Restaurant”的类。这个类包括了菜品列表,并提供一些方法,比如获得菜单、下单等。在代码中,我们通过调用“Restaurant”类的“place_order()”方法,让用户输入点餐信息,输出定单详情并计算总价格。

文章来源:丸子建站

文章标题:python 点餐系统

https://www.wanzijz.com/view/77087.html

TAG: php教程 centos
X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信