Here’s a basic implementation:
pythonCopy codeclass FinanceTracker:
def __init__(self):
self.income = []
self.expenses = []
def add_income(self, amount, source):
self.income.append({'amount': amount, 'source': source})
print(f"Added income: ${amount} from {source}")
def add_expense(self, amount, category):
self.expenses.append({'amount': amount, 'category': category})
print(f"Added expense: ${amount} for {category}")
def total_income(self):
return sum(item['amount'] for item in self.income)
def total_expenses(self):
return sum(item['amount'] for item in self.expenses)
def balance(self):
return self.total_income() - self.total_expenses()
def summary(self):
print("\n--- Financial Summary ---")
print(f"Total Income: ${self.total_income()}")
print(f"Total Expenses: ${self.total_expenses()}")
print(f"Balance: ${self.balance()}\n")
print("Income Details:")
for item in self.income:
print(f"- ${item['amount']} from {item['source']}")
print("Expense Details:")
for item in self.expenses:
print(f"- ${item['amount']} for {item['category']}")
def main():
tracker = FinanceTracker()
while True:
print("1. Add Income")
print("2. Add Expense")
print("3. Show Summary")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
amount = float(input("Enter income amount: "))
source = input("Enter income source: ")
tracker.add_income(amount, source)
elif choice == '2':
amount = float(input("Enter expense amount: "))
category = input("Enter expense category: ")
tracker.add_expense(amount, category)
elif choice == '3':
tracker.summary()
elif choice == '4':
print("Exiting the finance tracker.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
How to Use This Tracker
- Set Up Python Environment: Ensure you have Python installed on your machine. You can download it from python.org.
- Run the Code: Copy the code into a Python file (e.g.,
finance_tracker.py
) and run it using the command:bashCopy codepython finance_tracker.py
- Add Income and Expenses: Follow the prompts to add income and expenses. You can also view a summary of your finances.
- Exit: Choose the exit option when you’re done.
Leave a Reply