Skip to content
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

Спринт 6. Изменения хранения истории просмотра задач на LinkedList и … #1

Merged
merged 6 commits into from
Nov 26, 2024
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# java-kanban
Repository for homework project.
Sprint 6.
Необходимо:
- Сделать историю посещений неограниченной по размеру.
- Избавиться от повторных просмотров в истории. Если какую-либо задачу посещали несколько раз, то в истории должен остаться только её последний просмотр. Предыдущий должен быть удалён.
53 changes: 35 additions & 18 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import service.Managers;
import service.TaskManager;


public class Main {

public static void main(String[] args) {
Expand All @@ -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");
Expand All @@ -23,31 +24,45 @@ public static void main(String[] args) {
"Описание эпической задачи - 2");
int epic2Id = manager.addNewEpic(epic2);

Subtask subtask1 = new Subtask( "Подзадача - 1",
Subtask subtask1 = new Subtask("Подзадача - 1",
"Описание подзадачи - 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());
Subtask subtask3 = new Subtask("Подзадача - 3",
"Описание подзадачи - 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) {
System.out.println("Задачи:");
for (Task task : manager.getAllTasks()) {
Expand All @@ -65,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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/model/Epic.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package model;

import java.util.ArrayList;
import java.util.List;

public class Epic extends Task {

private final ArrayList<Integer> subtaskIds = new ArrayList<>();
private final List<Integer> subtaskIds = new ArrayList<>();

public Epic(int id, String title, String description, Status status) {
super(id, title, description, status);
Expand All @@ -19,7 +20,7 @@ public void addSubtaskId(int subtaskId) {
this.subtaskIds.add(subtaskId);
}

public ArrayList<Integer> getSubtaskIds() {
public List<Integer> getSubtaskIds() {
return subtaskIds;
}

Expand Down
3 changes: 3 additions & 0 deletions src/service/HistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

public interface HistoryManager {
void addInHistory(Task task);

void removeFromHistory(int id);

List<Task> getHistory();
}
68 changes: 57 additions & 11 deletions src/service/InMemoryHistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,74 @@
import model.Task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InMemoryHistoryManager implements HistoryManager {
private static final int SIZE_HISTORY = 10;
private final List<Task> history = new ArrayList<>(SIZE_HISTORY);

private void removeHistoryFirst() {
if (!history.isEmpty() && history.size() == SIZE_HISTORY) {
history.removeFirst();
}
}
private Node head = null;
private Node tail = null;
private final Map<Integer, Node> listHistory = new HashMap<>();

@Override
public List<Task> getHistory() {
return new ArrayList<>(history);
return getTasks();
}

@Override
public void addInHistory(Task task) {
if (task != null) {
removeHistoryFirst();
history.add(task);
removeFromHistory(task.getId());
linkLast(task);
}
}

private void linkLast(Task task) {
Node newNode = new Node(task, null, tail);
if (tail == null) {
head = newNode;
} else {
tail.setNext(newNode);
}
tail = newNode;
listHistory.put(task.getId(), newNode);
}

@Override
public void removeFromHistory(int id) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обязательно
Также нужно доработать сам таск менеджер и вызывать этот метод когда удаляются сами задачи, подзадачи и эпик не только удаление по id, но и при удалении всех задач, подзадач, эпиков. Согласно заданию
Добавьте вызов метода при удалении задач, чтобы они удалялись также из истории просмотров.

if (listHistory.containsKey(id)) {
removeNode(listHistory.get(id));
}
}

private List<Task> getTasks() {
List<Task> listHistoryRes = new ArrayList<>();
Node node = head;
while (node != null) {
listHistoryRes.add(node.getItem());
node = node.getNext();
}
return listHistoryRes;
}

private void removeNode(Node node) {
if (node != null) {
Node nodeNext = node.getNext();
Node nodePrev = node.getPrev();
if (head == node) {
head = nodeNext;
}
if (tail == node) {
tail = nodePrev;
}

if (nodePrev != null) {
nodePrev.setNext(nodeNext);
}

if (nodeNext != null) {
nodeNext.setPrev(nodePrev);
}
}
}
}
24 changes: 19 additions & 5 deletions src/service/InMemoryTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Task> tasks = new HashMap<>();
private final HashMap<Integer, Epic> epics = new HashMap<>();
private final HashMap<Integer, Subtask> subtasks = new HashMap<>();
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 int id = 0;

private final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory();
Expand All @@ -34,11 +35,13 @@ public List<Epic> 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());
Expand All @@ -48,7 +51,9 @@ public void deleteAllSubtasks() {

@Override
public void deleteAllEpics() {
deleteAllTasksFromHistory(subtasks);
subtasks.clear();
deleteAllTasksFromHistory(epics);
epics.clear();
}

Expand Down Expand Up @@ -137,6 +142,7 @@ public void updateEpic(Epic epic) {

@Override
public void deleteTask(int id) {
inMemoryHistoryManager.removeFromHistory(id);
tasks.remove(id);
}

Expand All @@ -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);
}
Expand All @@ -156,12 +162,14 @@ public void deleteSubtask(Integer id) {
public void deleteEpic(int epicId) {
if (epics.containsKey(epicId)) {
Epic epic = epics.get(epicId);
ArrayList<Integer> subtaskIdsClone = new ArrayList<>(epic.getSubtaskIds());
List<Integer> 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);
}
}
Expand Down Expand Up @@ -210,4 +218,10 @@ public void updateStatusEpic(int epicId) {
}
}
}

private void deleteAllTasksFromHistory(Map<Integer, ? extends Task> tasksToDelete) {
for (Integer keyId : tasksToDelete.keySet()) {
inMemoryHistoryManager.removeFromHistory(keyId);
}
}
}
39 changes: 39 additions & 0 deletions src/service/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package service;

import model.Task;

public class Node {
private Task item;
private Node next;
private Node prev;

public Node(Task item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}

public Task getItem() {
return item;
}

public void setItem(Task item) {
this.item = item;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public Node getPrev() {
return prev;
}

public void setPrev(Node prev) {
this.prev = prev;
}
}
Loading