Skip to content

The Task Tracker Brief

The Course
•
25 min study

Today you put the week together: a task tracker with stable IDs, validated fields, completion, edits, removal and reports. Before opening the code, read the brief carefully. Every rule below exists to prevent a bug you met earlier in the week, and every rule has a test or a mutation check that proves it holds. A brief written this precisely is also what makes the tests possible: "reject bad lines" cannot be tested, while "a line with four fields and minutes of 181 is rejected and counted" can.

The rules for a task

  • ID: 1 through 9999, chosen by the input. IDs are never renumbered: removing task 2 leaves task 3 as 3. While a task exists, no other task can take its ID; once it is removed, the ID is free and may be added again for a new task. Identity is not position (Day 2).
  • Duration: 1 through 180 minutes. Zero-minute tasks would distort the totals (Day 1).
  • State: exactly todo or done, stored as enum class State (Day 2).
  • Title: 1 through 40 printable ASCII characters, with no | and no space at either end. Inner spaces are fine, as in Read chapter.
  • Capacity: at most 1000 tasks. This bounds the worst-case total at 1000 × 180 = 180,000 minutes, which a long holds comfortably.

Why forbid | and outer spaces in titles? A | would make the exported line ambiguous, and outer spaces would make Read and Read two different titles that look identical in a report. Both rules keep the export canonical (Day 6).

The input format and rejection policy

Input is one record per line in the form id|minutes|state|title:

  • Numbers are plain decimal digits. Leading zeros are allowed (0002); signs, spaces inside numbers and empty fields are rejected.
  • Exactly four fields. Too few or too many | separators make the line malformed.
  • CRLF is accepted. The application strips one trailing \r, so text pasted from Windows works.
  • Bad lines do not stop the import. Each rejected line is counted, and reading continues with the next.
  • Duplicates keep the original. A second record with an existing ID is rejected; the first task is unchanged (Day 4).

The separation between malformed and invalid lines comes from Day 4's result records. The application only reports a total rejection count, but the library's tests check each kind.

Output and operations

The application prints a report of all tasks sorted by descending duration, then ascending ID, followed by four summary lines: task count, pending minutes, completed minutes and rejected lines. The library also supports operations that the import program does not expose as commands: edit (validated, all-or-nothing), complete (idempotent), remove (other IDs unchanged), find (returns an owning copy) and export_tasks (canonical, ID order). The tests exercise all of them.

Predict and run

The summary adds pending and completed minutes separately. What does this print?

Predict before running C++
#include <iostream>

int main() {
    long pending = 25L + 25L;
    long completed = 10;
    std::cout << "Pending: " << pending << "\nCompleted: " << completed << "\n";
    return 0;
}
Check your prediction
Text
Pending: 50
Completed: 10

Those are the numbers the sample input produces. Trace it by hand before you run the project in the next lesson:

  • 2|25|todo|Read chapter is accepted.
  • 1|10|done|Review notes is accepted.
  • bad has no separators: malformed, rejected.
  • 2|40|todo|Duplicate reuses ID 2: rejected, and task 2 keeps 25 minutes.
  • 3|25|todo|Practice is accepted.

That gives three tasks, 50 pending minutes, 10 completed and 2 rejected. The report lists task 2 before task 3 because they tie at 25 minutes and 2 is the lower ID. Write down the expected output for an empty input, an input where every task is done, and two tasks of equal duration given in reverse ID order, before you look at the code.

Practice

Exercise

Your turn

Write Summary summarize(const std::vector<Row>& rows) for struct Row { int minutes; bool done; }; and struct Summary { std::size_t tasks; long pending; long completed; };. Print summaries for the sample's three accepted tasks, for an empty list and for the 1000-task worst case (all todo, 180 minutes each):

Text
sample: 3 tasks, 50 pending, 10 completed
empty: 0 tasks, 0 pending, 0 completed
full: 1000 tasks, 180000 pending, 0 completed
Your workspace C++
#include <iostream>
#include <vector>

struct Row { int minutes; bool done; };
struct Summary { std::size_t tasks; long pending; long completed; };

// Write summarize here.

int main() {
    return 0;
}
Hint 1: the idea

Keep completed and pending work in separate sums, and let each row add to exactly one of them. The count is just the number of rows.

Hint 2: a first step

Start with Summary result{rows.size(), 0, 0};, then loop and add task.minutes to result.completed or result.pending. Build the worst case with std::vector<Row>(1000, Row{180, false}).

Show one solution
Run the solution C++
#include <iostream>
#include <vector>

struct Row { int minutes; bool done; };
struct Summary { std::size_t tasks; long pending; long completed; };

Summary summarize(const std::vector<Row>& rows) {
    Summary result{rows.size(), 0, 0};
    for (const Row& task : rows) {
        if (task.done) { result.completed += task.minutes; } else { result.pending += task.minutes; }
    }
    return result;
}

void print(const char* label, const Summary& summary) {
    std::cout << label << ": " << summary.tasks << " tasks, " << summary.pending << " pending, "
              << summary.completed << " completed\n";
}

int main() {
    print("sample", summarize({{25, false}, {10, true}, {25, false}}));
    print("empty", summarize({}));
    print("full", summarize(std::vector<Row>(1000, Row{180, false})));
    return 0;
}
Text
sample: 3 tasks, 50 pending, 10 completed
empty: 0 tasks, 0 pending, 0 completed
full: 1000 tasks, 180000 pending, 0 completed

The empty case gives zeros with no special branch, and the full case reaches the largest total the capacity rule allows, 180,000, which is why the accumulators are long. The project computes the same two sums with its total_minutes_if template and two predicates. The next lesson opens the full project and runs it on the sample input.

Was this article helpful?

Your response is saved on this device.