feature/add/array_input_logic #13

Merged
KamaSK merged 2 commits from feature/add/array_input_logic into main 2025-05-19 08:34:27 +03:00
2 changed files with 183 additions and 19 deletions

View File

@ -1,23 +1,52 @@
package ru.kamask.pet; package ru.kamask.pet;
public class Task { public class Task {
int id; private int id;
String title; private String title;
String description; private String description;
boolean completed; private boolean completed = false;
Task(String title, String description){ Task(int id, String title, String description) {
this.id = 0; this.id = id;
this.title = title; this.title = title;
this.description = description; this.description = description;
this.completed = false;
} }
void printInfo(){ public void printInfo() {
System.out.println(this.title + ": " + (this.completed ? "выполнена." : "в процессе")); String stringCompleted = completed ? "[х]" : "[ ]";
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
} }
void markAsCompleted(){ public void printInfo(boolean full) {
this.completed = true; if (!full) {
printInfo();
return;
}
String template = """
Номер: %-3d
Статус: %s
Название: %-20s
------------------------------
%s
""";
;
String stringCompleted = completed ? "выполнено" : "не выполнено";
System.out.printf(template, id, stringCompleted, title, description);
}
public void toggleCompleted() {
completed = !completed;
}
public int getId() {
return id;
}
public boolean getCompleted() {
return completed;
} }
} }

View File

@ -1,13 +1,148 @@
package ru.kamask.pet; package ru.kamask.pet;
import java.util.Scanner;
public class TodoApp { 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) { public static void main(String[] args) {
Task t1 = new Task("Первая задача", "Уррааа! Первая задача!"); displayMainMenu();
Task t2 = new Task("Вторя задача", "Ну, вторая уже не первая)))");
t1.printInfo();
t2.printInfo();
t1.markAsCompleted();
t1.printInfo();
t2.printInfo();
} }
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("Ошибка: используйте цифры.\овторите ввод:");
}
} while (true);
for (int i : allowedInts)
if (i == input)
return input;
System.out.print("Ошибка: укажите номер выбранного пункта.\овторите ввод:");
} while (true);
}
public static void displayMainMenu() {
while (true) {
String menu = """
| [1] Добавить дело |
| [2] Список дел |
| |
| [0] Выйти из программы |
Введите номер пункта меню:""";
switch (requestIntFromInput(menu, new int[] { 0, 1, 2 })) {
case 1 -> displayCreateTask();
case 2 -> displayTasks();
case 0 -> {
scanner.close();
System.exit(0);
}
}
}
}
private static void displayCreateTask() {
if (tasksCounter >= 9) {
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
return;
}
int id = nextTaskId++;
String title;
String description;
do {
System.out.print("\nНпишите название дела (320 символов): ");
title = scanner.nextLine().trim();
if (title.length() >= 3 && title.length() <= 20)
break;
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\опробуйте снова: ");
} while (true);
System.out.print("\nНпишите описание дела: ");
description = scanner.nextLine().trim();
var task = new Task(id, title, description);
tasks[tasksCounter++] = task;
}
private static void displayTasks() {
if (tasksCounter < 1) {
int input = requestIntFromInput("""
Список дел пуст.
[1] Добавить дело
[0] Выйти из программы
Введите номер пункта:""", new int[] { 0, 1 });
if (input == 1) {
displayCreateTask();
displayTasks();
} else
System.exit(0);
}
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("""
Введите номер дела или 0 для возврата в меню:""", tasksIDs);
if (input == 0)
return;
displayTask(getTaskById(input));
}
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 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);
}
}
} }