python-3.x 计算账单并将总和存储到daily_income

yduiuuwa  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(88)

所以我对Python编程还是个新手,只是测试了一些类和方法,我在为一家餐馆写一个程序,它有4个字典,其中key=food和value=price。字典存储在brunch_items,early_bird_items,dinner_items和kids_items中,然后我为我的Menu类中的每一个创建了一个对象。

from main import *
dinner_items = {
  'crostini with eggplant caponata': 13.00, 
  'caesar salad': 16.00, 
  'pizza with quattro formaggi': 11.00, 
  'duck ragu': 19.50, 
  'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00,
}
kids_items = {
  'chicken nuggets': 6.50, 
  'fusilli with wild mushrooms': 12.00, 
  'apple juice': 3.00
}

brunch_menu = Menu("Brunch Menu", brunch_items, 1100, 1600)
early_bird_menu = Menu("Early Bird Menu", early_bird_items, 1500, 1800)
dinner_menu = Menu("Dinner Menu", dinner_items, 1700, 1100)
kids_menu = Menu("Kids Menu", kids_items, 1100, 2100)

print(brunch_menu.calculate_bill(["pancakes", "waffles"]))

在Menu类中,有一个方法返回不同的菜单以及它们何时可用。下一个方法是计算账单并返回项目的价格。打印账单的输出:16.5

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
        self.daily_income = 0
      
    def __repr__(self):
        return "{} is available from {} - {}".format(self.name, self.start_time, self.end_time)

    def calculate_bill(self, purchased_items):
        bill = 0

        for purchased_item in purchased_items:
            if (purchased_item in self.items):
                bill += self.items[purchased_item]  
        return bill   

    def total_income(self, purchased_items):
        return self.daily_income + purchased_items

我不知道这是怎么回事,但问题是要定义一个方法,将计算出的账单存储到购买项目的日收入/利润中。
我尝试使self.daily_income = 0,并最终将其返回给一个跟踪付款的变量
有什么建议能帮上忙吗?

mspsb9vt

mspsb9vt1#

我认为您可能需要重新审视您所做的一些建模选择,但是只需对代码进行少量更改,您就可以通过将daily_income从示例变量移动到共享变量来获得运行总数。

class Menu: 

    daily_income = 0 # shared between all instances
    
    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):
        return "{} is available from {} - {}".format(self.name, self.start_time, self.end_time)

    def calculate_bill(self, purchased_items):
        bill = 0

        for purchased_item in purchased_items:
            if (purchased_item in self.items):
                bill += self.items[purchased_item]  

        Menu.daily_income + bill    ## Update the shared total

        return bill   

dinner_items = {
  'crostini with eggplant caponata': 13.00, 
  'caesar salad': 16.00, 
  'pizza with quattro formaggi': 11.00, 
  'duck ragu': 19.50, 
  'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00,
}

kids_items = {
  'chicken nuggets': 6.50, 
  'fusilli with wild mushrooms': 12.00, 
  'apple juice': 3.00
}

## ---------------------------
## Simulate table A
## ---------------------------
my_bill = Menu("Dinner Menu", dinner_items, 1700, 1100).calculate_bill(["caesar salad", "duck ragu"])
my_bill += Menu("Kids Menu", kids_items, 1100, 2100).calculate_bill(["chicken nuggets"])
Menu.daily_income += my_bill
print(f"Total Table Bill: ${my_bill}. Our revenue so far: ${Menu.daily_income}")
## ---------------------------

## ---------------------------
## Simulate table B
## ---------------------------
my_bill = Menu("Dinner Menu", dinner_items, 1700, 1100).calculate_bill(["pizza with quattro formaggi"])
Menu.daily_income += my_bill
print(f"Total Table Bill: ${my_bill}. Our revenue so far: ${Menu.daily_income}")
## ---------------------------

这应该给予你:

Total Table Bill: $42.0. Our revenue so far: $42.0
Total Table Bill: $11.0. Our revenue so far: $53.0

相关问题