Compare commits

..

No commits in common. "276b4caa5ea3a369b4e9a273d5debc8f1a6e9f39" and "7ca5445791c90c1d2d8ec5873e958cd245f4f73d" have entirely different histories.

4 changed files with 7 additions and 376 deletions

View File

@ -0,0 +1,7 @@
package ru.kamask.pet;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -1,85 +0,0 @@
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 int id;
private String title;
private String description;
private TaskStatus status;
private LocalDate createdAt;
Task(String title, String description) {
id = TaskIdGenerator.INSTANCE.nextId();
status = TaskStatus.NEW;
createdAt = LocalDate.now();
taskCount++;
this.title = title;
this.description = description;
};
static void printTotalTasksCreated() {
System.out.println("Колличество дел: " + taskCount);
}
int getId() {
return id;
}
String getTitle() {
return title;
}
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 = """
Номер: %-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

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

@ -1,250 +0,0 @@
package ru.kamask.pet;
import java.util.Scanner;
public class TodoApp {
private static TaskManager taskManager;
private static Task[] tasks = new Task[10];
private static int tasksCounter = 0;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
TodoApp app = new TodoApp();
taskManager = app.new TaskManager();
app.run();
}
private void run() {
while (true) {
Task.printTotalTasksCreated();
String menu = """
| [1] Добавить дело |
| [2] Список дел |
| |
| [0] Выйти из программы |
Введите номер пункта меню:""";
switch (requestIntFromInput(menu, new int[] { 0, 1, 2 })) {
case 1 -> displayCreateTask();
case 2 -> displayTasks();
case 0 -> {
scanner.close();
System.exit(0);
}
}
}
}
private static void displayCreateTask() {
if (Task.taskCount >= 9) {
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
return;
}
String title;
String description;
do {
System.out.print("\nНапишите название дела (320 символов): ");
title = scanner.nextLine().trim();
if (title.length() >= 3 && title.length() <= 20)
break;
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\опробуйте снова: ");
} while (true);
System.out.print("\nНaпишите описание дела: ");
description = scanner.nextLine().trim();
taskManager.addTask(new Task(title, description));
}
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());
}
displayTask(task);
}
private static void displayTasks() {
if (tasksCounter == 0) {
int input = requestIntFromInput("""
Список дел пуст.
[1] Добавить дело
[0] Выйти из программы
Введите номер пункта:""", new int[] { 0, 1 });
if (input == 1) {
displayCreateTask();
} else
System.exit(0);
}
System.out.println("""
Список дел:
""");
taskManager.printTasks();
int[] variantsInput = new int[tasksCounter + 1];
for (int i = 0; i < tasksCounter; i++)
variantsInput[i] = tasks[i].getId();
variantsInput[tasksCounter] = 0;
int input = requestIntFromInput("""
Введите номер дела или 0 для возврата в меню:""", variantsInput);
if (input == 0)
return;
displayTask(getTaskById(input));
}
private static Task getTaskById(int id) {
for (int i = 0; i < Task.taskCount; i++) {
if (tasks[i].getId() == id)
return tasks[i];
}
return null;
}
private static int requestIntFromInput(String template, int[] allowedInts) {
do {
System.out.print(template);
int input;
do {
if (scanner.hasNextInt()) {
input = scanner.nextInt();
scanner.nextLine();
break;
} else {
scanner.next();
System.out.print("Ошибка: используйте цифры.\овторите ввод:");
}
} while (true);
for (int i : allowedInts)
if (i == input)
return input;
System.out.print("Ошибка: укажите номер выбранного пункта.\овторите ввод:");
} while (true);
}
interface TaskAction {
default void execute(Task task) {
};
default void start(Task task) {
};
default void cancel(Task task) {
};
}
private class TaskManager {
void addTask(Task task) {
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 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);
}
}
ShortTaskPrinter printer = new ShortTaskPrinter();
for (int i = 0; i < Task.taskCount; i++)
printer.print(tasks[i]);
}
}
}