import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static final class Task {
        private final String title;
        private boolean done;

        Task(String title) {
            if (title == null || title.isBlank()) {
                throw new IllegalArgumentException("Title cannot be blank");
            }
            this.title = title.trim();
        }

        void finish() { done = true; }
        String summary() { return (done ? "[x] " : "[ ] ") + title; }
        boolean isDone() { return done; }
    }

    static void check(boolean condition) {
        if (!condition) throw new IllegalStateException("Check failed");
    }

    static void selfTest() {
        Task task = new Task("  Read chapter 1  ");
        check(task.summary().equals("[ ] Read chapter 1"));
        check(!task.isDone());
        task.finish();
        task.finish();
        check(task.isDone());
        try {
            new Task("  ");
            throw new IllegalStateException("Blank title accepted");
        } catch (IllegalArgumentException expected) {
            // Expected invalid input.
        }
        System.out.println("Java tasks: all checks passed");
    }

    public static void main(String[] args) {
        if (args.length == 1 && args[0].equals("--self-test")) {
            selfTest();
            return;
        }
        List<Task> tasks = new ArrayList<>();
        System.out.println("Commands: add TITLE | list | done NUMBER | exit");
        try (Scanner scanner = new Scanner(System.in)) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (line.equals("exit")) break;
                try {
                    if (line.startsWith("add ")) {
                        tasks.add(new Task(line.substring(4)));
                    } else if (line.equals("list")) {
                        for (int i = 0; i < tasks.size(); i++) {
                            System.out.println((i + 1) + ". " + tasks.get(i).summary());
                        }
                    } else if (line.startsWith("done ")) {
                        int index = Integer.parseInt(line.substring(5).trim()) - 1;
                        if (index < 0 || index >= tasks.size()) {
                            throw new IllegalArgumentException("Task number not found");
                        }
                        tasks.get(index).finish();
                    } else {
                        System.out.println("Unknown command");
                    }
                } catch (IllegalArgumentException error) {
                    System.out.println("Error: " + error.getMessage());
                }
            }
        }
    }
}
