Compare commits

..

No commits in common. "2104fcdb50009fe6b69612edef8768ea72ce02f6" and "5776ec51d865e7fec1b8ed778f765318bb17184a" have entirely different histories.

2 changed files with 63 additions and 108 deletions

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

View File

@ -4,38 +4,50 @@ 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(); /*
taskManager = app.new TaskManager(); * System.out.println("\n\nDeveloper mode:\n\n");
*
// TaskAction action = new TodoApp.TaskAction() { * Task t1 = new Task("Дело тест - t1", "");
// @Override * Task t2 = new Task("Дело тест - t2", "");
// public void execute(Task task) { * Task t3 = new Task("Дело тест - t3", "");
// if (task.getCompleted()) * printAll(t1, t2, t3);
// System.out.println("Дело " + task.getTitle() + " уже выполнено ранее."); * System.out.println(markTaskCompletedById(new Task[] { t1, t2, t3 }, 2));
// else { * printAll(t1, t2, t3);
// task.toggleCompleted(); *
// System.out.println("Дело " + task.getTitle() + " выполнено."); * System.out.println("\n\n" + "*".repeat(20) + "\n\n");
// } */
// }
// };
// Task t1 = new Task("Тестовое дело", "Тестовое дело для проверки.");
// action.execute(t1);
// action.execute(t1);
app.run();
displayMainMenu();
} }
private void run() { 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() {
while (true) { while (true) {
Task.printTotalTasksCreated(); Task.printTotalTasksCreated();
String menu = """ String menu = """
@ -82,23 +94,13 @@ public class TodoApp {
System.out.print("\nНпишите описание дела: "); System.out.print("\nНпишите описание дела: ");
description = scanner.nextLine().trim(); description = scanner.nextLine().trim();
taskManager.addTask(new Task(title, description)); var task = 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 (tasksCounter == 0) { if (Task.taskCount == 0) {
int input = requestIntFromInput(""" int input = requestIntFromInput("""
Список дел пуст. Список дел пуст.
@ -110,6 +112,7 @@ public class TodoApp {
if (input == 1) { if (input == 1) {
displayCreateTask(); displayCreateTask();
displayTasks();
} else } else
System.exit(0); System.exit(0);
} }
@ -121,10 +124,10 @@ public class TodoApp {
printAll(tasks); printAll(tasks);
int[] variantsInput = new int[tasksCounter + 1]; int[] variantsInput = new int[Task.taskCount + 1];
for (int i = 0; i < tasksCounter; i++) for (int i = 0; i < Task.taskCount; i++)
variantsInput[i] = tasks[i].getId(); variantsInput[i] = tasks[i].getId();
variantsInput[tasksCounter] = 0; variantsInput[Task.taskCount] = 0;
int input = requestIntFromInput(""" int input = requestIntFromInput("""
@ -145,26 +148,15 @@ public class TodoApp {
return null; return null;
} }
private static int requestIntFromInput(String template, int[] allowedInts) { private static void displayTask(Task task) {
do { task.printInfo(true);
System.out.print(template); String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено";
int input; int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:",
do { new int[] { 0, 1 });
if (scanner.hasNextInt()) { if (input == 1) {
input = scanner.nextInt(); task.toggleCompleted();
scanner.nextLine(); displayTask(task);
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) {
@ -183,30 +175,4 @@ public class TodoApp {
return false; 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);
}
} }