Home > Net >  Calculating a bill and store the sum to a daily_income
Calculating a bill and store the sum to a daily_income

Time:12-23

So I'm very new to Python programming and just testing out some stuff in classes and methods. Im writing a program for a restaurant that have 4 dictionaries that contains key=food and value=price The dict's are stored in brunch_items, early_bird_items, dinner_items and kids_items, and then I created a object for each of them to my Menu class.

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"]))

In the Menu class have the method that returns the different menus and when they are available. The next method is calculating the bill and return the price for the items. Output from printing the bill: 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

I dont know where this is going, but the problem is to define a method that takes the calculated bill and store it to a daily income/profit from the purchased items. The idea is to store the total sum from bills

I tried to make self.daily_income = 0 and eventually return that to a variable that keep tracking the payments

Any suggestions to help out?

CodePudding user response:

There are some modeling choices that you have made that I think you will want to revisit, but with light changes to your code you can get a running total by moving daily_income from an instance variable to a shared variable.

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}")
## ---------------------------

That should give you:

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