Merge pull request 'feature/tasks/enum-status-and-idgen' (#19) from feature/tasks/enum-status-and-idgen into main

Reviewed-on: #19
This commit is contained in:
KamaSK 2025-05-20 20:52:25 +03:00
commit 276b4caa5e
3 changed files with 174 additions and 77 deletions

View File

@ -3,27 +3,30 @@ package ru.kamask.pet;
import java.time.LocalDate; import java.time.LocalDate;
class Task { class Task {
private static int nextId; static int taskCount = 0;
static int taskCount;
enum TaskIdGenerator {
INSTANCE;
private int currentId = 1;
int nextId() {
return currentId++;
}
}
private int id; private int id;
private String title; private String title;
private String description; private String description;
private boolean completed = false; private TaskStatus status;
private LocalDate createdAt; private LocalDate createdAt;
static { Task(String title, String description) {
taskCount = 0; id = TaskIdGenerator.INSTANCE.nextId();
nextId = 0; status = TaskStatus.NEW;
}
{
id = ++nextId;
createdAt = LocalDate.now(); createdAt = LocalDate.now();
taskCount++; taskCount++;
}
Task(String title, String description) {
this.title = title; this.title = title;
this.description = description; this.description = description;
}; };
@ -32,37 +35,6 @@ class Task {
System.out.println("Колличество дел: " + taskCount); System.out.println("Колличество дел: " + taskCount);
} }
void printInfo() {
String stringCompleted = completed ? "[х]" : "[ ]";
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
}
void printInfo(boolean full) {
if (!full) {
printInfo();
return;
}
String template = """
Номер: %-3d
Дата создания: %s
Статус: %s
Название: %-20s
------------------------------
%s
""";
;
String stringCompleted = completed ? "выполнено" : "не выполнено";
System.out.printf(template, id, createdAt, stringCompleted, title, description);
}
void toggleCompleted() {
completed = !completed;
}
int getId() { int getId() {
return id; return id;
} }
@ -71,13 +43,42 @@ class Task {
return title; return title;
} }
boolean getCompleted() { boolean isCompleted() {
return completed; return status == TaskStatus.COMPLETED;
}
void markCompleted() {
status = TaskStatus.COMPLETED;
}
void markInProgress() {
status = TaskStatus.IN_PROGRESS;
}
void markCanceled() {
status = TaskStatus.CANCELLED;
}
TaskStatus getStatus() {
return status;
} }
static class TaskPrinter { static class TaskPrinter {
void print(Task task) { static void print(Task task) {
task.printInfo(true); String template = """
Номер: %-3d
Дата создания: %s
Статус: %s
Название: %-20s
------------------------------
%s
""";
;
String stringCompleted = task.status.getColorCode() + task.status.getDescription() + "\u001B[0m";
System.out.printf(template, task.id, task.createdAt, stringCompleted, task.title, task.description);
} }
} }

View File

@ -0,0 +1,41 @@
package ru.kamask.pet;
public enum TaskStatus {
NEW("новое") {
@Override
String getColorCode() {
return "\u001B[34m";
}
},
IN_PROGRESS("в работе") {
@Override
String getColorCode() {
return "\u001B[35m";
}
},
COMPLETED("сделано") {
@Override
String getColorCode() {
return "\u001B[32m";
}
},
CANCELLED("отменено") {
@Override
String getColorCode() {
return "\u001B[31m";
}
};
abstract String getColorCode();
private final String description;
TaskStatus(String description) {
this.description = description;
}
String getDescription() {
return description;
}
}

View File

@ -69,14 +69,32 @@ public class TodoApp {
} }
private static void displayTask(Task task) { private static void displayTask(Task task) {
task.printInfo(true); Task.TaskPrinter.print(task);
String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено";
int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:", String firstOption = switch (task.getStatus()) {
new int[] { 0, 1 }); case NEW, CANCELLED -> "Начать";
if (input == 1) { case IN_PROGRESS -> "Выполнено";
task.toggleCompleted(); case COMPLETED -> "Доделать";
displayTask(task); };
int input = requestIntFromInput("[1]" + firstOption + " [2]Отменить [0]Главное меню\n\nВвод:",
new int[] { 0, 1, 2 });
switch (input) {
case 0:
return;
case 1: {
switch (task.getStatus()) {
case NEW, CANCELLED, COMPLETED -> taskManager.markInProgress(task.getId());
case IN_PROGRESS -> taskManager.markCompleted(task.getId());
}
break;
}
case 2:
taskManager.markCanceled(task.getId());
} }
displayTask(task);
} }
private static void displayTasks() { private static void displayTasks() {
@ -102,7 +120,7 @@ public class TodoApp {
"""); """);
printAll(tasks); taskManager.printTasks();
int[] variantsInput = new int[tasksCounter + 1]; int[] variantsInput = new int[tasksCounter + 1];
for (int i = 0; i < tasksCounter; i++) for (int i = 0; i < tasksCounter; i++)
@ -150,20 +168,15 @@ public class TodoApp {
} while (true); } while (true);
} }
private static void printAll(Task... tasks) { interface TaskAction {
for (Task t : tasks) default void execute(Task task) {
if (t != null) };
t.printInfo();
}
private static boolean markTaskCompletedById(Task[] tasks, int id) { default void start(Task task) {
for (Task t : tasks) };
if (t != null && t.getId() == id) {
if (!t.getCompleted()) default void cancel(Task task) {
t.toggleCompleted(); };
return true;
}
return false;
} }
private class TaskManager { private class TaskManager {
@ -171,25 +184,67 @@ public class TodoApp {
tasks[tasksCounter++] = task; tasks[tasksCounter++] = task;
} }
void markCompleted(int id) { boolean markCompleted(int id) {
markTaskCompletedById(tasks, id); for (int i = 0; i < tasksCounter; i++)
if (tasks[i].getId() == id) {
TaskAction action = new TaskAction() {
@Override
public void execute(Task task) {
task.markCompleted();
}
};
action.execute(tasks[i]);
return true;
}
return false;
}
boolean markInProgress(int id) {
for (int i = 0; i < tasksCounter; i++)
if (tasks[i].getId() == id) {
TaskAction action = new TaskAction() {
@Override
public void start(Task task) {
task.markInProgress();
}
};
action.start(tasks[i]);
return true;
}
return false;
}
boolean markCanceled(int id) {
for (int i = 0; i < tasksCounter; i++)
if (tasks[i].getId() == id) {
TaskAction action = new TaskAction() {
@Override
public void cancel(Task task) {
task.markCanceled();
}
};
action.cancel(tasks[i]);
return true;
}
return false;
} }
void printTasks() { void printTasks() {
class ShortTaskPrinter { class ShortTaskPrinter {
void print(Task task) { void print(Task task) {
System.out.printf("%-3s | %s\n", task.getId(), task.getTitle()); int id = task.getId();
String title = task.getTitle();
TaskStatus status = task.getStatus();
String stringCompleted = status.getColorCode() + status.getDescription() + "\u001B[0m";
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
} }
} }
ShortTaskPrinter p = new ShortTaskPrinter(); ShortTaskPrinter printer = new ShortTaskPrinter();
for (int i = 0; i < Task.taskCount; i++) for (int i = 0; i < Task.taskCount; i++)
p.print(tasks[i]); printer.print(tasks[i]);
} }
} }
interface TaskAction {
void execute(Task task);
}
} }