From d36be4f036dfe27700ae2401c04ad8a1ea22e21d Mon Sep 17 00:00:00 2001 From: KamaSK Date: Sat, 17 May 2025 21:21:57 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BC=D0=BC?= =?UTF-8?q?=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- todo/src/main/java/ru/kamask/pet/Task.java | 45 ++++-- todo/src/main/java/ru/kamask/pet/TodoApp.java | 135 ++++++++++++++++-- 2 files changed, 161 insertions(+), 19 deletions(-) diff --git a/todo/src/main/java/ru/kamask/pet/Task.java b/todo/src/main/java/ru/kamask/pet/Task.java index c422cd0..9ef01de 100644 --- a/todo/src/main/java/ru/kamask/pet/Task.java +++ b/todo/src/main/java/ru/kamask/pet/Task.java @@ -1,23 +1,46 @@ package ru.kamask.pet; public class Task { - int id; - String title; - String description; - boolean completed; + private int id; + private String title; + private String description; + private boolean completed = false; - Task(String title, String description){ - this.id = 0; + Task(int id, String title, String description){ + this.id = id; this.title = title; this.description = description; - this.completed = false; } - void printInfo(){ - System.out.println(this.title + ": " + (this.completed ? "выполнена." : "в процессе")); + public void printInfo(){ + String stringCompleted = completed ? "[х]" : "[ ]"; + System.out.printf("%-3d | %s | %-20s\n", id, stringCompleted, title); } - void markAsCompleted(){ - this.completed = true; + public void printInfo(boolean full){ + if (!full){ + printInfo(); + return; + } + + String template = """ + + Номер: %-3d + Статус: %s + Название: %-20s + ------------------------------ + %s + + """;; + String stringCompleted = completed ? "в процессе" : "завершена"; + System.out.printf(template, id, stringCompleted, title, description); + } + + public void markAsCompleted(){ + completed = true; + } + + public int getId(){ + return id; } } diff --git a/todo/src/main/java/ru/kamask/pet/TodoApp.java b/todo/src/main/java/ru/kamask/pet/TodoApp.java index 5738ea6..635f524 100644 --- a/todo/src/main/java/ru/kamask/pet/TodoApp.java +++ b/todo/src/main/java/ru/kamask/pet/TodoApp.java @@ -1,13 +1,132 @@ package ru.kamask.pet; +import java.util.Scanner; + public class TodoApp { + + private static Task[] tasks = new Task[10]; + private static int nextTaskId = 1; + private static int tasksCounter = 0; + private static Scanner scanner = new Scanner(System.in); + public static void main(String[] args) { - Task t1 = new Task("Первая задача", "Уррааа! Первая задача!"); - Task t2 = new Task("Вторя задача", "Ну, вторая уже не первая)))"); - t1.printInfo(); - t2.printInfo(); - t1.markAsCompleted(); - t1.printInfo(); - t2.printInfo(); + mainMenu(); } -} \ No newline at end of file + + public static void mainMenu(){ + String menu = """ + + Введите номер пункта меню + ------------------------- + [1] Добавить дело + [2] Список дел + + [0] Выйти из программы + + """; + + while (true) { + + System.err.printf(menu); + switch (scanner.nextLine().trim()) { + + case "1" -> createTask(); + + case "2" -> listTasks(); + + case "0" -> { + scanner.close(); + System.exit(0); + } + + default -> { + System.out.println("Неизвестная команда. Используйте цифры."); + } + } + } + } + + private static void createTask() { + + if (tasksCounter >= 9) { + System.out.println("Ошибка: Достигнут лимит в 10 дел."); + return; + } + + int id = nextTaskId++; + + String title; + System.out.println("Нпишите название дела (3–20 символов)"); + while (true) { + 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(); + + var task = new Task(id, title, description); + tasks[tasksCounter++] = task; + } + + private static void listTasks() { + + if(tasksCounter < 1){ + System.out.println(""" + + Список дел пуст. + + [1] Добавить дело + [0] Выйти из программы + + """); + + while (true) { + String input = scanner.nextLine().trim(); + if(input.equals("1")) createTask(); + if(input.equals("0")) System.exit(0); + System.out.println("Неизвестная команда. Используйте цифры."); + } + + } + + System.out.println("\nСписок дел:\n"); + + for (int i = 0; i < tasksCounter; i++) { + tasks[i].printInfo(); + } + + System.out.println("Введите номер дела, или 0 для возврата в главное меню:\n"); + + while (true) { + String input = scanner.nextLine().trim(); + if(input.equals("0")) return; + + if(input.matches("\\d+")){ +/* + * + * + */ + } + + System.out.println("Неизвестная команда. Используйте цифры."); + } + } + + public static Task getTaskById(int id) { + for (int i = 0; i < tasksCounter; i++) { + if(tasks[i].getId() == id) return tasks[i]; + } + return null; + } + + public static void taskMenu(Task task){ + + } + + public static void requestInput() { + + } +} From acbcbeb340b89b45db1a420b01beb253bd95841b Mon Sep 17 00:00:00 2001 From: KamaSK Date: Mon, 19 May 2025 08:34:00 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8?= =?UTF-8?q?=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=D0=BC=D0=B8=20=D0=B2=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=D1=85=20Task=20=D0=B8=20To?= =?UTF-8?q?doApp.=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D1=8B=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=D0=B0=D1=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D1=89?= =?UTF-8?q?=D1=8C=D1=8E=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20toggleCom?= =?UTF-8?q?pleted.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- todo/src/main/java/ru/kamask/pet/Task.java | 26 ++-- todo/src/main/java/ru/kamask/pet/TodoApp.java | 142 ++++++++++-------- 2 files changed, 95 insertions(+), 73 deletions(-) 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() { - - } }