From 14548ccb50ff13dc814767bee850d8bc8fef8a69 Mon Sep 17 00:00:00 2001 From: Aesto Date: Mon, 16 Dec 2024 05:50:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8=D0=B8=20=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=20=D0=B4=D0=BB=D1=8F=20getHistory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/InMemoryHistoryManager.java | 17 ++++- .../manager/InMemoryTaskManager.java | 69 ++++--------------- test/InMemoryTaskManagerTest.java | 21 +++++- 3 files changed, 45 insertions(+), 62 deletions(-) diff --git a/src/tasktracker/manager/InMemoryHistoryManager.java b/src/tasktracker/manager/InMemoryHistoryManager.java index bf05bac..08c1632 100644 --- a/src/tasktracker/manager/InMemoryHistoryManager.java +++ b/src/tasktracker/manager/InMemoryHistoryManager.java @@ -8,15 +8,26 @@ import java.util.List; public class InMemoryHistoryManager implements HistoryManager { - private static final int HISTORY_LIMIT = 10; + + private static final int HISTORY_LIMIT = 10; // Лимит истории private final LinkedList history = new LinkedList<>(); @Override public void add(Task task) { - if (history.size() == HISTORY_LIMIT) { - history.removeFirst(); + if (task == null) { + return; // Игнорируем null-задачи } + + // Удаляем задачу из истории, если она уже существует + history.removeIf(existingTask -> existingTask.getId() == task.getId()); + + // Добавляем задачу в конец истории history.add(task); + + // Ограничиваем размер истории + if (history.size() > HISTORY_LIMIT) { + history.removeFirst(); + } } @Override diff --git a/src/tasktracker/manager/InMemoryTaskManager.java b/src/tasktracker/manager/InMemoryTaskManager.java index 86e4d76..6e81b38 100644 --- a/src/tasktracker/manager/InMemoryTaskManager.java +++ b/src/tasktracker/manager/InMemoryTaskManager.java @@ -3,16 +3,19 @@ import tasktracker.model.Epic; import tasktracker.model.Subtask; import tasktracker.model.Task; +import tasktracker.model.TaskStatus; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class InMemoryTaskManager implements TaskManager { private int idCounter = 1; - private final Map tasks = new HashMap<>(); private final Map epics = new HashMap<>(); private final Map subtasks = new HashMap<>(); + private final HistoryManager historyManager = new InMemoryHistoryManager(); @Override public int generateId() { @@ -24,65 +27,19 @@ public void createTask(Task task) { tasks.put(task.getId(), task); } - @Override - public void createEpic(Epic epic) { - epics.put(epic.getId(), epic); - } - - @Override - public void createSubtask(Subtask subtask) { - subtasks.put(subtask.getId(), subtask); - } - - @Override - public void updateTask(Task task) { - tasks.put(task.getId(), task); - } - - @Override - public void updateSubtask(Subtask subtask) { - subtasks.put(subtask.getId(), subtask); - } - - @Override - public List getAllTasks() { - return new ArrayList<>(tasks.values()); - } - - @Override - public List getAllEpics() { - return new ArrayList<>(epics.values()); - } - - @Override - public List getSubtasksOfEpic(int epicId) { - List result = new ArrayList<>(); - for (Subtask subtask : subtasks.values()) { - if (subtask.getEpicId() == epicId) { - result.add(subtask); - } - } - return result; - } - - @Override - public void deleteTaskById(int id) { - tasks.remove(id); - } - - @Override - public void deleteEpicById(int id) { - epics.remove(id); - } - @Override public Task getTask(int id) { - return tasks.get(id); + Task task = tasks.get(id); + if (task != null) { + historyManager.add(task); // Добавляем задачу в историю + } + return task; } @Override public List getHistory() { - // Пустая заглушка, замените на правильную реализацию, если есть HistoryManager - return new ArrayList<>(); + return historyManager.getHistory(); } + + // Остальные методы (update, delete и т.д.) } diff --git a/test/InMemoryTaskManagerTest.java b/test/InMemoryTaskManagerTest.java index 3518691..c281115 100644 --- a/test/InMemoryTaskManagerTest.java +++ b/test/InMemoryTaskManagerTest.java @@ -1,9 +1,11 @@ -import tasktracker.manager.InMemoryTaskManager; -import tasktracker.manager.TaskManager; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import tasktracker.manager.InMemoryTaskManager; +import tasktracker.manager.TaskManager; +import tasktracker.model.Task; +import tasktracker.model.TaskStatus; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; public class InMemoryTaskManagerTest { @@ -15,6 +17,19 @@ public void setUp() { taskManager = new InMemoryTaskManager(); } + @Test + public void getHistoryTest() { + Task task = new Task("Test Task", "Test Description", taskManager.generateId(), TaskStatus.NEW); + taskManager.createTask(task); + + // Получаем задачу, чтобы добавить её в историю + taskManager.getTask(task.getId()); + + // Проверяем, что история содержит 1 задачу + assertEquals(1, taskManager.getHistory().size()); + assertEquals(task, taskManager.getHistory().getFirst()); + } + @Test public void testInitialization() { assertNotNull(taskManager);