-
Notifications
You must be signed in to change notification settings - Fork 0
Реализация нового класса FileBackedTaskManager #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ac84da9
Реализация нового класса FileBackedTaskManager
evilgree c526b53
Исправления класса FileBackedTaskManager
evilgree 2a8c5ce
Ошибка pull request
evilgree f8c8cec
Ошибка pull request
evilgree d156d3d
Ошибка pull request
evilgree d4b35f3
добавления файлы tasks.csv
evilgree bd76055
ошибка pull request
evilgree 59307e5
ошибка в тестах
evilgree 53e28de
ошибка в тестах
evilgree ba062f3
ошибка в тесте inMemoryTaskManager
evilgree b5af7a8
ошибка в импорте
evilgree f63340f
добавлен класс CSVTaskConverter
evilgree 926aeee
ошибка pull request
evilgree 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,75 @@ | ||
| package manager; | ||
|
|
||
| import model.Epic; | ||
| import model.Subtask; | ||
| import model.Task; | ||
| import model.TaskType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class CSVTaskConverter { | ||
|
|
||
| public static String taskToString(Task task) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(task.getId()).append(","); | ||
| sb.append(task.getType()).append(","); | ||
| sb.append(task.getTitle()).append(","); | ||
| sb.append(task.getStatus()).append(","); | ||
| sb.append(task.getDescription()).append(","); | ||
| if (task instanceof Subtask) { | ||
| sb.append(((Subtask) task).getEpicId()); | ||
| } else { | ||
| sb.append(""); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| public static String historyToString(List<Task> history) { | ||
| return history.stream() | ||
| .map(t -> String.valueOf(t.getId())) | ||
| .collect(Collectors.joining(",")); | ||
| } | ||
|
|
||
| public static Task fromString(String value) { | ||
| String[] parts = value.split(",", 6); | ||
| int id = Integer.parseInt(parts[0]); | ||
| TaskType type = TaskType.valueOf(parts[1]); | ||
| String title = parts[2]; | ||
| Status status = Status.valueOf(parts[3]); | ||
| String description = parts[4]; | ||
| String epicField = parts.length > 5 ? parts[5] : ""; | ||
|
|
||
| switch (type) { | ||
| case TASK: | ||
| Task task = new Task(title, description, status); | ||
| task.setId(id); | ||
| return task; | ||
| case EPIC: | ||
| Epic epic = new Epic(title, description); | ||
| epic.setId(id); | ||
| return epic; | ||
| case SUBTASK: | ||
| int epicId = epicField.isEmpty() ? -1 : Integer.parseInt(epicField); | ||
| Subtask subtask = new Subtask(title, description, epicId, status); | ||
| subtask.setId(id); | ||
| return subtask; | ||
| default: | ||
| throw new IllegalArgumentException("Неизвестный тип задачи: " + type); | ||
| } | ||
| } | ||
|
|
||
| public static List<Integer> historyFromString(String value) { | ||
| if (value == null || value.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| String[] parts = value.split(","); | ||
| List<Integer> historyIds = new ArrayList<>(); | ||
| for (String part : parts) { | ||
| historyIds.add(Integer.parseInt(part)); | ||
| } | ||
| return historyIds; | ||
| } | ||
| } |
This file contains hidden or 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,208 @@ | ||
| package manager; | ||
|
|
||
| import model.*; | ||
|
|
||
| import java.io.*; | ||
| import java.nio.file.Files; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class FileBackedTaskManager extends InMemoryTaskManager { | ||
| private static final String CSV_HEADER = "id,type,name,status,description,epic"; | ||
|
|
||
| private final File file; | ||
|
|
||
| public FileBackedTaskManager(File file) { | ||
| super(Managers.getDefaultHistory()); | ||
| this.file = file; | ||
| } | ||
|
|
||
| protected void save() { | ||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | ||
| writer.write(CSV_HEADER); | ||
| writer.newLine(); | ||
|
|
||
| for (Task task : getAllTasks()) { | ||
| writer.write(CSVTaskConverter.taskToString(task)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| for (Epic epic : getAllEpics()) { | ||
| writer.write(CSVTaskConverter.taskToString(epic)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| for (Subtask subtask : getAllSubtasks()) { | ||
| writer.write(CSVTaskConverter.taskToString(subtask)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| writer.newLine(); | ||
|
|
||
| writer.write(CSVTaskConverter.historyToString(getHistory())); | ||
|
|
||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Ошибка сохранения в файл " + file.getAbsolutePath(), e); | ||
| } | ||
| } | ||
|
|
||
| public static FileBackedTaskManager loadFromFile(File file) { | ||
| FileBackedTaskManager manager = new FileBackedTaskManager(file); | ||
|
|
||
| try { | ||
| List<String> lines = Files.readAllLines(file.toPath()); | ||
|
|
||
| int i = 0; | ||
| if (!lines.isEmpty() && lines.get(0).equals("id,type,name,status,description,epic")) { | ||
| i++; | ||
| } | ||
|
|
||
| while (i < lines.size() && !lines.get(i).isEmpty()) { | ||
| Task task = CSVTaskConverter.fromString(lines.get(i)); | ||
| if (task.getType() == TaskType.EPIC) { | ||
| manager.epics.put(task.getId(), (Epic) task); | ||
| } else if (task.getType() == TaskType.SUBTASK) { | ||
| manager.subtasks.put(task.getId(), (Subtask) task); | ||
| } else { | ||
| manager.tasks.put(task.getId(), task); | ||
| } | ||
| if (task.getId() >= manager.idCounter) { | ||
| manager.idCounter = task.getId() + 1; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| i++; | ||
|
|
||
| if (i < lines.size()) { | ||
| List<Integer> historyIds = CSVTaskConverter.historyFromString(lines.get(i)); | ||
| for (Integer id : historyIds) { | ||
| Task task = manager.tasks.get(id); | ||
| if (task == null) { | ||
| task = manager.epics.get(id); | ||
| } | ||
| if (task == null) { | ||
| task = manager.subtasks.get(id); | ||
| } | ||
| if (task != null) { | ||
| manager.historyManager.add(task); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (Subtask subtask : manager.subtasks.values()) { | ||
| Epic epic = manager.epics.get(subtask.getEpicId()); | ||
| if (epic != null) { | ||
| if (!epic.getSubtaskIds().contains(subtask.getId())) { | ||
| epic.addSubtask(subtask.getId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (Epic epic : manager.epics.values()) { | ||
| manager.updateEpicStatus(epic); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Ошибка загрузки из файла " + file.getAbsolutePath(), e); | ||
| } | ||
|
|
||
| return manager; | ||
| } | ||
|
|
||
| @Override | ||
| public Task createTask(Task task) { | ||
| Task t = super.createTask(task); | ||
| save(); | ||
| return t; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic createEpic(Epic epic) { | ||
| Epic e = super.createEpic(epic); | ||
| save(); | ||
| return e; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask createSubtask(Subtask subtask) { | ||
| Subtask s = super.createSubtask(subtask); | ||
| save(); | ||
| return s; | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTask(Task task) { | ||
| super.updateTask(task); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateEpic(Epic epic) { | ||
| super.updateEpic(epic); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateSubtask(Subtask subtask) { | ||
| super.updateSubtask(subtask); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteTask(int taskId) { | ||
| super.deleteTask(taskId); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteEpic(int epicId) { | ||
| super.deleteEpic(epicId); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteSubtask(int subtaskId) { | ||
| super.deleteSubtask(subtaskId); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAllTasks() { | ||
| super.deleteAllTasks(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAllEpics() { | ||
| super.deleteAllEpics(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAllSubtasks() { | ||
| super.deleteAllSubtasks(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public Task getTaskById(int id) { | ||
| Task task = super.getTaskById(id); | ||
| save(); | ||
| return task; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic getEpicById(int id) { | ||
| Epic epic = super.getEpicById(id); | ||
| save(); | ||
| return epic; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask getSubtaskById(int id) { | ||
| Subtask subtask = super.getSubtaskById(id); | ||
| save(); | ||
| return subtask; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,7 @@ | ||
| package manager; | ||
|
|
||
| public class ManagerSaveException extends RuntimeException { | ||
| public ManagerSaveException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| package model; | ||
|
|
||
| public enum TaskType { | ||
| TASK, | ||
| EPIC, | ||
| SUBTASK | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 метод getDefault() должен теперь возвращать FileBackedTaskManager, это будет основной реализацией
Тесты для InMemoryTaskManager надо подкорректировать, если там getDefault используется