feature/tasks/nested-classes #17

Merged
KamaSK merged 11 commits from feature/tasks/nested-classes into main 2025-05-19 21:10:52 +03:00
2 changed files with 63 additions and 62 deletions
Showing only changes of commit 57ae7e6bcb - Show all commits

View File

@ -6,28 +6,28 @@ class Task {
private static int nextId; private static int nextId;
static int taskCount; static int taskCount;
static {
taskCount = 0;
nextId = 0;
}
private int id; private int id;
private String title; private String title;
private String description; private String description;
private boolean completed = false; private boolean completed = false;
private LocalDate createdAt; private LocalDate createdAt;
Task(String title, String description) { static {
this.title = title; taskCount = 0;
this.description = description; nextId = 0;
}; }
{ {
id = ++nextId; id = ++nextId;
createdAt = LocalDate.now(); createdAt = LocalDate.now();
++taskCount; taskCount++;
} }
Task(String title, String description) {
this.title = title;
this.description = description;
};
static void printTotalTasksCreated() { static void printTotalTasksCreated() {
System.out.println("Колличество дел: " + taskCount); System.out.println("Колличество дел: " + taskCount);
} }

View File

@ -4,50 +4,21 @@ import java.util.Scanner;
public class TodoApp { public class TodoApp {
private static TaskManager taskManager;
private static Task[] tasks = new Task[10]; private static Task[] tasks = new Task[10];
private static int tasksCounter = 0;
private static Scanner scanner = new Scanner(System.in); private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) { public static void main(String[] args) {
/* TodoApp app = new TodoApp();
* System.out.println("\n\nDeveloper mode:\n\n"); taskManager = app.new TaskManager();
*
* Task t1 = new Task("Дело тест - t1", ""); app.run();
* 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");
*/
displayMainMenu();
} }
private static int requestIntFromInput(String template, int[] allowedInts) { private void run() {
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);
}
private static void displayMainMenu() {
while (true) { while (true) {
Task.printTotalTasksCreated(); Task.printTotalTasksCreated();
String menu = """ String menu = """
@ -94,13 +65,23 @@ public class TodoApp {
System.out.print("\nНпишите описание дела: "); System.out.print("\nНпишите описание дела: ");
description = scanner.nextLine().trim(); description = scanner.nextLine().trim();
var task = new Task(title, description); taskManager.addTask(new Task(title, description));
tasks[Task.taskCount - 1] = task; }
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() { private static void displayTasks() {
if (Task.taskCount == 0) { if (tasksCounter == 0) {
int input = requestIntFromInput(""" int input = requestIntFromInput("""
Список дел пуст. Список дел пуст.
@ -112,7 +93,6 @@ public class TodoApp {
if (input == 1) { if (input == 1) {
displayCreateTask(); displayCreateTask();
displayTasks();
} else } else
System.exit(0); System.exit(0);
} }
@ -124,10 +104,10 @@ public class TodoApp {
printAll(tasks); printAll(tasks);
int[] variantsInput = new int[Task.taskCount + 1]; int[] variantsInput = new int[tasksCounter + 1];
for (int i = 0; i < Task.taskCount; i++) for (int i = 0; i < tasksCounter; i++)
variantsInput[i] = tasks[i].getId(); variantsInput[i] = tasks[i].getId();
variantsInput[Task.taskCount] = 0; variantsInput[tasksCounter] = 0;
int input = requestIntFromInput(""" int input = requestIntFromInput("""
@ -148,15 +128,26 @@ public class TodoApp {
return null; return null;
} }
private static void displayTask(Task task) { private static int requestIntFromInput(String template, int[] allowedInts) {
task.printInfo(true); do {
String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено"; System.out.print(template);
int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:", int input;
new int[] { 0, 1 }); do {
if (input == 1) { if (scanner.hasNextInt()) {
task.toggleCompleted(); input = scanner.nextInt();
displayTask(task); 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);
} }
private static void printAll(Task... tasks) { private static void printAll(Task... tasks) {
@ -175,4 +166,14 @@ public class TodoApp {
return false; return false;
} }
private class TaskManager {
void addTask(Task task) {
tasks[tasksCounter++] = task;
}
void markCompleted(int id) {
markTaskCompletedById(tasks, id);
}
}
} }