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 95 additions and 73 deletions
Showing only changes of commit acbcbeb340 - Show all commits

View File

@ -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;
}
}

View File

@ -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("Ошибка: используйте цифры.\овторите ввод:");
}
} while (true);
Введите номер пункта меню
-------------------------
[1] Добавить дело
[2] Список дел
[0] Выйти из программы
""";
for (int i : allowedInts)
if (i == input)
return input;
System.out.print("Ошибка: укажите номер выбранного пункта.\овторите ввод:");
} 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("Нпишите название дела (320 символов)");
while (true) {
String description;
do {
System.out.print("\nНпишите название дела (320 символов): ");
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 символов.\опробуйте снова: ");
} 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() {
}
}