Compare commits
No commits in common. "2104fcdb50009fe6b69612edef8768ea72ce02f6" and "7ca5445791c90c1d2d8ec5873e958cd245f4f73d" have entirely different histories.
2104fcdb50
...
7ca5445791
7
todo/src/main/java/ru/kamask/pet/Main.java
Normal file
7
todo/src/main/java/ru/kamask/pet/Main.java
Normal file
@ -0,0 +1,7 @@
|
||||
package ru.kamask.pet;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
package ru.kamask.pet;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
class Task {
|
||||
private static int nextId;
|
||||
static int taskCount;
|
||||
|
||||
private int id;
|
||||
private String title;
|
||||
private String description;
|
||||
private boolean completed = false;
|
||||
private LocalDate createdAt;
|
||||
|
||||
static {
|
||||
taskCount = 0;
|
||||
nextId = 0;
|
||||
}
|
||||
|
||||
{
|
||||
id = ++nextId;
|
||||
createdAt = LocalDate.now();
|
||||
taskCount++;
|
||||
}
|
||||
|
||||
Task(String title, String description) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
};
|
||||
|
||||
static void printTotalTasksCreated() {
|
||||
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() {
|
||||
return id;
|
||||
}
|
||||
|
||||
String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
boolean getCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
static class TaskPrinter {
|
||||
void print(Task task) {
|
||||
task.printInfo(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,212 +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();
|
||||
|
||||
// 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();
|
||||
|
||||
}
|
||||
|
||||
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Напишите название дела (3–20 символов): ");
|
||||
title = scanner.nextLine().trim();
|
||||
if (title.length() >= 3 && title.length() <= 20)
|
||||
break;
|
||||
|
||||
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: ");
|
||||
} while (true);
|
||||
|
||||
System.out.print("\nНпишите описание дела: ");
|
||||
description = scanner.nextLine().trim();
|
||||
|
||||
taskManager.addTask(new Task(title, description));
|
||||
}
|
||||
|
||||
private static void displayTask(Task task) {
|
||||
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() {
|
||||
|
||||
if (tasksCounter == 0) {
|
||||
int input = requestIntFromInput("""
|
||||
|
||||
Список дел пуст.
|
||||
|
||||
[1] Добавить дело
|
||||
[0] Выйти из программы
|
||||
|
||||
Введите номер пункта:""", new int[] { 0, 1 });
|
||||
|
||||
if (input == 1) {
|
||||
displayCreateTask();
|
||||
} else
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
System.out.println("""
|
||||
Список дел:
|
||||
|
||||
""");
|
||||
|
||||
printAll(tasks);
|
||||
|
||||
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("Ошибка: используйте цифры.\nПовторите ввод:");
|
||||
}
|
||||
} while (true);
|
||||
|
||||
for (int i : allowedInts)
|
||||
if (i == input)
|
||||
return input;
|
||||
System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:");
|
||||
} while (true);
|
||||
}
|
||||
|
||||
private static void printAll(Task... tasks) {
|
||||
for (Task t : tasks)
|
||||
if (t != null)
|
||||
t.printInfo();
|
||||
}
|
||||
|
||||
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 {
|
||||
void addTask(Task task) {
|
||||
tasks[tasksCounter++] = task;
|
||||
}
|
||||
|
||||
void markCompleted(int id) {
|
||||
markTaskCompletedById(tasks, id);
|
||||
}
|
||||
|
||||
void printTasks() {
|
||||
class ShortTaskPrinter {
|
||||
void print(Task task) {
|
||||
System.out.printf("%-3s | %s\n", task.getId(), task.getTitle());
|
||||
}
|
||||
}
|
||||
|
||||
ShortTaskPrinter p = new ShortTaskPrinter();
|
||||
for (int i = 0; i < Task.taskCount; i++)
|
||||
p.print(tasks[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface TaskAction {
|
||||
void execute(Task task);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user