Compare commits

...

4 Commits

2 changed files with 67 additions and 16 deletions

View File

@ -1,15 +1,35 @@
package ru.kamask.pet; package ru.kamask.pet;
import java.time.LocalDate;
class Task { class Task {
private static int nextId;
static int taskCount;
static {
taskCount = 0;
nextId = 0;
}
private int id; private int id;
private String title; private String title;
private String description; private String description;
private boolean completed = false; private boolean completed = false;
private LocalDate createdAt;
Task(int id, String title, String description) { Task(String title, String description) {
this.id = id;
this.title = title; this.title = title;
this.description = description; this.description = description;
};
{
id = ++nextId;
createdAt = LocalDate.now();
++taskCount;
}
static void printTotalTasksCreated() {
System.out.println("Колличество дел: " + taskCount);
} }
void printInfo() { void printInfo() {
@ -27,6 +47,7 @@ class Task {
Номер: %-3d Номер: %-3d
Дата создания: %s
Статус: %s Статус: %s
Название: %-20s Название: %-20s
------------------------------ ------------------------------
@ -35,7 +56,7 @@ class Task {
"""; """;
; ;
String stringCompleted = completed ? "выполнено" : "не выполнено"; String stringCompleted = completed ? "выполнено" : "не выполнено";
System.out.printf(template, id, stringCompleted, title, description); System.out.printf(template, id, createdAt, stringCompleted, title, description);
} }
void toggleCompleted() { void toggleCompleted() {

View File

@ -5,11 +5,23 @@ import java.util.Scanner;
public class TodoApp { public class TodoApp {
private static Task[] tasks = new Task[10]; private static Task[] tasks = new Task[10];
private static int nextTaskId = 1;
private static int tasksCounter = 0;
private static Scanner scanner = new Scanner(System.in); private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) { public static void main(String[] args) {
/*
* System.out.println("\n\nDeveloper mode:\n\n");
*
* Task t1 = new Task("Дело тест - t1", "");
* Task t2 = new Task("Дело тест - t2", "");
* Task t3 = new Task("Дело тест - t3", "");
* printAll(t1, t2, t3);
* System.out.println(markTaskCompletedById(new Task[] { t1, t2, t3 }, 2));
* printAll(t1, t2, t3);
*
* System.out.println("\n\n" + "*".repeat(20) + "\n\n");
*/
displayMainMenu(); displayMainMenu();
} }
@ -37,6 +49,7 @@ public class TodoApp {
private static void displayMainMenu() { private static void displayMainMenu() {
while (true) { while (true) {
Task.printTotalTasksCreated();
String menu = """ String menu = """
@ -61,12 +74,11 @@ public class TodoApp {
private static void displayCreateTask() { private static void displayCreateTask() {
if (tasksCounter >= 9) { if (Task.taskCount >= 9) {
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n"); System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
return; return;
} }
int id = nextTaskId++;
String title; String title;
String description; String description;
@ -82,13 +94,13 @@ public class TodoApp {
System.out.print("\nНпишите описание дела: "); System.out.print("\nНпишите описание дела: ");
description = scanner.nextLine().trim(); description = scanner.nextLine().trim();
var task = new Task(id, title, description); var task = new Task(title, description);
tasks[tasksCounter++] = task; tasks[Task.taskCount - 1] = task;
} }
private static void displayTasks() { private static void displayTasks() {
if (tasksCounter < 1) { if (Task.taskCount == 0) {
int input = requestIntFromInput(""" int input = requestIntFromInput("""
Список дел пуст. Список дел пуст.
@ -105,16 +117,18 @@ public class TodoApp {
System.exit(0); System.exit(0);
} }
int[] variantsInput = new int[tasksCounter + 1];
System.out.println(""" System.out.println("""
Список дел: Список дел:
"""); """);
for (int i = 0; i < tasksCounter; i++) {
tasks[i].printInfo(); printAll(tasks);
int[] variantsInput = new int[Task.taskCount + 1];
for (int i = 0; i < Task.taskCount; i++)
variantsInput[i] = tasks[i].getId(); variantsInput[i] = tasks[i].getId();
} variantsInput[Task.taskCount] = 0;
variantsInput[tasksCounter] = 0;
int input = requestIntFromInput(""" int input = requestIntFromInput("""
Введите номер дела или 0 для возврата в меню:""", variantsInput); Введите номер дела или 0 для возврата в меню:""", variantsInput);
@ -127,7 +141,7 @@ public class TodoApp {
} }
private static Task getTaskById(int id) { private static Task getTaskById(int id) {
for (int i = 0; i < tasksCounter; i++) { for (int i = 0; i < Task.taskCount; i++) {
if (tasks[i].getId() == id) if (tasks[i].getId() == id)
return tasks[i]; return tasks[i];
} }
@ -145,4 +159,20 @@ public class TodoApp {
} }
} }
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;
}
} }