Personal Expense Tracker

The Personal Expense Tracker is a console-driven application developed in Java to help users manage and track their financial expenditures. The project highlights key concepts such as file handling, user input processing, and working with structured data. Users can log expenses by entering the amount, category, date, and description. The program maintains records in a CSV or text file for persistence. It offers features to view all expenses in a formatted layout, filter/search expenses by category or date range, and generate a summary report that shows total spending and breakdowns. This project simulates personal budgeting tools and helps the developer understand real-life use cases for Java’s I/O and collection frameworks.

The Personal Expense Tracker is a console-based Java application that allows users to manage and monitor their day-to-day financial expenditures. This project emphasizes the importance of structured data storage, file handling, and user-driven input processing. It is an ideal real-world project that helps learners understand how software can simplify personal finance management.

📌 Key Features:

  • Add Expense: Users can log new expenses by entering the amount, category (e.g., food, travel), date, and a short description.

  • View Expenses: Displays all saved expenses in a clean and readable format.

  • Total Summary: Generates a breakdown of total spending by category and/or date, helping users understand their financial patterns.

  • Search/Filter: Allows users to filter expenses by specific categories or a custom date range.

  • Storage: Uses CSV (Comma Separated Values) files or plain text files to store expense records, making the data persistent across sessions.

🔧 Technologies & Concepts Used:

  • Java File Handling (IO/NIO): Reads and writes expense data to a file for long-term storage.

  • Collections (ArrayList): Stores all expense entries dynamically in memory before saving to file.

  • Date Handling: Uses Java’s SimpleDateFormat to manage date input and filter by date ranges.

  • Scanner Class: For user input via the command line.

  • Encapsulation: Each expense is stored as an object with fields like amount, date, category, and description.

🎯 Learning Outcomes:

By building this project, the learner gains:

  • Practical experience in Java file I/O for reading and writing data.

  • Understanding of data structures to manage and organize records.

  • Experience in designing search and filter features based on user criteria.

  • Skills in building interactive, menu-driven applications with real-world utility.

🔄 Future Scope:

The Expense Tracker can be further enhanced by:

  • Adding data visualization (graphs or charts for spending trends).

  • Integrating with databases like SQLite or MySQL for scalable storage.

  • Building a GUI version using JavaFX or Swing.

  • Introducing budget limits and automated alerts for overspending.

  • Supporting data export to Excel or PDF.

✅ Conclusion:

The Personal Expense Tracker project provides a solid foundation for learning how Java can be applied to solve daily life problems. It not only enhances coding skills but also introduces students to software development techniques like file storage, input validation, and data reporting. This project is a perfect showcase of how basic tools in Java can build useful, impactful applications.

BankingSystem.java

import java.io.*;
import java.text.*;
import java.util.*;

class Expense {
    private double amount;
    private String category;
    private String date;
    private String description;

    public Expense(double amount, String category, String date, String description) {
        this.amount = amount;
        this.category = category;
        this.date = date;
        this.description = description;
    }

    public double getAmount() { return amount; }
    public String getCategory() { return category; }
    public String getDate() { return date; }
    public String getDescription() { return description; }

    public String toCSV() {
        return amount + “,” + category + “,” + date + “,” + description;
    }

    public void display() {
        System.out.printf(“₹%.2f | %-10s | %-10s | %s\n”, amount, category, date, description);
    }
}

