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:
- Copy the Java code into a file named ExpenseTracker.java.
- Open a terminal or command prompt and navigate to the directory where the file is located.
- Compile the Java program using the following command:
javac ExpenseTracker.java
- Run the program with the command:
java ExpenseTracker
- Follow the on-screen prompts to interact with the expense tracker.