Java

 

Introduction

Managing personal finances can often be overwhelming, but having a tool to track and categorize expenses can make it much easier. This expense tracker program is designed to help you keep track of your daily expenses and categorize them for better financial insights. Whether you are trying to save for a goal, monitor your spending habits, or simply stay on top of your finances, this tool will help you manage it all with ease.

Objective

The main objective of this program is to provide a simple, easy-to-use solution for tracking personal expenses. It will allow users to enter various expenses, categorize them, and display a summary of their financial situation based on the categories of spending. By using this tracker, users can better understand where their money is going and make informed decisions for future budgeting.

Java Code for Expense Tracker

import java.util.ArrayList;
import java.util.Scanner;

class Expense {
    String name;
    double amount;
    String category;

    // Constructor to initialize expense details
    public Expense(String name, double amount, String category) {
        this.name = name;
        this.amount = amount;
        this.category = category;
    }
}

public class ExpenseTracker {

    private static ArrayList expenses = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("\nExpense Tracker Menu:");
            System.out.println("1. Add Expense");
            System.out.println("2. View All Expenses");
            System.out.println("3. View Expenses by Category");
            System.out.println("4. Exit");
            System.out.print("Choose an option: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline

            switch (choice) {
                case 1:
                    addExpense();
                    break;
                case 2:
                    viewAllExpenses();
                    break;
                case 3:
                    viewExpensesByCategory();
                    break;
                case 4:
                    System.out.println("Exiting the program. Goodbye!");
                    return;
                default:
                    System.out.println("Invalid option. Please try again.");
            }
        }
    }

    // Method to add an expense
    private static void addExpense() {
        System.out.print("Enter the expense name: ");
        String name = scanner.nextLine();
        System.out.print("Enter the expense amount: ");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // Consume newline
        System.out.print("Enter the category (e.g., Food, Transport, Entertainment): ");
        String category = scanner.nextLine();

        Expense expense = new Expense(name, amount, category);
        expenses.add(expense);
        System.out.println("Expense added successfully!");
    }

    // Method to view all expenses
    private static void viewAllExpenses() {
        if (expenses.isEmpty()) {
            System.out.println("No expenses recorded yet.");
            return;
        }

        System.out.println("\nAll Expenses:");
        for (Expense expense : expenses) {
            System.out.println("Name: " + expense.name + ", Amount: $" + expense.amount + ", Category: " + expense.category);
        }
    }

    // Method to view expenses by category
    private static void viewExpensesByCategory() {
        System.out.print("Enter the category to filter by: ");
        String category = scanner.nextLine();

        boolean found = false;
        System.out.println("\nExpenses in the category '" + category + "':");
        for (Expense expense : expenses) {
            if (expense.category.equalsIgnoreCase(category)) {
                System.out.println("Name: " + expense.name + ", Amount: $" + expense.amount);
                found = true;
            }
        }

        if (!found) {
            System.out.println("No expenses found in this category.");
        }
    }
}

Program Explanation

This Java program allows you to track personal expenses and categorize them into various groups such as Food, Transport, and Entertainment. It uses a simple text-based menu to provide users with options to add expenses, view all expenses, and filter expenses by category.

Program Structure

  • Expense Class: The Expense class defines the attributes of an expense such as the name, amount, and category.
  • ExpenseTracker Class: This is the main class that manages the expenses. It uses an ArrayList to store expenses and provides methods to add, view, and filter them.
  • Methods: The program defines three key methods: addExpense, viewAllExpenses, and viewExpensesByCategory, each of which performs a specific task related to expense tracking.
  • User Interaction: The program uses a menu-based approach, allowing users to interact with the program through console input. The user is prompted to choose an option from the menu and then prompted to input relevant data for the selected option.

How to Run the Program

To run the program, follow these steps:

  1. Copy the Java code into a file named ExpenseTracker.java.
  2. Open a terminal or command prompt and navigate to the directory where the file is located.
  3. Compile the Java program using the following command: javac ExpenseTracker.java
  4. Run the program with the command: java ExpenseTracker
  5. Follow the on-screen prompts to interact with the expense tracker.
© 2025 Learn Programming. All rights reserved.

 

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 :)