diff --git a/src/Main.java b/src/Main.java index 55cce3e..ce661e1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -9,17 +9,17 @@ public class Main { public static void main(String[] args) { TaskManager manager = new InMemoryTaskManager(); - Task task1 = new Task("Task 1", "Description 1", manager.generateId(), TaskStatus.NEW); - manager.createTask(task1); + Task task = new Task("Task 1", "Description 1", manager.generateId(), TaskStatus.NEW); + manager.createTask(task); - Epic epic1 = new Epic("Epic 1", "Epic Description", manager.generateId(), TaskStatus.NEW); - manager.createEpic(epic1); + Epic epic = new Epic("Epic 1", "Epic Description", manager.generateId(), TaskStatus.NEW); + manager.createEpic(epic); - Subtask subtask1 = new Subtask("Subtask 1", "Description 1", manager.generateId(), TaskStatus.NEW, epic1.getId()); - manager.createSubtask(subtask1); + Subtask subtask = new Subtask("Subtask 1", "Description 1", manager.generateId(), TaskStatus.NEW, epic.getId()); + manager.createSubtask(subtask); System.out.println("Tasks: " + manager.getAllTasks()); System.out.println("Epics: " + manager.getAllEpics()); - System.out.println("Subtasks of Epic: " + manager.getSubtasksOfEpic(epic1.getId())); + System.out.println("Subtasks: " + manager.getAllSubtasks()); } } diff --git a/src/tasktracker/manager/HistoryManager.java b/src/tasktracker/manager/HistoryManager.java index ca623f6..6238a5f 100644 --- a/src/tasktracker/manager/HistoryManager.java +++ b/src/tasktracker/manager/HistoryManager.java @@ -9,5 +9,5 @@ public interface HistoryManager { List getHistory(); - void remove(int id); // Новый метод для удаления задачи по ID + void remove(int id); } diff --git a/src/tasktracker/manager/InMemoryHistoryManager.java b/src/tasktracker/manager/InMemoryHistoryManager.java index 08c1632..9de79b1 100644 --- a/src/tasktracker/manager/InMemoryHistoryManager.java +++ b/src/tasktracker/manager/InMemoryHistoryManager.java @@ -2,29 +2,23 @@ import tasktracker.model.Task; -import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; 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 (task == null) { - return; // Игнорируем null-задачи + return; } - - // Удаляем задачу из истории, если она уже существует history.removeIf(existingTask -> existingTask.getId() == task.getId()); - - // Добавляем задачу в конец истории history.add(task); - - // Ограничиваем размер истории if (history.size() > HISTORY_LIMIT) { history.removeFirst(); } @@ -32,7 +26,7 @@ public void add(Task task) { @Override public List getHistory() { - return Collections.unmodifiableList(new ArrayList<>(history)); + return Collections.unmodifiableList(history); } @Override diff --git a/src/tasktracker/manager/InMemoryTaskManager.java b/src/tasktracker/manager/InMemoryTaskManager.java index 6e81b38..c59ff67 100644 --- a/src/tasktracker/manager/InMemoryTaskManager.java +++ b/src/tasktracker/manager/InMemoryTaskManager.java @@ -3,8 +3,8 @@ import tasktracker.model.Epic; import tasktracker.model.Subtask; import tasktracker.model.Task; -import tasktracker.model.TaskStatus; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,19 +27,115 @@ public void createTask(Task task) { tasks.put(task.getId(), task); } + @Override + public void updateTask(Task task) { + if (tasks.containsKey(task.getId())) { + tasks.put(task.getId(), task); + } + } + @Override public Task getTask(int id) { Task task = tasks.get(id); if (task != null) { - historyManager.add(task); // Добавляем задачу в историю + historyManager.add(task); } return task; } + @Override + public void createEpic(Epic epic) { + epics.put(epic.getId(), epic); + } + + @Override + public Epic getEpic(int id) { + Epic epic = epics.get(id); + if (epic != null) { + historyManager.add(epic); + } + return epic; + } + + @Override + public void createSubtask(Subtask subtask) { + subtasks.put(subtask.getId(), subtask); + Epic epic = epics.get(subtask.getEpicId()); + if (epic != null) { + epic.addSubtaskId(subtask.getId()); + } + } + + @Override + public Subtask getSubtask(int id) { + Subtask subtask = subtasks.get(id); + if (subtask != null) { + historyManager.add(subtask); + } + return subtask; + } + + @Override + public List getSubtasksOfEpic(int epicId) { + List epicSubtasks = new ArrayList<>(); + Epic epic = epics.get(epicId); + if (epic != null) { + for (Integer subtaskId : epic.getSubtaskIds()) { + if (subtasks.containsKey(subtaskId)) { + epicSubtasks.add(subtasks.get(subtaskId)); + } + } + } + return epicSubtasks; + } + + @Override + public void deleteTaskById(int id) { + tasks.remove(id); + historyManager.remove(id); + } + + @Override + public void deleteSubtaskById(int id) { + Subtask subtask = subtasks.remove(id); + if (subtask != null) { + Epic epic = epics.get(subtask.getEpicId()); + if (epic != null) { + epic.getSubtaskIds().remove((Integer) id); + } + } + historyManager.remove(id); + } + + @Override + public void deleteEpicById(int id) { + Epic epic = epics.remove(id); + if (epic != null) { + for (Integer subtaskId : epic.getSubtaskIds()) { + subtasks.remove(subtaskId); + historyManager.remove(subtaskId); + } + } + historyManager.remove(id); + } + + @Override + public List getAllTasks() { + return new ArrayList<>(tasks.values()); + } + + @Override + public List getAllEpics() { + return new ArrayList<>(epics.values()); + } + + @Override + public List getAllSubtasks() { + return new ArrayList<>(subtasks.values()); + } + @Override public List getHistory() { return historyManager.getHistory(); } - - // Остальные методы (update, delete и т.д.) } diff --git a/src/tasktracker/manager/InMemoryTaskManagerTest.java b/src/tasktracker/manager/InMemoryTaskManagerTest.java index ebdf73c..c281115 100644 --- a/src/tasktracker/manager/InMemoryTaskManagerTest.java +++ b/src/tasktracker/manager/InMemoryTaskManagerTest.java @@ -1,73 +1,37 @@ -package tasktracker.manager; - - - 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.*; - - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class InMemoryTaskManagerTest { - - private TaskManager taskManager; - - @BeforeEach - public void setUp() { - taskManager = new InMemoryTaskManager(); - } - - @Test - - public void addTaskTest() { - - Task task = new Task("Test Task", "Test Description", taskManager.generateId(), TaskStatus.NEW); - - taskManager.createTask(task); - - - - assertEquals(1, taskManager.getAllTasks().size()); - - assertEquals(task, taskManager.getAllTasks().getFirst()); - - } - - - - @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()); - } -} \ No newline at end of file + @Test + public void testInitialization() { + assertNotNull(taskManager); + } +} diff --git a/src/tasktracker/manager/TaskManager.java b/src/tasktracker/manager/TaskManager.java index cb21283..6df7ee8 100644 --- a/src/tasktracker/manager/TaskManager.java +++ b/src/tasktracker/manager/TaskManager.java @@ -15,21 +15,27 @@ public interface TaskManager { void createSubtask(Subtask subtask); - List getAllTasks(); + Task getTask(int id); - List getAllEpics(); + Epic getEpic(int id); - List getSubtasksOfEpic(int epicId); // Добавлено - - Task getTask(int id); + Subtask getSubtask(int id); void updateTask(Task task); - void updateSubtask(Subtask subtask); - void deleteTaskById(int id); void deleteEpicById(int id); + void deleteSubtaskById(int id); + + List getAllTasks(); + + List getAllEpics(); + + List getAllSubtasks(); + + List getSubtasksOfEpic(int epicId); + List getHistory(); } diff --git a/src/tasktracker/model/Epic.java b/src/tasktracker/model/Epic.java index a9459bc..ca30f00 100644 --- a/src/tasktracker/model/Epic.java +++ b/src/tasktracker/model/Epic.java @@ -4,29 +4,18 @@ import java.util.List; public class Epic extends Task { - private List subtaskIds; + + private final List subtaskIds = new ArrayList<>(); public Epic(String title, String description, int id, TaskStatus status) { super(title, description, id, status); - this.subtaskIds = new ArrayList<>(); } public List getSubtaskIds() { return subtaskIds; } - public void addSubtaskId(int subtaskId) { - subtaskIds.add(subtaskId); - } - - @Override - public String toString() { - return "Epic{" + - "subtaskIds=" + subtaskIds + - ", title='" + getTitle() + '\'' + - ", description='" + getDescription() + '\'' + - ", id=" + getId() + - ", status=" + getStatus() + - '}'; + public void addSubtaskId(int id) { + subtaskIds.add(id); } } diff --git a/src/tasktracker/model/Subtask.java b/src/tasktracker/model/Subtask.java index 9a66100..7e0137b 100644 --- a/src/tasktracker/model/Subtask.java +++ b/src/tasktracker/model/Subtask.java @@ -1,7 +1,8 @@ package tasktracker.model; public class Subtask extends Task { - private int epicId; + + private final int epicId; public Subtask(String title, String description, int id, TaskStatus status, int epicId) { super(title, description, id, status); @@ -11,15 +12,4 @@ public Subtask(String title, String description, int id, TaskStatus status, int public int getEpicId() { return epicId; } - - @Override - public String toString() { - return "Subtask{" + - "epicId=" + epicId + - ", title='" + getTitle() + '\'' + - ", description='" + getDescription() + '\'' + - ", id=" + getId() + - ", status=" + getStatus() + - '}'; - } } diff --git a/src/tasktracker/model/Task.java b/src/tasktracker/model/Task.java index 1fc7c80..0cfac83 100644 --- a/src/tasktracker/model/Task.java +++ b/src/tasktracker/model/Task.java @@ -1,9 +1,10 @@ package tasktracker.model; public class Task { - private String title; - private String description; - private int id; + + private final String title; + private final String description; + private final int id; private TaskStatus status; public Task(String title, String description, int id, TaskStatus status) { @@ -32,14 +33,4 @@ public TaskStatus getStatus() { public void setStatus(TaskStatus status) { this.status = status; } - - @Override - public String toString() { - return "Task{" + - "title='" + title + '\'' + - ", description='" + description + '\'' + - ", id=" + id + - ", status=" + status + - '}'; - } }