Compare commits
No commits in common. "276b4caa5ea3a369b4e9a273d5debc8f1a6e9f39" and "5652c8246b8986ec0afb7de3c747af1dd4ab7a4c" have entirely different histories.
276b4caa5e
...
5652c8246b
@ -3,30 +3,27 @@ package ru.kamask.pet;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
class Task {
|
class Task {
|
||||||
static int taskCount = 0;
|
private static int nextId;
|
||||||
|
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 TaskStatus status;
|
private boolean completed = false;
|
||||||
private LocalDate createdAt;
|
private LocalDate createdAt;
|
||||||
|
|
||||||
Task(String title, String description) {
|
static {
|
||||||
id = TaskIdGenerator.INSTANCE.nextId();
|
taskCount = 0;
|
||||||
status = TaskStatus.NEW;
|
nextId = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
@ -35,6 +32,37 @@ 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;
|
||||||
}
|
}
|
||||||
@ -43,42 +71,13 @@ class Task {
|
|||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isCompleted() {
|
boolean getCompleted() {
|
||||||
return status == TaskStatus.COMPLETED;
|
return 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 {
|
||||||
static void print(Task task) {
|
void print(Task task) {
|
||||||
String template = """
|
task.printInfo(true);
|
||||||
|
|
||||||
──────────────────────────────
|
|
||||||
Номер: %-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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,41 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -69,32 +69,14 @@ public class TodoApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void displayTask(Task task) {
|
private static void displayTask(Task task) {
|
||||||
Task.TaskPrinter.print(task);
|
task.printInfo(true);
|
||||||
|
String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено";
|
||||||
String firstOption = switch (task.getStatus()) {
|
int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:",
|
||||||
case NEW, CANCELLED -> "Начать";
|
new int[] { 0, 1 });
|
||||||
case IN_PROGRESS -> "Выполнено";
|
if (input == 1) {
|
||||||
case COMPLETED -> "Доделать";
|
task.toggleCompleted();
|
||||||
};
|
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() {
|
||||||
@ -120,7 +102,7 @@ public class TodoApp {
|
|||||||
|
|
||||||
""");
|
""");
|
||||||
|
|
||||||
taskManager.printTasks();
|
printAll(tasks);
|
||||||
|
|
||||||
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++)
|
||||||
@ -168,15 +150,20 @@ public class TodoApp {
|
|||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TaskAction {
|
private static void printAll(Task... tasks) {
|
||||||
default void execute(Task task) {
|
for (Task t : tasks)
|
||||||
};
|
if (t != null)
|
||||||
|
t.printInfo();
|
||||||
|
}
|
||||||
|
|
||||||
default void start(Task task) {
|
private static boolean markTaskCompletedById(Task[] tasks, int id) {
|
||||||
};
|
for (Task t : tasks)
|
||||||
|
if (t != null && t.getId() == id) {
|
||||||
default void cancel(Task task) {
|
if (!t.getCompleted())
|
||||||
};
|
t.toggleCompleted();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TaskManager {
|
private class TaskManager {
|
||||||
@ -184,67 +171,25 @@ public class TodoApp {
|
|||||||
tasks[tasksCounter++] = task;
|
tasks[tasksCounter++] = task;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean markCompleted(int id) {
|
void markCompleted(int id) {
|
||||||
for (int i = 0; i < tasksCounter; i++)
|
markTaskCompletedById(tasks, id);
|
||||||
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) {
|
||||||
int id = task.getId();
|
System.out.printf("%-3s | %s\n", task.getId(), task.getTitle());
|
||||||
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 printer = new ShortTaskPrinter();
|
ShortTaskPrinter p = new ShortTaskPrinter();
|
||||||
for (int i = 0; i < Task.taskCount; i++)
|
for (int i = 0; i < Task.taskCount; i++)
|
||||||
printer.print(tasks[i]);
|
p.print(tasks[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TaskAction {
|
||||||
|
void execute(Task task);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user