Compare commits

..

No commits in common. "276b4caa5ea3a369b4e9a273d5debc8f1a6e9f39" and "5652c8246b8986ec0afb7de3c747af1dd4ab7a4c" have entirely different histories.

3 changed files with 77 additions and 174 deletions

View File

@ -3,30 +3,27 @@ package ru.kamask.pet;
import java.time.LocalDate;
class Task {
static int taskCount = 0;
enum TaskIdGenerator {
INSTANCE;
private int currentId = 1;
int nextId() {
return currentId++;
}
}
private static int nextId;
static int taskCount;
private int id;
private String title;
private String description;
private TaskStatus status;
private boolean completed = false;
private LocalDate createdAt;
Task(String title, String description) {
id = TaskIdGenerator.INSTANCE.nextId();
status = TaskStatus.NEW;
static {
taskCount = 0;
nextId = 0;
}
{
id = ++nextId;
createdAt = LocalDate.now();
taskCount++;
}
Task(String title, String description) {
this.title = title;
this.description = description;
};
@ -35,36 +32,17 @@ class Task {
System.out.println("Колличество дел: " + taskCount);
}
int getId() {
return id;
void printInfo() {
String stringCompleted = completed ? "[х]" : "[ ]";
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
}
String getTitle() {
return title;
void printInfo(boolean full) {
if (!full) {
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 = """
@ -77,8 +55,29 @@ class Task {
""";
;
String stringCompleted = task.status.getColorCode() + task.status.getDescription() + "\u001B[0m";
System.out.printf(template, task.id, task.createdAt, stringCompleted, task.title, task.description);
String stringCompleted = completed ? "выполнено" : "не выполнено";
System.out.printf(template, id, createdAt, stringCompleted, title, 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);
}
}

View File

@ -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;
}
}

View File

@ -69,33 +69,15 @@ public class TodoApp {
}
private static void displayTask(Task task) {
Task.TaskPrinter.print(task);
String firstOption = switch (task.getStatus()) {
case NEW, CANCELLED -> "Начать";
case IN_PROGRESS -> "Выполнено";
case COMPLETED -> "Доделать";
};
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());
}
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() {
@ -120,7 +102,7 @@ public class TodoApp {
""");
taskManager.printTasks();
printAll(tasks);
int[] variantsInput = new int[tasksCounter + 1];
for (int i = 0; i < tasksCounter; i++)
@ -168,15 +150,20 @@ public class TodoApp {
} while (true);
}
interface TaskAction {
default void execute(Task task) {
};
private static void printAll(Task... tasks) {
for (Task t : tasks)
if (t != null)
t.printInfo();
}
default void start(Task task) {
};
default void cancel(Task task) {
};
private static boolean markTaskCompletedById(Task[] tasks, int id) {
for (Task t : tasks)
if (t != null && t.getId() == id) {
if (!t.getCompleted())
t.toggleCompleted();
return true;
}
return false;
}
private class TaskManager {
@ -184,67 +171,25 @@ public class TodoApp {
tasks[tasksCounter++] = task;
}
boolean markCompleted(int 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 markCompleted(int id) {
markTaskCompletedById(tasks, id);
}
void printTasks() {
class ShortTaskPrinter {
void print(Task task) {
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);
System.out.printf("%-3s | %s\n", task.getId(), task.getTitle());
}
}
ShortTaskPrinter printer = new ShortTaskPrinter();
ShortTaskPrinter p = new ShortTaskPrinter();
for (int i = 0; i < Task.taskCount; i++)
printer.print(tasks[i]);
p.print(tasks[i]);
}
}
interface TaskAction {
void execute(Task task);
}
}