Compare commits
9
Commits
e0275f14e6
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
276b4caa5e | ||
|
|
a257a76f37 | ||
|
|
2db26eb77a | ||
|
|
1932cdb297 | ||
|
|
5652c8246b | ||
|
|
c0ee62a58e | ||
|
|
2104fcdb50 | ||
|
|
8fadb464b1 | ||
|
|
27be75bade |
@@ -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,17 +35,36 @@ class Task {
|
|||||||
System.out.println("Колличество дел: " + taskCount);
|
System.out.println("Колличество дел: " + taskCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printInfo() {
|
int getId() {
|
||||||
String stringCompleted = completed ? "[х]" : "[ ]";
|
return id;
|
||||||
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printInfo(boolean full) {
|
String getTitle() {
|
||||||
if (!full) {
|
return title;
|
||||||
printInfo();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean isCompleted() {
|
||||||
|
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 void print(Task task) {
|
||||||
String template = """
|
String template = """
|
||||||
|
|
||||||
──────────────────────────────
|
──────────────────────────────
|
||||||
@@ -55,29 +77,8 @@ class Task {
|
|||||||
|
|
||||||
""";
|
""";
|
||||||
;
|
;
|
||||||
String stringCompleted = completed ? "выполнено" : "не выполнено";
|
String stringCompleted = task.status.getColorCode() + task.status.getDescription() + "\u001B[0m";
|
||||||
System.out.printf(template, id, createdAt, stringCompleted, title, description);
|
System.out.printf(template, task.id, task.createdAt, stringCompleted, task.title, task.description);
|
||||||
}
|
|
||||||
|
|
||||||
void toggleCompleted() {
|
|
||||||
completed = !completed;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean getCompleted() {
|
|
||||||
return completed;
|
|
||||||
}
|
|
||||||
|
|
||||||
static class TaskPrinter {
|
|
||||||
void print(Task task) {
|
|
||||||
task.printInfo(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,23 +14,6 @@ public class TodoApp {
|
|||||||
TodoApp app = new TodoApp();
|
TodoApp app = new TodoApp();
|
||||||
taskManager = app.new TaskManager();
|
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();
|
app.run();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -79,21 +62,39 @@ public class TodoApp {
|
|||||||
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: ");
|
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: ");
|
||||||
} while (true);
|
} while (true);
|
||||||
|
|
||||||
System.out.print("\nНпишите описание дела: ");
|
System.out.print("\nНaпишите описание дела: ");
|
||||||
description = scanner.nextLine().trim();
|
description = scanner.nextLine().trim();
|
||||||
|
|
||||||
taskManager.addTask(new Task(title, description));
|
taskManager.addTask(new Task(title, description));
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
@@ -119,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++)
|
||||||
@@ -167,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 {
|
||||||
@@ -188,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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user