Skip to content

Extend and Download Your Planner

The Course
•
20 min study

A project you can extend is one that you understand. In this lesson you add a pending count to the planner's summary, which means changing a record, a calculation and the report. That's a small version of how most real changes look.

One new field, three places

A record's constructor changes when its fields do. Add a field to Summary and the compiler flags every call that's now wrong, which gives you a list of what to update. This is one of the benefits of a compiled language, so let the errors guide you.

The compiler lists what's left to do C#
var summary = new Summary(2, 35);
Console.WriteLine(summary);

record Summary(int Tasks, int Planned, int Pending);
Check your prediction
Text
error CS7036

Pass a third argument to fix the error.

Your turn

Exercise

Add a pending count

Using the planner below, add a Pending field to Summary so that it counts tasks not yet done. Update Summarize and print a new line, Pending: 2 tasks, after Remaining in Program.cs. For the supplied input you should see two pending tasks, and the rest of the report shouldn't change.

If you also run the checks, they'll need updating, as their new Summary(...) calls have one fewer value than the record now expects. That's the compiler helping you again.

Your workspace C#
#nullable enable using StudyPlanner; var planner = new Planner(); int rejected = 0; string? line; while ((line = Console.ReadLine()) is not null) { StudyTask? parsed = Planner.ParseTask(line); if (parsed is null || !planner.Add(parsed)) { rejected++; } } foreach (StudyTask task in planner.Tasks) { string state = task.Done ? "Done" : "Todo"; Console.WriteLine($"{state}: {task.Title} ({task.Minutes} min)"); } Summary summary = planner.Summarize(); Console.WriteLine($"Tasks: {summary.Tasks}"); Console.WriteLine($"Planned: {summary.Planned} minutes"); Console.WriteLine($"Completed: {summary.Completed} minutes"); Console.WriteLine($"Remaining: {summary.Remaining} minutes"); Console.WriteLine($"Rejected: {rejected}"); #nullable enable namespace StudyPlanner; public record StudyTask(string Title, int Minutes, bool Done); public record Summary(int Tasks, int Planned, int Completed, int Remaining); public class Planner { public const int MaxTasks = 100; public const int MaxMinutes = 180; public const int MaxTitleLength = 40; private readonly List<StudyTask> tasks = new(); public IReadOnlyList<StudyTask> Tasks => tasks; public static bool IsValid(StudyTask task) { return task.Minutes >= 1 && task.Minutes <= MaxMinutes && task.Title.Trim().Length >= 1 && task.Title.Trim().Length <= MaxTitleLength; } // Returns null for a line that does not describe a valid task. public static StudyTask? ParseTask(string line) { string[] parts = line.Split(' ', 3); if (parts.Length != 3 || !int.TryParse(parts[0], out int minutes)) { return null; } if (parts[1] != "0" && parts[1] != "1") { return null; } var task = new StudyTask(parts[2].Trim(), minutes, parts[1] == "1"); return IsValid(task) ? task : null; } public bool Add(StudyTask task) { if (!IsValid(task) || tasks.Count >= MaxTasks) { return false; } tasks.Add(task); return true; } public bool Complete(int index) { if (index < 0 || index >= tasks.Count) { return false; } tasks[index] = tasks[index] with { Done = true }; return true; } public Summary Summarize() { int planned = 0; int completed = 0; foreach (StudyTask task in tasks) { planned += task.Minutes; if (task.Done) { completed += task.Minutes; } } return new Summary(tasks.Count, planned, completed, planned - completed); } } 25 0 Read chapter 10 1 Review notes bad line 180 0 Practice
Hint 1: the idea

Pending is counted in the same loop that adds up the minutes, as a third running value.

Hint 2: a first step

Change the record, and then read what the compiler complains about. The return new Summary(...) line is the first thing to fix.

Show one solution

The change lives in Planner.cs, inside Planner.Summarize, plus one line in Program.cs. So that it can run here, this listing cuts Planner down to the parts Summarize needs and puts everything in one file.

C#
var planner = new Planner();
planner.Add(new StudyTask("Read chapter", 25, false));
planner.Add(new StudyTask("Review notes", 10, true));
planner.Add(new StudyTask("Practice", 180, false));

Summary summary = planner.Summarize();
Console.WriteLine($"Remaining: {summary.Remaining} minutes");
Console.WriteLine($"Pending: {summary.Pending} tasks");

class Planner
{
    private readonly List<StudyTask> tasks = new List<StudyTask>();

    public void Add(StudyTask task)
    {
        tasks.Add(task);
    }

    public Summary Summarize()
    {
        int planned = 0;
        int completed = 0;
        int pending = 0;
        foreach (StudyTask task in tasks)
        {
            planned += task.Minutes;
            if (task.Done)
            {
                completed += task.Minutes;
            }
            else
            {
                pending++;
            }
        }
        return new Summary(tasks.Count, planned, completed, planned - completed, pending);
    }
}

record StudyTask(string Title, int Minutes, bool Done);

record Summary(int Tasks, int Planned, int Completed, int Remaining, int Pending);
Text
Remaining: 205 minutes
Pending: 2 tasks

The new count is added to the loop that already visits every task, in the else of the existing if, so a task is either completed or pending and never both. Adding Pending as a new field at the end of the record means existing code that reads the other fields keeps working.

More ideas, if you want to go further with the project: call Complete before printing the report and see which totals move; add a method that returns only the unfinished tasks; or print the longest task's title. Each one of these needs a new check.

Optional local setup

Everything so far has run in your browser, and you can stop here. To work on the project on your own computer, download the C# study planner. It contains the same source files as the browser tabs, along with a project file, sample input and a README. Your browser edits aren't included, so copy across anything you want to keep.

Install the .NET 10 SDK from dotnet.microsoft.com, open a terminal in the extracted folder and run:

Shell
dotnet --version
dotnet run < sample-input.txt
dotnet run --property:Checks=true

< supplies a file as standard input. In Windows PowerShell, use Get-Content sample-input.txt | dotnet run instead. The project file StudyPlanner.csproj sets up the project, including nullable checking everywhere and the choice of Program.cs or Checks.cs as the starting file.

XML
<Compile Include="Planner.cs" />
<Compile Include="Program.cs" Condition="'$(Checks)' != 'true'" />
<Compile Include="Checks.cs" Condition="'$(Checks)' == 'true'" />

A normal .NET project has one starting file and compiles every .cs file it finds, so you don't normally need these lines. They're here so the application and checks can share one folder, as they do in the browser. Larger projects usually keep tests in a separate project that uses a framework such as xUnit.

The browser runtime has some limits that a local install doesn't: there are no threads, input has to be supplied before Run, and files don't last beyond a single Run. Those are limits of running in a browser, not of C#.

Was this article helpful?

Your response is saved on this device.