diff --git a/todo/src/main/java/ru/kamask/pet/Task.java b/todo/src/main/java/ru/kamask/pet/Task.java index 3ec962b..40ab7c8 100644 --- a/todo/src/main/java/ru/kamask/pet/Task.java +++ b/todo/src/main/java/ru/kamask/pet/Task.java @@ -3,27 +3,30 @@ package ru.kamask.pet; import java.time.LocalDate; class Task { - private static int nextId; - static int taskCount; + static int taskCount = 0; + + enum TaskIdGenerator { + INSTANCE; + + private int currentId = 1; + + int nextId() { + return currentId++; + } + } private int id; private String title; private String description; - private boolean completed = false; + private TaskStatus status; private LocalDate createdAt; - static { - taskCount = 0; - nextId = 0; - } - - { - id = ++nextId; + Task(String title, String description) { + id = TaskIdGenerator.INSTANCE.nextId(); + status = TaskStatus.NEW; createdAt = LocalDate.now(); taskCount++; - } - Task(String title, String description) { this.title = title; this.description = description; }; @@ -32,37 +35,6 @@ class Task { System.out.println("Колличество дел: " + taskCount); } - void printInfo() { - String stringCompleted = completed ? "[х]" : "[ ]"; - System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted); - } - - void printInfo(boolean full) { - if (!full) { - printInfo(); - return; - } - - String template = """ - - ────────────────────────────── - Номер: %-3d - Дата создания: %s - Статус: %s - Название: %-20s - ------------------------------ - %s - - """; - ; - String stringCompleted = completed ? "выполнено" : "не выполнено"; - System.out.printf(template, id, createdAt, stringCompleted, title, description); - } - - void toggleCompleted() { - completed = !completed; - } - int getId() { return id; } @@ -71,13 +43,42 @@ class Task { return title; } - boolean getCompleted() { - return completed; + boolean isCompleted() { + return status == TaskStatus.COMPLETED; + } + + void markCompleted() { + status = TaskStatus.COMPLETED; + } + + void markInProgress() { + status = TaskStatus.IN_PROGRESS; + } + + void markCanceled() { + status = TaskStatus.CANCELLED; + } + + TaskStatus getStatus() { + return status; } static class TaskPrinter { - void print(Task task) { - task.printInfo(true); + static void print(Task task) { + String template = """ + + ────────────────────────────── + Номер: %-3d + Дата создания: %s + Статус: %s + Название: %-20s + ------------------------------ + %s + + """; + ; + String stringCompleted = task.status.getColorCode() + task.status.getDescription() + "\u001B[0m"; + System.out.printf(template, task.id, task.createdAt, stringCompleted, task.title, task.description); } } diff --git a/todo/src/main/java/ru/kamask/pet/TaskStatus.java b/todo/src/main/java/ru/kamask/pet/TaskStatus.java new file mode 100644 index 0000000..262f244 --- /dev/null +++ b/todo/src/main/java/ru/kamask/pet/TaskStatus.java @@ -0,0 +1,41 @@ +package ru.kamask.pet; + +public enum TaskStatus { + NEW("новое") { + @Override + String getColorCode() { + return "\u001B[34m"; + } + }, + IN_PROGRESS("в работе") { + @Override + String getColorCode() { + return "\u001B[35m"; + } + }, + COMPLETED("сделано") { + @Override + String getColorCode() { + return "\u001B[32m"; + } + }, + CANCELLED("отменено") { + @Override + String getColorCode() { + return "\u001B[31m"; + } + }; + + abstract String getColorCode(); + + private final String description; + + TaskStatus(String description) { + this.description = description; + } + + String getDescription() { + return description; + } + +} diff --git a/todo/src/main/java/ru/kamask/pet/TodoApp.java b/todo/src/main/java/ru/kamask/pet/TodoApp.java index 0a6e3f5..42ba9b7 100644 --- a/todo/src/main/java/ru/kamask/pet/TodoApp.java +++ b/todo/src/main/java/ru/kamask/pet/TodoApp.java @@ -69,14 +69,32 @@ public class TodoApp { } private static void displayTask(Task task) { - task.printInfo(true); - String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено"; - int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:", - new int[] { 0, 1 }); - if (input == 1) { - task.toggleCompleted(); - displayTask(task); + Task.TaskPrinter.print(task); + + String firstOption = switch (task.getStatus()) { + case NEW, CANCELLED -> "Начать"; + case IN_PROGRESS -> "Выполнено"; + case COMPLETED -> "Доделать"; + }; + + int input = requestIntFromInput("[1]" + firstOption + " [2]Отменить [0]Главное меню\n\nВвод:", + new int[] { 0, 1, 2 }); + + switch (input) { + case 0: + return; + case 1: { + switch (task.getStatus()) { + case NEW, CANCELLED, COMPLETED -> taskManager.markInProgress(task.getId()); + case IN_PROGRESS -> taskManager.markCompleted(task.getId()); + } + break; + } + case 2: + taskManager.markCanceled(task.getId()); } + + displayTask(task); } private static void displayTasks() { @@ -102,7 +120,7 @@ public class TodoApp { """); - printAll(tasks); + taskManager.printTasks(); int[] variantsInput = new int[tasksCounter + 1]; for (int i = 0; i < tasksCounter; i++) @@ -150,20 +168,15 @@ public class TodoApp { } while (true); } - private static void printAll(Task... tasks) { - for (Task t : tasks) - if (t != null) - t.printInfo(); - } + interface TaskAction { + default void execute(Task task) { + }; - private static boolean markTaskCompletedById(Task[] tasks, int id) { - for (Task t : tasks) - if (t != null && t.getId() == id) { - if (!t.getCompleted()) - t.toggleCompleted(); - return true; - } - return false; + default void start(Task task) { + }; + + default void cancel(Task task) { + }; } private class TaskManager { @@ -171,25 +184,67 @@ public class TodoApp { tasks[tasksCounter++] = task; } - void markCompleted(int id) { - markTaskCompletedById(tasks, id); + boolean markCompleted(int id) { + for (int i = 0; i < tasksCounter; i++) + if (tasks[i].getId() == id) { + TaskAction action = new TaskAction() { + @Override + public void execute(Task task) { + task.markCompleted(); + } + }; + action.execute(tasks[i]); + return true; + } + return false; + } + + boolean markInProgress(int id) { + for (int i = 0; i < tasksCounter; i++) + if (tasks[i].getId() == id) { + TaskAction action = new TaskAction() { + @Override + public void start(Task task) { + task.markInProgress(); + } + }; + action.start(tasks[i]); + return true; + } + return false; + } + + boolean markCanceled(int id) { + for (int i = 0; i < tasksCounter; i++) + if (tasks[i].getId() == id) { + TaskAction action = new TaskAction() { + @Override + public void cancel(Task task) { + task.markCanceled(); + } + }; + action.cancel(tasks[i]); + return true; + } + return false; } void printTasks() { class ShortTaskPrinter { void print(Task task) { - System.out.printf("%-3s | %s\n", task.getId(), task.getTitle()); + int id = task.getId(); + String title = task.getTitle(); + TaskStatus status = task.getStatus(); + + String stringCompleted = status.getColorCode() + status.getDescription() + "\u001B[0m"; + System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted); } } - ShortTaskPrinter p = new ShortTaskPrinter(); + ShortTaskPrinter printer = new ShortTaskPrinter(); for (int i = 0; i < Task.taskCount; i++) - p.print(tasks[i]); + printer.print(tasks[i]); } } - - interface TaskAction { - void execute(Task task); - } }