public class ExpenseTracker {
    private static final String FILE_NAME = “expenses.csv”;
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int choice;
        do {
            System.out.println(“\n💰 Personal Expense Tracker 💰”);
            System.out.println(“1. Add Expense”);
            System.out.println(“2. View All Expenses”);
            System.out.println(“3. Summary Report”);
            System.out.println(“4. Search/Filter Expenses”);
            System.out.println(“0. Exit”);
            System.out.print(“Choose an option: “);
            choice = sc.nextInt();

            switch (choice) {
                case 1 -> addExpense();
                case 2 -> viewExpenses();
                case 3 -> summaryReport();
                case 4 -> searchFilterExpenses();
                case 0 -> System.out.println(“Exiting…”);
                default -> System.out.println(“Invalid option.”);
            }
        } while (choice != 0);
    }

    private static void addExpense() {
        try {
            sc.nextLine(); // Consume newline
            System.out.print(“Enter amount: ₹”);
            double amount = sc.nextDouble();
            sc.nextLine(); // Consume newline

            System.out.print(“Enter category (e.g., Food, Travel): “);
            String category = sc.nextLine();

            System.out.print(“Enter date (yyyy-MM-dd): “);
            String date = sc.nextLine();

            System.out.print(“Enter description: “);
            String description = sc.nextLine();

            Expense exp = new Expense(amount, category, date, description);
            saveToFile(exp);
            System.out.println(“✅ Expense added.”);
        } catch (Exception e) {
            System.out.println(“❌ Error: ” + e.getMessage());
        }
    }

    private static void saveToFile(Expense exp) throws IOException {
        FileWriter fw = new FileWriter(FILE_NAME, true);
        fw.write(exp.toCSV() + “\n”);
        fw.close();
    }

    private static List<Expense> loadExpenses() {
        List<Expense> expenses = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(“,”, 4);
                double amount = Double.parseDouble(parts[0]);
                String category = parts[1];
                String date = parts[2];
                String desc = parts[3];
                expenses.add(new Expense(amount, category, date, desc));
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println(“📂 No expenses recorded yet.”);
        } catch (IOException e) {
            System.out.println(“❌ Error loading file.”);
        }
        return expenses;
    }

    private static void viewExpenses() {
        List<Expense> expenses = loadExpenses();
        if (expenses.isEmpty()) {
            System.out.println(“📭 No expenses to show.”);
            return;
        }
        System.out.println(“\n📋 All Expenses:”);
        for (Expense e : expenses) e.display();
    }

    private static void summaryReport() {
        List<Expense> expenses = loadExpenses();
        if (expenses.isEmpty()) return;

        double total = 0;
        Map<String, Double> categoryMap = new HashMap<>();

        for (Expense e : expenses) {
            total += e.getAmount();
            categoryMap.put(e.getCategory(), categoryMap.getOrDefault(e.getCategory(), 0.0) + e.getAmount());
        }

        System.out.printf(“\n💡 Total Spent: ₹%.2f\n”, total);
        System.out.println(“📊 Category-wise Breakdown:”);
        for (String cat : categoryMap.keySet()) {
            System.out.printf(” – %-10s : ₹%.2f\n”, cat, categoryMap.get(cat));
        }
    }

    private static void searchFilterExpenses() {
        List<Expense> expenses = loadExpenses();
        if (expenses.isEmpty()) return;

        sc.nextLine(); // consume newline
        System.out.println(“1. Filter by Category”);
        System.out.println(“2. Filter by Date Range”);
        System.out.print(“Choose: “);
        int opt = sc.nextInt();

        if (opt == 1) {
            sc.nextLine();
            System.out.print(“Enter category to search: “);
            String cat = sc.nextLine().toLowerCase();
            expenses.stream()
                    .filter(e -> e.getCategory().toLowerCase().equals(cat))
                    .forEach(Expense::display);
        } else if (opt == 2) {
            sc.nextLine();
            System.out.print(“Enter start date (yyyy-MM-dd): “);
            String start = sc.nextLine();
            System.out.print(“Enter end date (yyyy-MM-dd): “);
            String end = sc.nextLine();
            SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

            expenses.stream().filter(e -> {
                try {
                    Date d = sdf.parse(e.getDate());
                    return !d.before(sdf.parse(start)) && !d.after(sdf.parse(end));
                } catch (ParseException ex) {
                    return false;
                }
            }).forEach(Expense::display);
        } else {
            System.out.println(“❌ Invalid choice.”);
        }
    }
}
Scroll to Top