diff --git a/src/Main.java b/src/Main.java index ef4bbc2..dcd7281 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,7 +5,6 @@ import service.Managers; import service.TaskManager; - public class Main { public static void main(String[] args) { @@ -15,6 +14,8 @@ public static void main(String[] args) { int task1Id = manager.addNewTask(task1); Task task2 = new Task("Просто Задача - 2", "Описание простой задачи - 2"); int task2Id = manager.addNewTask(task2); + Task task3 = new Task("Просто Задача - 2", "Описание простой задачи - 2"); + int task3Id = manager.addNewTask(task3); Epic epic1 = new Epic("Эпическая задача - 1", "Описание эпической задачи - 1"); @@ -27,26 +28,39 @@ public static void main(String[] args) { "Описание подзадачи - 1, эпической задачи - 1", epic1.getId()); int subtask1Id = manager.addNewSubtask(subtask1); Subtask subtask2 = new Subtask("Подзадача - 2", -"Описание подзадачи - 2, эпической задачи - 1", epic1.getId()); + "Описание подзадачи - 2, эпической задачи - 1", epic1.getId()); int subtask2Id = manager.addNewSubtask(subtask2); Subtask subtask3 = new Subtask("Подзадача - 3", - "Описание подзадачи - 3, эпической задачи - 2", epic2.getId()); + "Описание подзадачи - 3, эпической задачи - 1", epic1.getId()); int subtask3Id = manager.addNewSubtask(subtask3); - manager.getTask(2); - manager.getTask(1); - manager.getTask(2); - manager.getEpic(4); - manager.getSubtask(6); - manager.getSubtask(5); - manager.getEpic(3); - manager.getSubtask(7); - manager.getTask(1); - manager.getTask(1); - manager.getSubtask(6); - manager.getSubtask(5); + manager.getTask(task2Id); + manager.getTask(task1Id); + manager.getTask(task2Id); + manager.getEpic(epic2Id); + manager.getSubtask(subtask2Id); + manager.getSubtask(subtask1Id); + manager.getEpic(epic1Id); + manager.getSubtask(subtask3Id); + manager.getTask(task3Id); + manager.getTask(task1Id); + manager.getSubtask(subtask2Id); + manager.getSubtask(subtask1Id); printAllTasks(manager); + printHistory(manager); + + manager.deleteTask(task1Id); + System.out.println("\n Удаляем задачу - 1"); + printHistory(manager); + + manager.deleteEpic(epic1Id); + System.out.println("\n Удаляем эпик - 1 с тремя подзадачами"); + printHistory(manager); + + manager.deleteAllTasks(); + System.out.println("\n Удаляем все задачи"); + printHistory(manager); } private static void printAllTasks(TaskManager manager) { @@ -66,8 +80,10 @@ private static void printAllTasks(TaskManager manager) { for (Task subtask : manager.getAllSubtasks()) { System.out.println(subtask); } + } - System.out.println("История:"); + private static void printHistory(TaskManager manager) { + System.out.println("\nИстория:"); for (Task task : manager.getHistory()) { System.out.println(task); } diff --git a/src/model/Epic.java b/src/model/Epic.java index e9d193a..62d6059 100644 --- a/src/model/Epic.java +++ b/src/model/Epic.java @@ -1,10 +1,11 @@ package model; import java.util.ArrayList; +import java.util.List; public class Epic extends Task { - private final ArrayList subtaskIds = new ArrayList<>(); + private final List subtaskIds = new ArrayList<>(); public Epic(int id, String title, String description, Status status) { super(id, title, description, status); @@ -19,7 +20,7 @@ public void addSubtaskId(int subtaskId) { this.subtaskIds.add(subtaskId); } - public ArrayList getSubtaskIds() { + public List getSubtaskIds() { return subtaskIds; } diff --git a/src/service/InMemoryHistoryManager.java b/src/service/InMemoryHistoryManager.java index d5133ab..de3b402 100644 --- a/src/service/InMemoryHistoryManager.java +++ b/src/service/InMemoryHistoryManager.java @@ -20,19 +20,19 @@ public List getHistory() { @Override public void addInHistory(Task task) { if (task != null) { + removeFromHistory(task.getId()); linkLast(task); } } private void linkLast(Task task) { - Node newNode = new Node(task, null, this.tail); - if (this.tail == null) { - this.head = newNode; + Node newNode = new Node(task, null, tail); + if (tail == null) { + head = newNode; } else { tail.setNext(newNode); } - this.tail = newNode; - removeFromHistory(task.getId()); + tail = newNode; listHistory.put(task.getId(), newNode); } @@ -57,16 +57,18 @@ private void removeNode(Node node) { if (node != null) { Node nodeNext = node.getNext(); Node nodePrev = node.getPrev(); - if (nodePrev == null) { + if (head == node) { head = nodeNext; - head.setPrev(null); - } else { - nodePrev.setNext(nodeNext); } - if (nodeNext == null) { + if (tail == node) { tail = nodePrev; - tail.setNext(null); - } else { + } + + if (nodePrev != null) { + nodePrev.setNext(nodeNext); + } + + if (nodeNext != null) { nodeNext.setPrev(nodePrev); } } diff --git a/src/service/InMemoryTaskManager.java b/src/service/InMemoryTaskManager.java index 82523a3..7e2cfc9 100644 --- a/src/service/InMemoryTaskManager.java +++ b/src/service/InMemoryTaskManager.java @@ -8,11 +8,12 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class InMemoryTaskManager implements TaskManager { - private final HashMap tasks = new HashMap<>(); - private final HashMap epics = new HashMap<>(); - private final HashMap subtasks = new HashMap<>(); + private final Map tasks = new HashMap<>(); + private final Map epics = new HashMap<>(); + private final Map subtasks = new HashMap<>(); private int id = 0; private final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory(); @@ -34,11 +35,13 @@ public List getAllEpics() { @Override public void deleteAllTasks() { + deleteAllTasksFromHistory(tasks); tasks.clear(); } @Override public void deleteAllSubtasks() { + deleteAllTasksFromHistory(subtasks); for (Epic epic : epics.values()) { epic.getSubtaskIds().clear(); updateStatusEpic(epic.getId()); @@ -48,7 +51,9 @@ public void deleteAllSubtasks() { @Override public void deleteAllEpics() { + deleteAllTasksFromHistory(subtasks); subtasks.clear(); + deleteAllTasksFromHistory(epics); epics.clear(); } @@ -137,6 +142,7 @@ public void updateEpic(Epic epic) { @Override public void deleteTask(int id) { + inMemoryHistoryManager.removeFromHistory(id); tasks.remove(id); } @@ -145,8 +151,8 @@ public void deleteSubtask(Integer id) { if (subtasks.containsKey(id)) { int epicId = subtasks.get(id).getEpicId(); Epic epic = epics.get(epicId); - epic.getSubtaskIds().remove(id); + inMemoryHistoryManager.removeFromHistory(id); subtasks.remove(id); updateStatusEpic(epicId); } @@ -156,12 +162,14 @@ public void deleteSubtask(Integer id) { public void deleteEpic(int epicId) { if (epics.containsKey(epicId)) { Epic epic = epics.get(epicId); - ArrayList subtaskIdsClone = new ArrayList<>(epic.getSubtaskIds()); + List subtaskIdsClone = new ArrayList<>(epic.getSubtaskIds()); for (Integer subtaskId : subtaskIdsClone) { + inMemoryHistoryManager.removeFromHistory(subtaskId); epic.getSubtaskIds().remove(subtaskId); subtasks.remove(subtaskId); } + inMemoryHistoryManager.removeFromHistory(epicId); epics.remove(epicId); } } @@ -210,4 +218,10 @@ public void updateStatusEpic(int epicId) { } } } + + private void deleteAllTasksFromHistory(Map tasksToDelete) { + for (Integer keyId : tasksToDelete.keySet()) { + inMemoryHistoryManager.removeFromHistory(keyId); + } + } } diff --git a/test/service/InMemoryHistoryManagerTest.java b/test/service/InMemoryHistoryManagerTest.java index 3cbc1c7..fe686e1 100644 --- a/test/service/InMemoryHistoryManagerTest.java +++ b/test/service/InMemoryHistoryManagerTest.java @@ -5,6 +5,7 @@ import model.Task; import java.util.List; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; @@ -131,7 +132,7 @@ void removeSubtaskInHistoryTest() { assertNotNull(history, "История не пустая."); assertEquals(4, history.size(), "Размер истории не верный"); - assertEquals(List.of(epic, subtask2, subtask1,epic2), manager.getHistory(), "История не соответствует"); + assertEquals(List.of(epic, subtask2, subtask1, epic2), manager.getHistory(), "История не соответствует"); } @Test @@ -150,4 +151,18 @@ void getHistoryTest() { assertEquals(history.get(0), subtask, "История возвращается неверно"); assertEquals(history.get(1), epic, "История возвращается неверно"); } + + @Test + void deleteAllTasksFromHistoryTest() { + int taskId1 = manager.addNewTask(task); + int taskId2 = manager.addNewTask(task2); + manager.getTask(taskId1); + manager.getTask(taskId2); + + manager.deleteAllTasks(); + + List history = manager.getHistory(); + + assertTrue(history.isEmpty(), "Задачи не удалились"); + } }