diff --git a/todo/src/main/java/ru/kamask/pet/Task.java b/todo/src/main/java/ru/kamask/pet/Task.java index 9ef01de..6672d29 100644 --- a/todo/src/main/java/ru/kamask/pet/Task.java +++ b/todo/src/main/java/ru/kamask/pet/Task.java @@ -6,41 +6,47 @@ public class Task { private String description; private boolean completed = false; - Task(int id, String title, String description){ + Task(int id, String title, String description) { this.id = id; this.title = title; this.description = description; } - public void printInfo(){ + public void printInfo() { String stringCompleted = completed ? "[х]" : "[ ]"; - System.out.printf("%-3d | %s | %-20s\n", id, stringCompleted, title); + System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted); } - public void printInfo(boolean full){ - if (!full){ + public void printInfo(boolean full) { + if (!full) { printInfo(); return; } String template = """ + ────────────────────────────── Номер: %-3d Статус: %s Название: %-20s ------------------------------ %s - """;; - String stringCompleted = completed ? "в процессе" : "завершена"; + """; + ; + String stringCompleted = completed ? "выполнено" : "не выполнено"; System.out.printf(template, id, stringCompleted, title, description); } - public void markAsCompleted(){ - completed = true; + public void toggleCompleted() { + completed = !completed; } - public int getId(){ + public int getId() { return id; } + + public boolean getCompleted() { + return completed; + } } diff --git a/todo/src/main/java/ru/kamask/pet/TodoApp.java b/todo/src/main/java/ru/kamask/pet/TodoApp.java index 635f524..a9a3a55 100644 --- a/todo/src/main/java/ru/kamask/pet/TodoApp.java +++ b/todo/src/main/java/ru/kamask/pet/TodoApp.java @@ -10,123 +10,139 @@ public class TodoApp { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { - mainMenu(); + displayMainMenu(); } - public static void mainMenu(){ - String menu = """ + public 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); - Введите номер пункта меню - ------------------------- - [1] Добавить дело - [2] Список дел - - [0] Выйти из программы - - """; + for (int i : allowedInts) + if (i == input) + return input; + System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:"); + } while (true); + } + public static void displayMainMenu() { while (true) { + String menu = """ - System.err.printf(menu); - switch (scanner.nextLine().trim()) { + ┌──────────────────────────┐ + | [1] Добавить дело | + | [2] Список дел | + | | + | [0] Выйти из программы | + └──────────────────────────┘ - case "1" -> createTask(); + Введите номер пункта меню:"""; - case "2" -> listTasks(); - - case "0" -> { + switch (requestIntFromInput(menu, new int[] { 0, 1, 2 })) { + case 1 -> displayCreateTask(); + case 2 -> displayTasks(); + case 0 -> { scanner.close(); System.exit(0); } - - default -> { - System.out.println("Неизвестная команда. Используйте цифры."); - } } } } - private static void createTask() { + private static void displayCreateTask() { if (tasksCounter >= 9) { - System.out.println("Ошибка: Достигнут лимит в 10 дел."); + System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n"); return; } int id = nextTaskId++; - String title; - System.out.println("Нпишите название дела (3–20 символов)"); - while (true) { + String description; + + do { + System.out.print("\nНпишите название дела (3–20 символов): "); title = scanner.nextLine().trim(); if (title.length() >= 3 && title.length() <= 20) break; - System.out.println("Ошибка: Название должно содержать от 3 до 20 символов. Попробуйте снова."); - } - System.out.println("Нпишите описание дела"); - String description = scanner.nextLine().trim(); + System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: "); + } while (true); + + System.out.print("\nНпишите описание дела: "); + description = scanner.nextLine().trim(); var task = new Task(id, title, description); tasks[tasksCounter++] = task; } - private static void listTasks() { + private static void displayTasks() { - if(tasksCounter < 1){ - System.out.println(""" - - Список дел пуст. + if (tasksCounter < 1) { + int input = requestIntFromInput(""" - [1] Добавить дело - [0] Выйти из программы + Список дел пуст. - """); + [1] Добавить дело + [0] Выйти из программы - while (true) { - String input = scanner.nextLine().trim(); - if(input.equals("1")) createTask(); - if(input.equals("0")) System.exit(0); - System.out.println("Неизвестная команда. Используйте цифры."); - } + Введите номер пункта:""", new int[] { 0, 1 }); + if (input == 1) { + displayCreateTask(); + displayTasks(); + } else + System.exit(0); } - System.out.println("\nСписок дел:\n"); + int[] tasksIDs = new int[tasksCounter + 1]; + System.out.println(""" + Список дел: + """); for (int i = 0; i < tasksCounter; i++) { tasks[i].printInfo(); + tasksIDs[i] = tasks[i].getId(); } + tasksIDs[tasksCounter] = 0; + int input = requestIntFromInput(""" - System.out.println("Введите номер дела, или 0 для возврата в главное меню:\n"); + Введите номер дела или 0 для возврата в меню:""", tasksIDs); - while (true) { - String input = scanner.nextLine().trim(); - if(input.equals("0")) return; + if (input == 0) + return; - if(input.matches("\\d+")){ -/* - * - * - */ - } + displayTask(getTaskById(input)); - System.out.println("Неизвестная команда. Используйте цифры."); - } } public static Task getTaskById(int id) { for (int i = 0; i < tasksCounter; i++) { - if(tasks[i].getId() == id) return tasks[i]; + if (tasks[i].getId() == id) + return tasks[i]; } return null; } - public static void taskMenu(Task task){ - + public 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); + } } - public static void requestInput() { - - } }