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 107 additions and 62 deletions

View File

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

View File

@ -4,50 +4,38 @@ import java.util.Scanner;
public class TodoApp {
private static TaskManager taskManager;
private static Task[] tasks = new Task[10];
private static int tasksCounter = 0;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
/*
* System.out.println("\n\nDeveloper mode:\n\n");
*
* Task t1 = new Task("Дело тест - t1", "");
* 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");
*/
TodoApp app = new TodoApp();
taskManager = app.new TaskManager();
// TaskAction action = new TodoApp.TaskAction() {
// @Override
// public void execute(Task task) {
// if (task.getCompleted())
// System.out.println("Дело " + task.getTitle() + " уже выполнено ранее.");
// else {
// task.toggleCompleted();
// System.out.println("Дело " + task.getTitle() + " выполнено.");
// }
// }
// };
// Task t1 = new Task("Тестовое дело", "Тестовое дело для проверки.");
// action.execute(t1);
// action.execute(t1);
app.run();
displayMainMenu();
}
private 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);
}
private static void displayMainMenu() {
private void run() {
while (true) {
Task.printTotalTasksCreated();
String menu = """
@ -94,13 +82,23 @@ public class TodoApp {
System.out.print("\nНпишите описание дела: ");
description = scanner.nextLine().trim();
var task = new Task(title, description);
tasks[Task.taskCount - 1] = task;
taskManager.addTask(new Task(title, description));
}
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() {
if (Task.taskCount == 0) {
if (tasksCounter == 0) {
int input = requestIntFromInput("""
Список дел пуст.
@ -112,7 +110,6 @@ public class TodoApp {
if (input == 1) {
displayCreateTask();
displayTasks();
} else
System.exit(0);
}
@ -124,10 +121,10 @@ public class TodoApp {
printAll(tasks);
int[] variantsInput = new int[Task.taskCount + 1];
for (int i = 0; i < Task.taskCount; i++)
int[] variantsInput = new int[tasksCounter + 1];
for (int i = 0; i < tasksCounter; i++)
variantsInput[i] = tasks[i].getId();
variantsInput[Task.taskCount] = 0;
variantsInput[tasksCounter] = 0;
int input = requestIntFromInput("""
@ -148,15 +145,26 @@ public class TodoApp {
return null;
}
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 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);
}
private static void printAll(Task... tasks) {
@ -175,4 +183,30 @@ public class TodoApp {
return false;
}
private class TaskManager {
void addTask(Task task) {
tasks[tasksCounter++] = task;
}
void markCompleted(int id) {
markTaskCompletedById(tasks, id);
}
void printTasks() {
class ShortTaskPrinter {
void print(Task task) {
System.out.printf("%-3s | %s\n", task.getId(), task.getTitle());
}
}
ShortTaskPrinter p = new ShortTaskPrinter();
for (int i = 0; i < Task.taskCount; i++)
p.print(tasks[i]);
}
}
interface TaskAction {
void execute(Task task);
}
}