Skip to content

Commit

Permalink
Реализован метод deleteSubtaskById и исправлены ошибки
Browse files Browse the repository at this point in the history
  • Loading branch information
AestoDes committed Dec 16, 2024
1 parent 14548cc commit 040a979
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 116 deletions.
14 changes: 7 additions & 7 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/tasktracker/manager/HistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface HistoryManager {

List<Task> getHistory();

void remove(int id); // Новый метод для удаления задачи по ID
void remove(int id);
}
14 changes: 4 additions & 10 deletions src/tasktracker/manager/InMemoryHistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@

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<Task> 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();
}
}

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

@Override
Expand Down
104 changes: 100 additions & 4 deletions src/tasktracker/manager/InMemoryTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Subtask> getSubtasksOfEpic(int epicId) {
List<Subtask> 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<Task> getAllTasks() {
return new ArrayList<>(tasks.values());
}

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

@Override
public List<Subtask> getAllSubtasks() {
return new ArrayList<>(subtasks.values());
}

@Override
public List<Task> getHistory() {
return historyManager.getHistory();
}

// Остальные методы (update, delete и т.д.)
}
58 changes: 11 additions & 47 deletions src/tasktracker/manager/InMemoryTaskManagerTest.java
Original file line number Diff line number Diff line change
@@ -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());

}

}
@Test
public void testInitialization() {
assertNotNull(taskManager);
}
}
20 changes: 13 additions & 7 deletions src/tasktracker/manager/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ public interface TaskManager {

void createSubtask(Subtask subtask);

List<Task> getAllTasks();
Task getTask(int id);

List<Epic> getAllEpics();
Epic getEpic(int id);

List<Subtask> 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<Task> getAllTasks();

List<Epic> getAllEpics();

List<Subtask> getAllSubtasks();

List<Subtask> getSubtasksOfEpic(int epicId);

List<Task> getHistory();
}
19 changes: 4 additions & 15 deletions src/tasktracker/model/Epic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@
import java.util.List;

public class Epic extends Task {
private List<Integer> subtaskIds;

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

public Epic(String title, String description, int id, TaskStatus status) {
super(title, description, id, status);
this.subtaskIds = new ArrayList<>();
}

public List<Integer> 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);
}
}
14 changes: 2 additions & 12 deletions src/tasktracker/model/Subtask.java
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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() +
'}';
}
}
Loading

0 comments on commit 040a979

Please sign in to comment.