OneCompiler

urkurk c9

123
 class ExpenseTracker:
#Constructor: no parameters.    
    def __init__(self):
        self.expenses = {}
#adds an expense under a specific category. Returns True.        
    def add_expense(self, category, amount):
        if category in self.expenses:
            self.expenses[category] += amount
        else:
            self.expenses[category] = amount
        return True
#returns the total amount of all expenses.      
    def get_total_expenses(self):
        return sum(self.expenses.values())
#returns the total expenses for a specific category. Returns 0 if the category does not exist.
    def get_expenses_by_category(self, category):
        return self.expenses.get(category, 0)
#returns a dictionary of all categories and their respective total expenses.        
    def get_all_expenses(self):
        return self.expenses