Python

 

 

Introduction

Managing personal finances is an important skill that everyone should develop. One of the best ways to stay on top of your finances is by keeping track of your expenses. With an expense tracker, you can monitor your spending, categorize your expenses, and work towards your financial goals.

Objective

The objective of this program is to provide an easy way to track and categorize personal expenses. It allows the user to input different types of expenses, categorize them, and calculate totals for each category. This can help you identify where you’re spending the most money and improve your budgeting skills.

Expense Tracker Program Code


# Expense Tracker Program
# Author: Learn Programming

class Expense:
    def __init__(self, name, amount, category):
        self.name = name
        self.amount = amount
        self.category = category

    def __str__(self):
        return f"{self.name}: ${self.amount} ({self.category})"


class ExpenseTracker:
    def __init__(self):
        self.expenses = []

    def add_expense(self, name, amount, category):
        expense = Expense(name, amount, category)
        self.expenses.append(expense)

    def get_total_expenses(self):
        total = sum(expense.amount for expense in self.expenses)
        return total

    def get_expenses_by_category(self):
        category_totals = {}
        for expense in self.expenses:
            if expense.category not in category_totals:
                category_totals[expense.category] = 0
            category_totals[expense.category] += expense.amount
        return category_totals

    def display_expenses(self):
        for expense in self.expenses:
            print(expense)

    def display_summary(self):
        print("\nExpense Summary:")
        print(f"Total Expenses: ${self.get_total_expenses()}")
        category_totals = self.get_expenses_by_category()
        for category, total in category_totals.items():
            print(f"{category}: ${total}")


# Program Execution
tracker = ExpenseTracker()

# Sample input
tracker.add_expense("Coffee", 5.50, "Food")
tracker.add_expense("Groceries", 50.00, "Food")
tracker.add_expense("Movie Ticket", 12.00, "Entertainment")
tracker.add_expense("Gym Membership", 30.00, "Fitness")

# Display all expenses
tracker.display_expenses()

# Display expense summary
tracker.display_summary()
        

Program Explanation

This expense tracker program is structured as follows:

  • Expense Class: This class represents a single expense. It stores the expense name, amount, and category.
  • ExpenseTracker Class: This class is responsible for managing a list of expenses. It allows adding new expenses, displaying expenses, and summarizing total expenses and expenses by category.
  • Adding Expenses: The program allows users to add expenses by providing the name, amount, and category of the expense.
  • Displaying Expenses: It shows the list of all expenses, along with their category and amount.
  • Expense Summary: The program provides a summary showing the total expenses and expenses grouped by category.

How to Run the Program

1. Copy the Python code above into a Python file (e.g., expense_tracker.py).

2. Make sure you have Python installed on your computer.

3. Run the Python script using the command:

python expense_tracker.py

4. The program will display the list of expenses, as well as the summary showing the total expenses and expenses by category.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)