Merge pull request 'feature/tasks/methods-and-statics' (#16) from feature/tasks/methods-and-statics into main
Reviewed-on: #16
This commit is contained in:
commit
5776ec51d8
@ -1,15 +1,35 @@
|
||||
package ru.kamask.pet;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
class Task {
|
||||
private static int nextId;
|
||||
static int taskCount;
|
||||
|
||||
static {
|
||||
taskCount = 0;
|
||||
nextId = 0;
|
||||
}
|
||||
|
||||
private int id;
|
||||
private String title;
|
||||
private String description;
|
||||
private boolean completed = false;
|
||||
private LocalDate createdAt;
|
||||
|
||||
Task(int id, String title, String description) {
|
||||
this.id = id;
|
||||
Task(String title, String description) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
};
|
||||
|
||||
{
|
||||
id = ++nextId;
|
||||
createdAt = LocalDate.now();
|
||||
++taskCount;
|
||||
}
|
||||
|
||||
static void printTotalTasksCreated() {
|
||||
System.out.println("Колличество дел: " + taskCount);
|
||||
}
|
||||
|
||||
void printInfo() {
|
||||
@ -27,6 +47,7 @@ class Task {
|
||||
|
||||
──────────────────────────────
|
||||
Номер: %-3d
|
||||
Дата создания: %s
|
||||
Статус: %s
|
||||
Название: %-20s
|
||||
------------------------------
|
||||
@ -35,7 +56,7 @@ class Task {
|
||||
""";
|
||||
;
|
||||
String stringCompleted = completed ? "выполнено" : "не выполнено";
|
||||
System.out.printf(template, id, stringCompleted, title, description);
|
||||
System.out.printf(template, id, createdAt, stringCompleted, title, description);
|
||||
}
|
||||
|
||||
void toggleCompleted() {
|
||||
|
||||
@ -5,11 +5,23 @@ import java.util.Scanner;
|
||||
public class TodoApp {
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@ -37,6 +49,7 @@ public class TodoApp {
|
||||
|
||||
private static void displayMainMenu() {
|
||||
while (true) {
|
||||
Task.printTotalTasksCreated();
|
||||
String menu = """
|
||||
|
||||
┌──────────────────────────┐
|
||||
@ -61,12 +74,11 @@ public class TodoApp {
|
||||
|
||||
private static void displayCreateTask() {
|
||||
|
||||
if (tasksCounter >= 9) {
|
||||
if (Task.taskCount >= 9) {
|
||||
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int id = nextTaskId++;
|
||||
String title;
|
||||
String description;
|
||||
|
||||
@ -82,13 +94,13 @@ public class TodoApp {
|
||||
System.out.print("\nНпишите описание дела: ");
|
||||
description = scanner.nextLine().trim();
|
||||
|
||||
var task = new Task(id, title, description);
|
||||
tasks[tasksCounter++] = task;
|
||||
var task = new Task(title, description);
|
||||
tasks[Task.taskCount - 1] = task;
|
||||
}
|
||||
|
||||
private static void displayTasks() {
|
||||
|
||||
if (tasksCounter < 1) {
|
||||
if (Task.taskCount == 0) {
|
||||
int input = requestIntFromInput("""
|
||||
|
||||
Список дел пуст.
|
||||
@ -105,16 +117,18 @@ public class TodoApp {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
int[] variantsInput = new int[tasksCounter + 1];
|
||||
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[tasksCounter] = 0;
|
||||
variantsInput[Task.taskCount] = 0;
|
||||
|
||||
int input = requestIntFromInput("""
|
||||
|
||||
Введите номер дела или 0 для возврата в меню:""", variantsInput);
|
||||
@ -127,7 +141,7 @@ public class TodoApp {
|
||||
}
|
||||
|
||||
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)
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user