diff --git a/todo/src/main/java/ru/kamask/pet/Task.java b/todo/src/main/java/ru/kamask/pet/Task.java index 3c5218b..834ce25 100644 --- a/todo/src/main/java/ru/kamask/pet/Task.java +++ b/todo/src/main/java/ru/kamask/pet/Task.java @@ -6,28 +6,28 @@ class Task { private static int nextId; static int taskCount; - static { - taskCount = 0; - nextId = 0; - } - private int id; private String title; private String description; private boolean completed = false; private LocalDate createdAt; - Task(String title, String description) { - this.title = title; - this.description = description; - }; + static { + taskCount = 0; + nextId = 0; + } { id = ++nextId; createdAt = LocalDate.now(); - ++taskCount; + taskCount++; } + Task(String title, String description) { + this.title = title; + this.description = description; + }; + static void printTotalTasksCreated() { System.out.println("Колличество дел: " + taskCount); } diff --git a/todo/src/main/java/ru/kamask/pet/TodoApp.java b/todo/src/main/java/ru/kamask/pet/TodoApp.java index ff89c79..a18a574 100644 --- a/todo/src/main/java/ru/kamask/pet/TodoApp.java +++ b/todo/src/main/java/ru/kamask/pet/TodoApp.java @@ -4,50 +4,21 @@ import java.util.Scanner; public class TodoApp { + private static TaskManager taskManager; private static Task[] tasks = new Task[10]; + private static int tasksCounter = 0; private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { - /* - * System.out.println("\n\nDeveloper mode:\n\n"); - * - * Task t1 = new Task("Дело тест - t1", ""); - * Task t2 = new Task("Дело тест - t2", ""); - * Task t3 = new Task("Дело тест - t3", ""); - * printAll(t1, t2, t3); - * System.out.println(markTaskCompletedById(new Task[] { t1, t2, t3 }, 2)); - * printAll(t1, t2, t3); - * - * System.out.println("\n\n" + "*".repeat(20) + "\n\n"); - */ + TodoApp app = new TodoApp(); + taskManager = app.new TaskManager(); + + app.run(); - displayMainMenu(); } - private static int requestIntFromInput(String template, int[] allowedInts) { - do { - System.out.print(template); - int input; - do { - if (scanner.hasNextInt()) { - input = scanner.nextInt(); - scanner.nextLine(); - break; - } else { - scanner.next(); - System.out.print("Ошибка: используйте цифры.\nПовторите ввод:"); - } - } while (true); - - for (int i : allowedInts) - if (i == input) - return input; - System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:"); - } while (true); - } - - private static void displayMainMenu() { + private void run() { while (true) { Task.printTotalTasksCreated(); String menu = """ @@ -94,13 +65,23 @@ public class TodoApp { System.out.print("\nНпишите описание дела: "); description = scanner.nextLine().trim(); - var task = new Task(title, description); - tasks[Task.taskCount - 1] = task; + taskManager.addTask(new Task(title, description)); + } + + 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); + } } private static void displayTasks() { - if (Task.taskCount == 0) { + if (tasksCounter == 0) { int input = requestIntFromInput(""" Список дел пуст. @@ -112,7 +93,6 @@ public class TodoApp { if (input == 1) { displayCreateTask(); - displayTasks(); } else System.exit(0); } @@ -124,10 +104,10 @@ public class TodoApp { printAll(tasks); - int[] variantsInput = new int[Task.taskCount + 1]; - for (int i = 0; i < Task.taskCount; i++) + int[] variantsInput = new int[tasksCounter + 1]; + for (int i = 0; i < tasksCounter; i++) variantsInput[i] = tasks[i].getId(); - variantsInput[Task.taskCount] = 0; + variantsInput[tasksCounter] = 0; int input = requestIntFromInput(""" @@ -148,15 +128,26 @@ public class TodoApp { return null; } - 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); - } + private static int requestIntFromInput(String template, int[] allowedInts) { + do { + System.out.print(template); + int input; + do { + if (scanner.hasNextInt()) { + input = scanner.nextInt(); + scanner.nextLine(); + break; + } else { + scanner.next(); + System.out.print("Ошибка: используйте цифры.\nПовторите ввод:"); + } + } while (true); + + for (int i : allowedInts) + if (i == input) + return input; + System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:"); + } while (true); } private static void printAll(Task... tasks) { @@ -175,4 +166,14 @@ public class TodoApp { return false; } + private class TaskManager { + void addTask(Task task) { + tasks[tasksCounter++] = task; + } + + void markCompleted(int id) { + markTaskCompletedById(tasks, id); + } + + } }