feature/tasks/nested-classes #17
@ -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);
|
||||||
}
|
}
|
||||||
@ -67,7 +67,18 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,50 +4,38 @@ 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", "");
|
// TaskAction action = new TodoApp.TaskAction() {
|
||||||
* Task t2 = new Task("Дело тест - t2", "");
|
// @Override
|
||||||
* Task t3 = new Task("Дело тест - t3", "");
|
// public void execute(Task task) {
|
||||||
* printAll(t1, t2, t3);
|
// if (task.getCompleted())
|
||||||
* System.out.println(markTaskCompletedById(new Task[] { t1, t2, t3 }, 2));
|
// System.out.println("Дело " + task.getTitle() + " уже выполнено ранее.");
|
||||||
* printAll(t1, t2, t3);
|
// else {
|
||||||
*
|
// task.toggleCompleted();
|
||||||
* System.out.println("\n\n" + "*".repeat(20) + "\n\n");
|
// 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) {
|
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("Ошибка: используйте цифры.\nПовторите ввод:");
|
|
||||||
}
|
|
||||||
} while (true);
|
|
||||||
|
|
||||||
for (int i : allowedInts)
|
|
||||||
if (i == input)
|
|
||||||
return input;
|
|
||||||
System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:");
|
|
||||||
} while (true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void displayMainMenu() {
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Task.printTotalTasksCreated();
|
Task.printTotalTasksCreated();
|
||||||
String menu = """
|
String menu = """
|
||||||
@ -94,13 +82,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 +110,6 @@ public class TodoApp {
|
|||||||
|
|
||||||
if (input == 1) {
|
if (input == 1) {
|
||||||
displayCreateTask();
|
displayCreateTask();
|
||||||
displayTasks();
|
|
||||||
} else
|
} else
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
@ -124,10 +121,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 +145,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("Ошибка: используйте цифры.\nПовторите ввод:");
|
||||||
}
|
}
|
||||||
|
} while (true);
|
||||||
|
|
||||||
|
for (int i : allowedInts)
|
||||||
|
if (i == input)
|
||||||
|
return input;
|
||||||
|
System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:");
|
||||||
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printAll(Task... tasks) {
|
private static void printAll(Task... tasks) {
|
||||||
@ -175,4 +183,30 @@ 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user