Today you assemble a study planner from everything you've learned. It stores tasks and reports planned, completed and remaining study time. Before reading any code, read the brief closely; most bugs start as misunderstood requirements.
The brief
Input is one task per line: a whole number of minutes, a completion flag, then a title, separated by single spaces. For example, 25 0 Read chapter.
| Field | Rule |
|---|---|
| Minutes | a whole number from 1 to 180 |
| Flag | exactly 0 (todo) or 1 (done) |
| Title | 1 to 40 characters after trimming; may contain spaces |
| Tasks | at most 100 |
Invalid lines, and valid ones past the 100-task limit, count as rejected. Later lines must still be processed. An empty line is rejected too. After reading all input, the program prints one line per stored task and then the totals:
Todo: Read chapter (25 min)
Tasks: 1
Planned: 25 minutes
Completed: 0 minutes
Remaining: 25 minutes
Rejected: 0Empty input is valid and gives zero tasks with zero totals.
Trace it by hand
Take this input and write out the complete report on paper before you go on. Which lines are rejected, and by which rule?
25 0 Read chapter10 1 Review notesbad line180 0 Practice
You should find 3 tasks, 215 planned minutes, 10 completed, 205 remaining and 1 rejected. In the next lesson you'll run the real program against this same input.
Derived, not stored
The program only needs to store tasks. Remaining time is worked out from the other two totals, so it can never disagree with them. Predict this small version of the calculation.
var minutes = new List<int> { 25, 10, 180 };
var done = new List<bool> { false, true, false };
int planned = 0;
int completed = 0;
for (int i = 0; i < minutes.Count; i++)
{
planned += minutes[i];
completed += done[i] ? minutes[i] : 0;
}
Console.WriteLine($"Remaining: {planned - completed} minutes");Check your prediction
Remaining: 205 minutesThose two parallel lists work, but they rely on staying the same length and in the same order. A list of records can't get out of step in that way, which is why the project uses one. Explain why hard-coded totals would be fine as expected results in a check, but can't implement a planner that accepts new input.
Your turn
Print the totals
Write PrintTotals(int tasks, int planned, int completed, int rejected), which prints the five report lines from the brief, working out the remaining minutes. Call it with 3, 215, 10, 1, and then check that an empty plan works by calling it with four zeros.
Console.WriteLine("Tasks: 3");Hint 1: the idea
Formatting receives totals that have already been calculated. Only the remaining time is worked out here, and it isn't a parameter.
Hint 2: a first step
Declare a static void method with four int parameters, and print just the Tasks line from it.
Show one solution
PrintTotals(3, 215, 10, 1);
PrintTotals(0, 0, 0, 0);
static void PrintTotals(int tasks, int planned, int completed, int rejected)
{
Console.WriteLine($"Tasks: {tasks}");
Console.WriteLine($"Planned: {planned} minutes");
Console.WriteLine($"Completed: {completed} minutes");
Console.WriteLine($"Remaining: {planned - completed} minutes");
Console.WriteLine($"Rejected: {rejected}");
}Tasks: 3
Planned: 215 minutes
Completed: 10 minutes
Remaining: 205 minutes
Rejected: 1
Tasks: 0
Planned: 0 minutes
Completed: 0 minutes
Remaining: 0 minutes
Rejected: 0PrintTotals takes the planned and completed minutes and works out the remaining minutes itself, so the three totals can't disagree. Calling it with zeros checks the empty-input case from the brief.
Completion is saved in this browser only. It does not measure mastery.