#C14224. Task Manager Operations

    ID: 43850 Type: Default 1000ms 256MiB

Task Manager Operations

Task Manager Operations

You are required to implement a simple task manager that supports the following operations:

  • ADD: Add a new task to the list. A valid task must include at least a title and a due_date. Each task also contains a description field. If a task misses either title or due_date, print ERROR (without quotes) immediately.
  • COMPLETE: Mark a task as completed, given its title. When a task is marked as completed, add a key completed with value true to that task.
  • DELETE: Delete a task from the list, given its title.
  • FILTER: Filter tasks based on their status. The status is either active (tasks without completed: true) or completed (tasks having completed: true). When a FILTER command is issued, output the matching list of tasks in JSON format. Note that after an ADD operation, the tasks list must be maintained sorted in ascending order of due_date (which follows the format YYYY-MM-DD).

The operations will be provided one per line and should be processed sequentially. Only the FILTER commands (and error messages from ADD commands) produce output. All inputs are read from standard input and outputs should be printed to standard output.

Implementation details:

For sorting tasks, use the fact that the due_date is given in the format \(YYYY-MM-DD\), so lexicographical comparison works correctly. The input for an ADD command is given as a JSON string representing an object with keys title, description, and due_date.

inputFormat

The input begins with an integer \(N\) denoting the number of operations. Each of the following \(N\) lines contains an operation in one of the following formats:

  • ADD <json>: where <json> is a JSON object representing a task. Example:
    ADD {"title": "Task 1", "description": "Do something", "due_date": "2023-10-10"}
  • COMPLETE <title>: marks the task with the given title as completed. Example:
    COMPLETE Task 1
  • DELETE <title>: deletes the task with the given title. Example:
    DELETE Task 1
  • FILTER <status>: where <status> is either active or completed. When this command is processed, output the JSON array (in a single line) of tasks matching the specified status. Example:
    FILTER active

All operations are provided on separate lines from stdin.

outputFormat

Your program should write to stdout. For each FILTER operation, output one line, which is a JSON array of the tasks that match the provided status. In addition, if an ADD operation has missing title or due_date, output a line with the word ERROR. No output is expected for other operations.

## sample
5
ADD {"title": "Task 1", "description": "Do something", "due_date": "2023-10-10"}
ADD {"title": "Task 2", "description": "Do something else", "due_date": "2023-10-11"}
COMPLETE Task 1
FILTER active
FILTER completed
[{"title": "Task 2", "description": "Do something else", "due_date": "2023-10-11"}]

[{"title": "Task 1", "description": "Do something", "due_date": "2023-10-10", "completed": true}]

</p>