generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint 7 solution in file manager #2
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
Open
Max-Browckin
wants to merge
16
commits into
main
Choose a base branch
from
sprint_7-solution-in-file-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0087d5b
commit 7
25f1cef
commit 8
a983b45
commit 9
10a757e
commit 10
9b3d3d1
commit 11
2c3695b
commit 12
c08dbf8
commit 13
73316fd
commit 14
b781730
commit 15
46194f8
Add saving tasks to a file
f0747a1
Add saving tasks to a file
a3c8ae8
Add saving tasks to a file
d5603a6
Error correction
f9a8384
Error correction
4a37c98
Error correction
2e78efb
implemented methods with conservation
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 was deleted.
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
Empty file.
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,172 @@ | ||
| package manager; | ||
|
|
||
| import model.Epic; | ||
| import model.Status; | ||
| import model.Subtask; | ||
| import model.Task; | ||
| import tasktype.TaskType; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.nio.file.Files; | ||
| import java.util.List; | ||
|
|
||
| public class FileBackedTaskManager extends InMemoryTaskManager { | ||
| private final File file; | ||
|
|
||
| public FileBackedTaskManager(File file) { | ||
| this.file = file; | ||
| } | ||
|
|
||
| @Override | ||
| public Task addTask(Task task) { | ||
| Task addedTask = super.addTask(task); | ||
| save(); | ||
| return addedTask; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic addEpic(Epic epic) { | ||
| Epic addedEpic = super.addEpic(epic); | ||
| save(); | ||
| return addedEpic; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask addSubtask(Subtask subtask) { | ||
| Subtask addedSubtask = super.addSubtask(subtask); | ||
| save(); | ||
| return addedSubtask; | ||
| } | ||
|
|
||
| @Override | ||
| public Task updateTask(Task updatedTask) { | ||
| Task existingTask = super.updateTask(updatedTask); | ||
| save(); | ||
| return existingTask; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic updateEpic(Epic updatedEpic) { | ||
| Epic existingEpic = super.updateEpic(updatedEpic); | ||
| save(); | ||
| return existingEpic; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask updateSubtask(Subtask updatedSubtask) { | ||
| Subtask existingSubtask = super.updateSubtask(updatedSubtask); | ||
| save(); | ||
| return existingSubtask; | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteTaskByID(int id) { | ||
| super.deleteTaskByID(id); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteEpicByID(int id) { | ||
| super.deleteEpicByID(id); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteSubtaskByID(int id) { | ||
| super.deleteSubtaskByID(id); | ||
| save(); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не забыть реализовать методы с сохранением данных в файл. 🙂 @Override
public void deleteTasks() {
@Override
public void deleteEpics() {
@Override
public void deleteSubtasks() {
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сделано |
||
| @Override | ||
| public void deleteTasks() { | ||
| super.deleteTasks(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteEpics() { | ||
| super.deleteEpics(); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteSubtasks() { | ||
| super.deleteSubtasks(); | ||
| save(); | ||
| } | ||
|
|
||
| public void save() { | ||
| try (PrintWriter writer = new PrintWriter(file)) { | ||
| writer.println("id,type,name,status,description,epic"); | ||
| for (Task task : getTasks()) { | ||
| writer.println(toString(task)); | ||
| } | ||
| for (Epic epic : getEpics()) { | ||
| writer.println(toString(epic)); | ||
| } | ||
| for (Subtask subtask : getSubtasks()) { | ||
| writer.println(toString(subtask)); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Failed to save tasks to file", e); | ||
| } | ||
| } | ||
|
|
||
| private String toString(Task task) { | ||
| switch (task.getType()) { | ||
| case SUBTASK: | ||
| return String.format("%d,SUBTASK,%s,%s,%s,%d", | ||
| task.getId(), task.getName(), task.getStatus(), task.getDescription(), ((Subtask) task).getEpicID()); | ||
| case EPIC: | ||
| return String.format("%d,EPIC,%s,%s,%s,", | ||
| task.getId(), task.getName(), task.getStatus(), task.getDescription()); | ||
| case TASK: | ||
| default: | ||
| return String.format("%d,TASK,%s,%s,%s,", | ||
| task.getId(), task.getName(), task.getStatus(), task.getDescription()); | ||
| } | ||
| } | ||
|
|
||
| public static FileBackedTaskManager loadFromFile(File file) { | ||
| FileBackedTaskManager manager = new FileBackedTaskManager(file); | ||
| try { | ||
| List<String> lines = Files.readAllLines(file.toPath()); | ||
| for (String line : lines.subList(1, lines.size())) { | ||
| Task task = fromString(line); | ||
| if (task instanceof Epic) { | ||
| manager.addEpic((Epic) task); | ||
| } else if (task instanceof Subtask) { | ||
| manager.addSubtask((Subtask) task); | ||
| } else { | ||
| manager.addTask(task); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Failed to load tasks from file", e); | ||
| } | ||
| return manager; | ||
| } | ||
|
|
||
| private static Task fromString(String value) { | ||
| String[] parts = value.split(","); | ||
| int id = Integer.parseInt(parts[0]); | ||
| String type = parts[1]; | ||
| String name = parts[2]; | ||
| Status status = Status.valueOf(parts[3]); | ||
| String description = parts[4]; | ||
|
|
||
| TaskType taskType = TaskType.valueOf(type); | ||
| switch (taskType) { | ||
| case SUBTASK: | ||
| int epicID = Integer.parseInt(parts[5]); | ||
| return new Subtask(id, name, description, status, epicID); | ||
| case EPIC: | ||
| return new Epic(id, name, description); | ||
| case TASK: | ||
| default: | ||
| return new Task(id, name, description, status); | ||
| } | ||
| } | ||
| } | ||
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
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.
Сюда ещё нужно добавить методы delete* update* для всех типов задач. В общем, все методы, которые изменяют содержимое хранилищ.