-
Notifications
You must be signed in to change notification settings - Fork 0
Реализовать класс сохранения данных в файл. #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
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 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 @@ | ||
| id,type,name,status,description,epic |
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,59 @@ | ||
| package manager; | ||
| import tasks.*; | ||
|
|
||
| public class CsvFormatter { | ||
|
|
||
| public static final String CSV_HEADER = "id,type,name,status,description,epic"; | ||
|
|
||
|
|
||
| // Преобразование задачи в CSV строку | ||
| public static String toString(Task task) { | ||
| if (task == null) return ""; | ||
|
|
||
| String epicField = ""; | ||
| if (task.getType() == TaskType.SUBTASK) { | ||
| epicField = String.valueOf(((Subtask) task).getEpicId()); | ||
| } | ||
|
|
||
| return String.join(",", | ||
| String.valueOf(task.getId()), | ||
| task.getType().name(), | ||
| task.getName(), | ||
| task.getTaskStatus().name(), | ||
| task.getDescription(), | ||
| epicField); | ||
| } | ||
|
|
||
| // Создание задачи из CSV строки | ||
| public static Task fromString(String value) { | ||
| if (value == null || value.isEmpty()) return null; | ||
|
|
||
| String[] fields = value.split(","); | ||
| if (fields.length < 5) return null; | ||
|
|
||
| int id = Integer.parseInt(fields[0]); | ||
| TaskType type = TaskType.valueOf(fields[1]); | ||
| String name = fields[2]; | ||
| TaskStatus status = TaskStatus.valueOf(fields[3]); | ||
| String description = fields[4]; | ||
|
|
||
| switch (type) { | ||
| case TASK: | ||
| return new Task(id, name, description, status); | ||
| case EPIC: | ||
| Epic epic = new Epic(name, description); | ||
| epic.setId(id); | ||
| epic.setTaskStatus(status); | ||
| return epic; | ||
| case SUBTASK: | ||
| int epicId = fields.length > 5 ? Integer.parseInt(fields[5]) : 0; | ||
| Subtask subtask = new Subtask(name, description, status, epicId); | ||
| subtask.setId(id); | ||
| return subtask; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
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,209 @@ | ||
| package manager; | ||
|
|
||
| import tasks.*; | ||
| import manager.exceptions.ManagerSaveException; | ||
| import manager.exceptions.ManagerLoadException; | ||
| import java.io.*; | ||
| import java.nio.file.Files; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class FileBackedTaskManager extends InMemoryTaskManager { | ||
|
|
||
| private final File file; | ||
|
|
||
|
|
||
| public FileBackedTaskManager(File file, HistoryManager historyManager) { | ||
| this.file = file; | ||
| this.historyManager = historyManager; | ||
| } | ||
|
|
||
| // Загрузка данных из файла. | ||
| public static FileBackedTaskManager loadFromFile(File file) { | ||
| FileBackedTaskManager manager = new FileBackedTaskManager(file, Managers.getDefaultHistory()); | ||
|
|
||
| if (file.exists() && file.length() > 0) { | ||
| manager.load(); | ||
| } | ||
| return manager; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // Переопределенные методы Task | ||
| @Override | ||
| public Task getTask(int id) { | ||
| Task task = super.getTask(id); | ||
| save(); | ||
| return task; | ||
| } | ||
|
|
||
| @Override | ||
| public Task createTask(Task task) { | ||
| Task createdTask = super.createTask(task); | ||
| save(); | ||
| return createdTask; | ||
| } | ||
|
|
||
| @Override | ||
| public Task updateTask(Task task) { | ||
| Task updatedTask = super.updateTask(task); | ||
| save(); | ||
| return updatedTask; | ||
| } | ||
|
|
||
| @Override | ||
| public Task deleteTask(int id) { | ||
| Task deletedTask = super.deleteTask(id); | ||
| save(); | ||
| return deletedTask; | ||
| } | ||
|
|
||
| // Переопределенные методы Subtask | ||
| @Override | ||
| public Subtask getSubtasks(int id) { | ||
| Subtask subtask = super.getSubtasks(id); | ||
| save(); | ||
| return subtask; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask createSubtask(Subtask subtask) { | ||
| Subtask createdSubtask = super.createSubtask(subtask); | ||
| save(); | ||
| return createdSubtask; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask updateSubtask(Subtask subtask) { | ||
| Subtask updatedSubtask = super.updateSubtask(subtask); | ||
| save(); | ||
| return updatedSubtask; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask deleteSubtask(int id) { | ||
| Subtask deletedSubtask = super.deleteSubtask(id); | ||
| save(); | ||
| return deletedSubtask; | ||
| } | ||
|
|
||
| // Переопределенные методы Epic | ||
| @Override | ||
| public Epic getEpic(int id) { | ||
| Epic epic = super.getEpic(id); | ||
| save(); | ||
| return epic; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic createEpic(Epic epic) { | ||
| Epic createdEpic = super.createEpic(epic); | ||
| save(); | ||
| return createdEpic; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic updateEpic(Epic epic) { | ||
| Epic updatedEpic = super.updateEpic(epic); | ||
| save(); | ||
| return updatedEpic; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic deleteEpic(int id) { | ||
| Epic deletedEpic = super.deleteEpic(id); | ||
| save(); | ||
| return deletedEpic; | ||
| } | ||
|
|
||
| // Переопределенные методы удаления всех задач | ||
| @Override | ||
| public void deleteAllTasks() { | ||
| super.deleteAllTasks(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAllSubtasks() { | ||
| super.deleteAllSubtasks(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAllEpics() { | ||
| super.deleteAllEpics(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAll() { | ||
| super.deleteAll(); | ||
| save(); | ||
| } | ||
|
|
||
|
|
||
| // Загрузка данных | ||
| private void load() { | ||
| try { | ||
| String content = Files.readString(file.toPath()); | ||
| String[] lines = content.split("\n"); | ||
|
|
||
| if (lines.length < 2) return; | ||
|
|
||
| int maxId = 0; | ||
|
|
||
| for (int i = 1; i < lines.length; i++) { | ||
| Task task = CsvFormatter.fromString(lines[i]); | ||
| if (task == null) continue; | ||
|
|
||
| if (task.getId() > maxId) { | ||
| maxId = task.getId(); | ||
| } | ||
|
|
||
| switch (task.getType()) { | ||
| case TASK: | ||
| tasks.put(task.getId(), task); | ||
| break; | ||
| case EPIC: | ||
| epics.put(task.getId(), (Epic) task); | ||
| break; | ||
| case SUBTASK: | ||
| Subtask subtask = (Subtask) task; | ||
| subtasks.put(task.getId(), subtask); | ||
| Epic epic = epics.get(subtask.getEpicId()); | ||
| if (epic != null) { | ||
| epic.addSubtaskId(subtask.getId()); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| generatorId = maxId + 1; | ||
| } catch (IOException e) { | ||
| throw new ManagerLoadException("Не удалось загрузить задачи из файла: " + file.getPath(), e); | ||
| } | ||
| } | ||
|
|
||
| // Сохранение данных в файл | ||
| private void save() throws ManagerSaveException { | ||
| try (FileWriter writer = new FileWriter(file)) { | ||
| writer.write(CsvFormatter.CSV_HEADER + "\n"); // Запись заголовка | ||
| for (Task task : getAllTasks()) { | ||
| writer.write(CsvFormatter.toString(task) + "\n"); // Запись задач | ||
| } | ||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Ошибка сохранения файла", e); | ||
| } | ||
| } | ||
|
|
||
| // Получение всех задач. | ||
| private List<Task> getAllTasks() { | ||
| List<Task> tasks = new ArrayList<>(); | ||
| tasks.addAll(getTasks()); | ||
| tasks.addAll(getEpics()); | ||
| tasks.addAll(getSubtasks()); | ||
| return tasks; | ||
| } | ||
| } | ||
|
|
||
|
|
||
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,12 @@ | ||
| package manager.exceptions; | ||
|
|
||
| public class ManagerLoadException extends RuntimeException { | ||
|
|
||
| public ManagerLoadException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public ManagerLoadException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.