ASP.NET Hosting

Make a C# Personal Budget Tracker with Console UI and JSON

Keeping track of personal spending is crucial, and creating a budget tracker is a fantastic way for beginners to master C# file handling, serialization, and console user interface. This tutorial explains how to create a basic budget tracker that allows you to enter, view, and delete expenses as well as display your overall spending. It stores data in JSON.

Prerequisites

  • .NET SDK installed
  • Basic C# knowledge
  • Visual Studio or any C# editor

1. Project Setup

dotnet new console -n BudgetTrackerApp
cd BudgetTrackerApp

2. Create the Expense Model (Expense.cs)

public class Expense
{
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal Amount { get; set; }
    public DateTime Date { get; set; }
}

3. Add JSON Storage Helpers (Program.cs)

using System.Text.Json;

public static class BudgetHelper
{
    private const string FilePath = "expenses.json";

    public static List<Expense> LoadExpenses()
    {
        if (!File.Exists(FilePath))
            return new List<Expense>();

        var json = File.ReadAllText(FilePath);
        return JsonSerializer.Deserialize<List<Expense>>(json) ?? new List<Expense>();
    }

    public static void SaveExpenses(List<Expense> expenses)
    {
        var json = JsonSerializer.Serialize(expenses, new JsonSerializerOptions
        {
            WriteIndented = true
        });

        File.WriteAllText(FilePath, json);
    }
}

4. Console Application Logic (Program.cs)

class Program
{
    static List<Expense> expenses = BudgetHelper.LoadExpenses();
    static int idCounter = expenses.Count > 0 ? expenses.Max(e => e.Id) + 1 : 1;

    static void Main()
    {
        while (true)
        {
            Console.Clear();
            Console.WriteLine("=== Personal Budget Tracker ===");
            Console.WriteLine("1. Add Expense");
            Console.WriteLine("2. View Expenses");
            Console.WriteLine("3. Delete Expense");
            Console.WriteLine("4. Total Spent");
            Console.WriteLine("5. Exit");
            Console.Write("Select an option: ");

            var input = Console.ReadLine();

            switch (input)
            {
                case "1": AddExpense(); break;
                case "2": ViewExpenses(); break;
                case "3": DeleteExpense(); break;
                case "4": ShowTotal(); break;
                case "5": return;
                default: Console.WriteLine("Invalid option"); break;
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }

    static void AddExpense()
    {
        Console.Write("Description: ");
        var desc = Console.ReadLine();

        Console.Write("Amount: ");
        decimal amt = decimal.TryParse(Console.ReadLine(), out amt) ? amt : 0;

        expenses.Add(new Expense
        {
            Id = idCounter++,
            Description = desc,
            Amount = amt,
            Date = DateTime.Now
        });

        BudgetHelper.SaveExpenses(expenses);
        Console.WriteLine("Expense added!");
    }

    static void ViewExpenses()
    {
        Console.WriteLine("\n--- Expenses ---");
        foreach (var e in expenses)
        {
            Console.WriteLine($"{e.Id}. {e.Description} - ₹{e.Amount} on {e.Date:dd/MM/yyyy}");
        }
    }

    static void DeleteExpense()
    {
        ViewExpenses();
        Console.Write("Enter ID to delete: ");
        if (int.TryParse(Console.ReadLine(), out int id))
        {
            var item = expenses.FirstOrDefault(e => e.Id == id);
            if (item != null)
            {
                expenses.Remove(item);
                BudgetHelper.SaveExpenses(expenses);
                Console.WriteLine("Deleted!");
            }
            else
            {
                Console.WriteLine("ID not found.");
            }
        }
    }

    static void ShowTotal()
    {
        var total = expenses.Sum(e => e.Amount);
        Console.WriteLine($"\nTotal Spent: ₹{total}");
    }
}

Output Example

Extensions (Optional)

  • Add monthly limits
  • Use categories (e.g., food, rent)
  • Export to CSV
  • Convert to GUI using WPF or MAUI

Conclusion

This simple budget tracker project demonstrates how to use C# and JSON to manage personal finance data. It’s beginner-friendly and provides a strong foundation for learning file I/O, serialization, and structured application logic.

ASP.NET Core 10.0 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.