Introduction
Managing personal expenses is essential, and building a budget tracker is a great beginner project to practice file handling, serialization, and console UI in C#. This guide walks through creating a simple budget tracker that uses JSON to store data and supports adding, viewing, deleting expenses, and showing total spending.
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
![Output]()
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.