C#  

Build a Daily Mood Tracker in C# Console App with JSON Storage

Introduction

Tracking your daily mood can improve self-awareness and mental wellness. In this article, we’ll build a simple console-based Mood Tracker in C# using JSON to store daily entries. This is a beginner-friendly project perfect for learning file handling, DateTime usage, and basic CRUD operations.

Prerequisites

  • Basic understanding of C#
  • .NET SDK installed (v6+ recommended)
  • Any code editor (like Visual Studio or VS Code)

Project Features

  • Add today’s mood and note
  • View all logged moods
  • Edit an entry by date
  • Delete a mood entry
  • Store data locally in a JSON file

Step 1. Create the Mood Model

public class MoodEntry

{

    public DateTime Date { get; set; }
    public string Mood { get; set; }
    public string Note { get; set; }

}

Step 2. Create a JSON Helper Class

using System.Text.Json;

public static class MoodStorage
{
    private const string FileName = "moods.json";

    public static List<MoodEntry> Load()
    {
        if (!File.Exists(FileName))
            return new List<MoodEntry>();

        string json = File.ReadAllText(FileName);
        return JsonSerializer.Deserialize<List<MoodEntry>>(json) ?? new List<MoodEntry>();
    }

    public static void Save(List<MoodEntry> entries)
    {
        string json = JsonSerializer.Serialize(entries, new JsonSerializerOptions { WriteIndented = true });
        File.WriteAllText(FileName, json);
    }
}

Step 3. Main Program Logic

class Program
{
    static void Main()
    {
        List<MoodEntry> entries = MoodStorage.Load();

        while (true)
        {
            Console.WriteLine("\nMood Tracker");
            Console.WriteLine("1. Add Mood");
            Console.WriteLine("2. View Moods");
            Console.WriteLine("3. Edit Mood");
            Console.WriteLine("4. Delete Mood");
            Console.WriteLine("5. Exit");
            Console.Write("Select option: ");

            switch (Console.ReadLine())
            {
                case "1": AddMood(entries); break;
                case "2": ViewMoods(entries); break;
                case "3": EditMood(entries); break;
                case "4": DeleteMood(entries); break;
                case "5": return;
                default: Console.WriteLine("Invalid choice."); break;
            }
        }
    }

    static void AddMood(List<MoodEntry> entries)
    {
        Console.Write("Enter mood: ");
        string mood = Console.ReadLine();

        Console.Write("Add note (optional): ");
        string note = Console.ReadLine();

        var entry = new MoodEntry
        {
            Date = DateTime.Today,
            Mood = mood,
            Note = note
        };

        entries.Add(entry);
        MoodStorage.Save(entries);
        Console.WriteLine("Mood added successfully.");
    }

    static void ViewMoods(List<MoodEntry> entries)
    {
        if (!entries.Any())
        {
            Console.WriteLine("No entries found.");
            return;
        }

        foreach (var entry in entries.OrderBy(e => e.Date))
        {
            Console.WriteLine($"\nDate: {entry.Date:yyyy-MM-dd}");
            Console.WriteLine($"Mood: {entry.Mood}");
            Console.WriteLine($"Note: {entry.Note}");
        }
    }

    static void EditMood(List<MoodEntry> entries)
    {
        Console.Write("Enter date to edit (yyyy-MM-dd): ");
        if (!DateTime.TryParse(Console.ReadLine(), out DateTime date))
        {
            Console.WriteLine("Invalid date.");
            return;
        }

        var entry = entries.FirstOrDefault(e => e.Date.Date == date.Date);
        if (entry == null)
        {
            Console.WriteLine("Entry not found.");
            return;
        }

        Console.Write("New mood: ");
        entry.Mood = Console.ReadLine();

        Console.Write("New note: ");
        entry.Note = Console.ReadLine();

        MoodStorage.Save(entries);
        Console.WriteLine("Entry updated.");
    }

    static void DeleteMood(List<MoodEntry> entries)
    {
        Console.Write("Enter date to delete (yyyy-MM-dd): ");
        if (!DateTime.TryParse(Console.ReadLine(), out DateTime date))
        {
            Console.WriteLine("Invalid date.");
            return;
        }

        var entry = entries.FirstOrDefault(e => e.Date.Date == date.Date);
        if (entry == null)
        {
            Console.WriteLine("Entry not found.");
            return;
        }

        entries.Remove(entry);
        MoodStorage.Save(entries);
        Console.WriteLine("Entry deleted.");
    }
}

Mood Tracker