Skip to content

Project Brief: Task Tracker

The Course
•
15 min study

Today you'll read, test and extend a complete program that uses this week's ideas: a command-driven task tracker that saves to JSON, reports on overdue work and is checked without using a real clock or disk.

The commands

The tracker reads one command per line.

CommandEffect
add <priority> <yyyy-MM-dd> <title>Adds a task. Priority is Low, Normal or High
done <id>Marks a task done. Repeating this changes nothing
remove <id>Removes a task. Its id isn't used again in that session; after a save and load, the highest id can come back
listOpen tasks first, then by due date, priority and id
reportCounts of open, done and overdue tasks, then open tasks by priority
save <file>Writes JSON, through a temporary file
load <file>Replaces the tasks, only if the file is present and valid
today <yyyy-MM-dd>Fixes the date that report uses, for repeatable runs

The rules

  • Titles hold 1 to 60 characters after trimming.
  • A task due today isn't overdue, and a finished task is never overdue.
  • A bad command prints an Error: line, and later lines still run.
  • After a load, new ids continue from the highest loaded id.

Where does each rule live?

Each part has one job, and each job uses a lesson from this week.

FileJobComes from
Models.csThe data: TaskItem, Priority, ResultDays 1, 2 and 4
Tracker.csRules that change tasksDay 4 results
Reports.csSorted lists and totals, with no changesDay 3 LINQ
Stores.csITaskStore and its JSON implementationDays 1 and 5
Clock.csIClock and its two implementationsDay 6
Commands.csTurns text into calls, and results into textDay 4 patterns
Program.csReads the console and wires it all upDay 6

Only Program.cs touches the console, so everything else can be tested from a check.

Trace it by hand

This small version of the report's rule is worth predicting exactly, because its edge cases are in the brief. Why is each line what it is?

Which tasks are overdue? C#
var today = new DateOnly(2026, 11, 2);
var tasks = new[]
{
    new TaskItem(1, new DateOnly(2026, 11, 1), false),
    new TaskItem(2, new DateOnly(2026, 11, 2), false),
    new TaskItem(3, new DateOnly(2026, 11, 1), true),
    new TaskItem(4, new DateOnly(2026, 11, 3), false),
};

foreach (TaskItem task in tasks.Where(task => !task.Done && task.Due < today))
{
    Console.WriteLine(task.Id);
}
Console.WriteLine($"Open: {tasks.Count(task => !task.Done)}");

record TaskItem(int Id, DateOnly Due, bool Done);
Check your prediction
Text
1
Open: 3

Task 2 is due today, so it isn't late yet. Task 3 is late but done. Task 4 is in the future.

Now the list order. Each sort key only matters when those before it tie, so reading the chain from top to bottom gives the rule.

Predict the order C#
var tasks = new[]
{
    new Row(1, "Low", 0, new DateOnly(2026, 11, 5), false),
    new Row(2, "High", 2, new DateOnly(2026, 11, 5), false),
    new Row(3, "Low", 0, new DateOnly(2026, 11, 1), true),
    new Row(4, "Low", 0, new DateOnly(2026, 11, 9), false),
};

var ordered = tasks
    .OrderBy(task => task.Done)
    .ThenBy(task => task.Due)
    .ThenByDescending(task => task.Rank)
    .ThenBy(task => task.Id);

Console.WriteLine(string.Join(",", ordered.Select(task => task.Id)));

record Row(int Id, string Priority, int Rank, DateOnly Due, bool Done);
Check your prediction
Text
2,1,4,3

false sorts before true, which puts open tasks first. Task 3 is done, so it comes last even though it has the earliest date.

Your turn

Exercise

State the priority rule

The report command prints High, Normal and Low even when a count is 0. Enum.GetValues<Priority>() returns them in declared order, Low first. Write a program with enum Priority { Low, Normal, High } and a dictionary of counts holding only High = 2. Print every priority, highest first, in the form High: 2, using 0 when a count is missing.

When it works, it prints:

Text
High: 2
Normal: 0
Low: 0
Your workspace C#
var counts = new Dictionary<Priority, int> { [Priority.High] = 2 };
Console.WriteLine(counts.Count);

enum Priority { Low, Normal, High }
Hint 1: the idea

Loop over the enum's values, not over the dictionary, because the dictionary lacks the missing ones.

Hint 2: a first step

Print Enum.GetValues<Priority>() in a loop first. LINQ's Reverse flips the order, and GetValueOrDefault deals with the missing counts.

Show one solution
C#
var counts = new Dictionary<Priority, int> { [Priority.High] = 2 };

foreach (Priority priority in Enum.GetValues<Priority>().Reverse())
{
    Console.WriteLine($"{priority}: {counts.GetValueOrDefault(priority)}");
}

enum Priority { Low, Normal, High }
Text
High: 2
Normal: 0
Low: 0

Enum.GetValues<Priority>() lists the values in declaration order, Low first, so Reverse() gives the report's High-to-Low order without a hand-written list that could fall out of step with the enum. GetValueOrDefault prints 0 for a priority with no tasks, so every priority always gets a line.

Was this article helpful?

Your response is saved on this device.