Search code examples
pythonclassobjectoopinstance

Classes and Instances


I am new to OOP and practicing by writing a budget class as asuch:

class Budget:
    category_record = []
    def __init__(self, category = '', balance = 0):
        self.category = category
        self.balance = balance
        
        Budget.category_record.append(self)
        
    
    def deposit(self, amount):
        self.balance += amount
        print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
        return

    def withdraw(self, amount):
        if amount > self.balance:
            print('Insufficient Balance, unable to withdraw')
        else:
            self.balance -= amount
        print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
        return 
    
    def category_balance(self):
        print(f'Your balance is {self.balance} for your {self.category} budget')

IM trying to keep a record of all instances of the budget class as a method(Forgive me if my terms are wrong, still getting used to them)


# Instantiate Food budget 
food = Budget('food')

# deposit 200 into food budget
food.deposit(200)

# withdraw 100 from food budget
food.withdraw(100)

#instantaite rent budget
rent = Budget('house', 5000)

# check balance of rent budget
rent.category_balance()

Such that when I call a record method on the budget class I can get a list of ['food', 'rent'] or if possible a dictionary with the key as category and value as balance {'food':100...}


Solution

  • To elaborate on my @classmethod comment:

    class Budget:
        category_record = []
        def __init__(self, category = '', balance = 0):
            self.category = category
            self.balance = balance
    
            Budget.category_record.append(self)
    
    
        def deposit(self, amount):
            self.balance += amount
            print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
            return
    
        def withdraw(self, amount):
            if amount > self.balance:
                print('Insufficient Balance, unable to withdraw')
            else:
                self.balance -= amount
            print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
            return
    
        def category_balance(self):
            print(f'Your balance is {self.balance} for your {self.category} budget')
    
        @classmethod
        def budget_list(cls):
            if len(Budget.category_record)== 0:
                print("No budgets created")
            else:
                print("Budget Categories:")
                for budge in Budget.category_record:
                    print(budge.category)
    
        @classmethod
        def full_report(cls):
            if len(Budget.category_record)== 0:
                print("No budgets created")
            else:
                print("Budget Balances:")
                for budge in Budget.category_record:
                    print(f"{budge.category} : {budge.balance}")
    
    
    
    
    # Instantiate Food budget
    food = Budget('food')
    
    # deposit 200 into food budget
    food.deposit(200)
    
    # withdraw 100 from food budget
    food.withdraw(100)
    
    #instantaite rent budget
    rent = Budget('house', 5000)
    
    # check balance of rent budget
    rent.category_balance()
    
    Budget.budget_list()
    
    Budget.full_report()