Skip to content

Commit

Permalink
Исправлены методы истории и добавлен тест для getHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
AestoDes committed Dec 16, 2024
1 parent e57d5c1 commit 14548cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 62 deletions.
17 changes: 14 additions & 3 deletions src/tasktracker/manager/InMemoryHistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task> 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
Expand Down
69 changes: 13 additions & 56 deletions src/tasktracker/manager/InMemoryTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Task> tasks = new HashMap<>();
private final Map<Integer, Epic> epics = new HashMap<>();
private final Map<Integer, Subtask> subtasks = new HashMap<>();
private final HistoryManager historyManager = new InMemoryHistoryManager();

@Override
public int generateId() {
Expand All @@ -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<Task> getAllTasks() {
return new ArrayList<>(tasks.values());
}

@Override
public List<Epic> getAllEpics() {
return new ArrayList<>(epics.values());
}

@Override
public List<Subtask> getSubtasksOfEpic(int epicId) {
List<Subtask> 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<Task> getHistory() {
// Пустая заглушка, замените на правильную реализацию, если есть HistoryManager
return new ArrayList<>();
return historyManager.getHistory();
}

// Остальные методы (update, delete и т.д.)
}
21 changes: 18 additions & 3 deletions test/InMemoryTaskManagerTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 14548cc

Please sign in to comment.