generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint 7 solution in file manager #4
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
9 commits
Select commit
Hold shift + click to select a range
cde6028
Создана заглушка класса FileBackedTaskManager для фз 7.
dendzim cc1ff4f
Добавлена заглушка для класса тестирования
dendzim c841e8e
Добавлены методы в классы для enum Type
dendzim 1079f79
Реализован менеджер записывающий данные в файл
dendzim 908b0e7
Добавлены тесты
dendzim 21d8770
Merge branch 'recovered-branch' into sprint_7-solution-in-file-manager
dendzim dd70796
исправлен check style
dendzim 93e7803
исправлен check style
dendzim a630c32
Внесены правки согласно замечаниям.
dendzim 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,4 @@ | ||
| id,type,name,status,description,epic | ||
| 1,TASK,Task001,NEW,Description,Task, | ||
| 2,EPIC,Epic001,NEW,Description,Epic, | ||
| 3,SUBTASK,Subtask001,NEW,Description,Subtask,2 |
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 exceptions; | ||
|
|
||
| public class ManagerSaveException extends RuntimeException { | ||
| public ManagerSaveException(String message, String ex) { | ||
| super(message); | ||
| } | ||
| } |
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,143 @@ | ||
| package managers; | ||
|
|
||
| import exceptions.ManagerSaveException; | ||
| import tasks.*; | ||
| import java.io.*; | ||
|
|
||
| public class FileBackedTaskManager extends InMemoryTaskManager { | ||
| private final File tasks; | ||
|
|
||
| public FileBackedTaskManager(File file) { | ||
| this.tasks = file; | ||
| } | ||
|
|
||
| public void save() { | ||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(tasks))) { | ||
| if (tasks.length() == 0) { //прописываем первую строку | ||
| writer.write("id,type,name,status,description,epic\n"); | ||
| } | ||
| for (Task task : getTaskList()) { | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
| for (Task task : getEpicList()) { | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
| for (Task task : getAllSubtask()) { | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
| } catch (IOException ex) { | ||
| throw new ManagerSaveException("Ошибка сохранения в файл " + tasks.getName(), ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public static FileBackedTaskManager loadFromFile(File file) { | ||
| final FileBackedTaskManager taskManager = new FileBackedTaskManager(file); | ||
| try (BufferedReader reader = new BufferedReader(new FileReader(file))) { | ||
| int idCounter = 0; | ||
| reader.readLine(); //пропуск первой строки | ||
| while (reader.ready()) { | ||
| Task task = fromString(reader.readLine()); | ||
| final int id = task.getId(); | ||
| if (idCounter < id) { | ||
| idCounter = id; | ||
| } | ||
| if (task.getType() == TaskType.TASK) { | ||
| taskManager.taskList.put(task.getId(), task); | ||
| } else if (task.getType() == TaskType.EPIC) { | ||
| taskManager.epicList.put(task.getId(), (Epic) task); | ||
| } else { | ||
| taskManager.subtaskList.put(task.getId(), (Subtask) task); | ||
| } | ||
| } | ||
| } catch (IOException ex) { | ||
| throw new ManagerSaveException("Ошибка чтения файла " + file.getName(), ex.getMessage()); | ||
| } | ||
| return taskManager; | ||
| } | ||
|
|
||
| public String toString(Task task) { | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
| stringBuilder.append(task.getId() + ","); | ||
| stringBuilder.append(task.getType() + ","); | ||
| stringBuilder.append(task.getTitle() + ","); | ||
| stringBuilder.append(task.getStatus() + ","); | ||
| stringBuilder.append(task.getDescription() + ","); | ||
| stringBuilder.append(task.getClass().getSimpleName() + ","); | ||
| if (task.getClass() == Subtask.class) { | ||
| stringBuilder.append(((Subtask) task).getEpicId()); | ||
| } | ||
| return stringBuilder.toString(); | ||
| } | ||
|
|
||
| public static Task fromString(String value) { | ||
| String[] str = value.split(","); | ||
|
|
||
| if (TaskType.valueOf(str[1]) == (TaskType.TASK)) { | ||
| Task task = new Task(str[2], str[4], TaskStatus.valueOf(str[3])); | ||
| task.setId(Integer.parseInt(str[0])); | ||
| return task; | ||
| } else if (TaskType.valueOf(str[1]) == (TaskType.EPIC)) { | ||
| Epic task = new Epic(str[2], str[4], TaskStatus.valueOf(str[3])); | ||
| task.setId(Integer.parseInt(str[0])); | ||
| return task; | ||
| } else { | ||
| Subtask task = new Subtask(str[2], str[4], Integer.parseInt(str[6]), TaskStatus.valueOf(str[3])); | ||
| task.setId(Integer.parseInt(str[0])); | ||
| return task; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addTask(Task task) { | ||
| super.addTask(task); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addEpic(Epic epic) { | ||
| super.addEpic(epic); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addSubtask(Subtask subtask) { | ||
| super.addSubtask(subtask); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTask(Task task) { | ||
| super.updateTask(task); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateSubtask(Subtask subtask) { | ||
| super.updateSubtask(subtask); | ||
| save(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateEpic(Epic epic) { | ||
| super.updateEpic(epic); | ||
| save(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| File file = new File("files/Task.txt"); | ||
| FileBackedTaskManager fileBackedTaskManager = new FileBackedTaskManager(file); | ||
| Task task001 = new Task("Task001", "Description", TaskStatus.NEW); | ||
| fileBackedTaskManager.addTask(task001); | ||
| Epic epic001 = new Epic("Epic001", "Description",TaskStatus.NEW); | ||
| fileBackedTaskManager.addEpic(epic001); | ||
| Subtask subtask001 = new Subtask("Subtask001", "Description", epic001.getId(), TaskStatus.NEW); | ||
| fileBackedTaskManager.addSubtask(subtask001); | ||
| fileBackedTaskManager.deleteAllEpic(); | ||
| fileBackedTaskManager.deleteAllSubtask(); | ||
| fileBackedTaskManager.deleteTaskList(); | ||
| FileBackedTaskManager fileBackedTaskManager1 = loadFromFile(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
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 tasks; | ||
|
|
||
| public enum TaskType { | ||
| TASK, | ||
| SUBTASK, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package managers; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import tasks.Task; | ||
| import tasks.TaskStatus; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class FileBackedTaskManagerTest { | ||
|
|
||
| File temp; | ||
| FileBackedTaskManager taskManager; | ||
|
|
||
| @BeforeEach | ||
| public void createHistoryManager() { | ||
| { | ||
| try { | ||
| temp = File.createTempFile("Test", ".txt"); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| taskManager = new FileBackedTaskManager(temp); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Сохранение пустого файла") | ||
| public void saveFile() { | ||
| taskManager.save(); | ||
| assertTrue(temp.exists()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Сохранение файлов") | ||
| public void saveTasksToFile() { | ||
| Task task001 = new Task ("Task001", "Description", TaskStatus.NEW); | ||
| taskManager.addTask(task001); | ||
| Task task002 = new Task ("Task002", "Description", TaskStatus.IN_PROGRESS); | ||
| taskManager.addTask(task002); | ||
| assertTrue(temp.exists()); | ||
| assertTrue(temp.length() > 0); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Загрузка файлов") | ||
| public void loadFile() { | ||
| FileBackedTaskManager taskManager1 = FileBackedTaskManager.loadFromFile(temp); | ||
| assertEquals(taskManager1.getTaskList().size(), taskManager.getTaskList().size()); | ||
| } | ||
| } |
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.
Хорошо бы еще проинициализировать idCounter максимальным значением из файла, чтобы не было коллизий при последующем добавлении задач в менеджер.