generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added functionality according to assignment specifications #1
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package manager; | ||
|
||
import model.Task; | ||
|
||
import java.util.List; | ||
|
||
public interface HistoryManager { | ||
void add(Task task); | ||
|
||
void remove(int id); | ||
|
||
List<Task> getHistory(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package manager; | ||
|
||
import model.Task; | ||
|
||
import java.util.*; | ||
|
||
public class InMemoryHistoryManager implements HistoryManager { | ||
// Хранение задач по ID и узлов двусвязного списка | ||
private final Map<Integer, Node> historyMap = new HashMap<>(); | ||
private Node head; | ||
private Node tail; | ||
|
||
@Override | ||
public void add(Task task) { | ||
if (task == null) return; | ||
|
||
// Удаляем задачу из истории, если она уже существует | ||
remove(task.getId()); | ||
|
||
// Добавляем задачу в конец списка | ||
linkLast(task); | ||
} | ||
|
||
@Override | ||
public void remove(int id) { | ||
Node node = historyMap.remove(id); | ||
if (node != null) { | ||
removeNode(node); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Task> getHistory() { | ||
List<Task> history = new ArrayList<>(); | ||
Node current = head; | ||
while (current != null) { | ||
history.add(current.task); | ||
current = current.next; | ||
} | ||
return history; | ||
} | ||
|
||
// Добавляет задачу в конец двусвязного списка | ||
private void linkLast(Task task) { | ||
Node newNode = new Node(task, tail, null); | ||
if (tail != null) { | ||
tail.next = newNode; | ||
} else { | ||
head = newNode; | ||
} | ||
tail = newNode; | ||
historyMap.put(task.getId(), newNode); | ||
} | ||
|
||
// Удаляет узел из двусвязного списка | ||
private void removeNode(Node node) { | ||
if (node.prev != null) { | ||
node.prev.next = node.next; | ||
} else { | ||
head = node.next; | ||
} | ||
if (node.next != null) { | ||
node.next.prev = node.prev; | ||
} else { | ||
tail = node.prev; | ||
} | ||
} | ||
|
||
// Вложенный класс для узла двусвязного списка | ||
private static class Node { | ||
Task task; | ||
Node prev; | ||
Node next; | ||
|
||
Node(Task task, Node prev, Node next) { | ||
this.task = task; | ||
this.prev = prev; | ||
this.next = next; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package manager; | ||
|
||
import model.*; | ||
import model.enums.StatusEnum; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
class InMemoryHistoryManagerTest { | ||
|
||
@Test | ||
void shouldAddTaskToHistory() { | ||
HistoryManager historyManager = new InMemoryHistoryManager(); | ||
Task task = new Task("Task 1", "Description 1", StatusEnum.NEW); | ||
task.setId(1); | ||
|
||
historyManager.add(task); | ||
|
||
List<Task> history = historyManager.getHistory(); | ||
assertEquals(1, history.size(), "История должна содержать одну задачу."); | ||
assertEquals(task, history.get(0), "Задача должна быть добавлена в историю."); | ||
} | ||
|
||
@Test | ||
void shouldRemoveTaskFromHistory() { | ||
HistoryManager historyManager = new InMemoryHistoryManager(); | ||
Task task1 = new Task("Task 1", "Description 1", StatusEnum.NEW); | ||
Task task2 = new Task("Task 2", "Description 2", StatusEnum.NEW); | ||
task1.setId(1); | ||
task2.setId(2); | ||
|
||
historyManager.add(task1); | ||
historyManager.add(task2); | ||
historyManager.remove(1); | ||
|
||
List<Task> history = historyManager.getHistory(); | ||
assertEquals(1, history.size(), "История должна содержать одну задачу после удаления."); | ||
assertEquals(task2, history.get(0), "Оставшаяся задача должна быть корректной."); | ||
} | ||
|
||
@Test | ||
void shouldNotLimitHistorySize() { | ||
HistoryManager historyManager = new InMemoryHistoryManager(); | ||
|
||
// Добавляем 15 задач | ||
for (int i = 0; i < 15; i++) { | ||
Task task = new Task("Task " + i, "Description " + i, StatusEnum.NEW); | ||
task.setId(i); | ||
historyManager.add(task); | ||
} | ||
|
||
List<Task> history = historyManager.getHistory(); | ||
assertEquals(15, history.size(), "История должна содержать все 15 задач без ограничения."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А давай все таки создадим класс Managers в котором будут методы
и все места создания обоих менеджеров через new заменим на вызовы соответствующих методов из списка выше.
Что нам это даст: