generated from yandex-praktikum/java-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
Java sprint 7 v1.0 #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
Merged
Changes from all commits
Commits
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,6 @@ | ||
| id,type,name,status,description,epic | ||
| 1,TASK,Дом,NEW,Убраться в кухни и ванной, | ||
| 2,TASK,Работа,IN_PROGRESS,Сделать куча рутины и пойти домой:), | ||
| 3,EPIC,Прогулка,NEW,Прежде чем погулять нужно:, | ||
| 4,EPIC,Приготовить кофе,NEW,Пойти на кухню и:, | ||
|
|
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
18 changes: 18 additions & 0 deletions
18
src/ru/yandex/javacource/golotin/schedule/exception/ManagerSaveException.java
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,18 @@ | ||
| package ru.yandex.javacource.golotin.schedule.exception; | ||
|
|
||
| public class ManagerSaveException extends RuntimeException { | ||
| public ManagerSaveException() { | ||
| } | ||
|
|
||
| public ManagerSaveException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public ManagerSaveException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
|
|
||
| public ManagerSaveException(Throwable cause) { | ||
| super(cause); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
src/ru/yandex/javacource/golotin/schedule/exception/NotFoundException.java
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,18 @@ | ||
| package ru.yandex.javacource.golotin.schedule.exception; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| public NotFoundException() { | ||
| } | ||
|
|
||
| public NotFoundException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public NotFoundException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
|
|
||
| public NotFoundException(Throwable cause) { | ||
| super(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
7 changes: 7 additions & 0 deletions
7
src/ru/yandex/javacource/golotin/schedule/model/TaskType.java
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 ru.yandex.javacource.golotin.schedule.model; | ||
|
|
||
| public enum TaskType { | ||
| TASK, | ||
| SUBTASK, | ||
| EPIC | ||
| } |
177 changes: 177 additions & 0 deletions
177
src/ru/yandex/javacource/golotin/schedule/service/FileBackedTaskManager.java
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,177 @@ | ||
| package ru.yandex.javacource.golotin.schedule.service; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| import java.io.*; | ||
|
|
||
| import java.nio.file.Files; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import ru.yandex.javacource.golotin.schedule.exception.ManagerSaveException; | ||
| import ru.yandex.javacource.golotin.schedule.model.*; | ||
|
|
||
| public class FileBackedTaskManager extends InMemoryTaskManager { | ||
1EVILGUN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static final String HEADER = "id,type,name,status,description,epic"; | ||
| private final File file; | ||
|
|
||
| public FileBackedTaskManager(File file) { | ||
| super(Manager.getDefaultHistory()); | ||
| this.file = file; | ||
| } | ||
|
|
||
| public static FileBackedTaskManager loadFromFile(File file) { | ||
1EVILGUN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final FileBackedTaskManager taskManager = new FileBackedTaskManager(file); | ||
| try { | ||
| final String csv = Files.readString(file.toPath()); | ||
| final String[] lines = csv.split(System.lineSeparator()); | ||
| int generatorId = 0; | ||
| for (int i = 1; i < lines.length; i++) { | ||
| String line = lines[i]; | ||
| if (line.isEmpty()) { | ||
| break; | ||
| } | ||
| final Task task = taskFromString(line); | ||
| final int id = task.getId(); | ||
| if (id > generatorId) { | ||
| generatorId = id; | ||
| } | ||
| if (task.getType() == TaskType.TASK) { | ||
| taskManager.createTask(task); | ||
| } else if (task.getType() == TaskType.SUBTASK) { | ||
| taskManager.createSubtask(new Subtask(task.getId(), task.getName(), task.getDescription(), | ||
| task.getStatus(), task.getEpicId())); | ||
| } else if (task.getType() == TaskType.EPIC) { | ||
| taskManager.createEpic(new Epic(task.getId(), task.getName(), task.getDescription(), | ||
| task.getStatus())); | ||
| for (Subtask subtask : taskManager.subtasks.values()) {// Поиск подзадач эпика | ||
| if (subtask.getEpicId() == task.getId()) { | ||
| Epic epic = taskManager.epics.get(task.getId()); | ||
| epic.addSubtaskId(subtask.getId()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| for (Map.Entry<Integer, Subtask> e : taskManager.subtasks.entrySet()) { | ||
| final Subtask subtask = e.getValue(); | ||
| final Epic epic = taskManager.epics.get(subtask.getEpicId()); | ||
| epic.addSubtaskId(subtask.getId()); | ||
| } | ||
| taskManager.counterId = generatorId; | ||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Невозможно прочитать файл: " + file.getName(), e); | ||
| } | ||
| return taskManager; | ||
| } | ||
|
|
||
| @Override | ||
| public Task createTask(Task task) { | ||
| Task newTask = super.createTask(task); | ||
| saveToFile(); | ||
| return newTask; | ||
| } | ||
|
|
||
| @Override | ||
| public Epic createEpic(Epic epic) { | ||
| Epic newEpic = super.createEpic(epic); | ||
| saveToFile(); | ||
| return newEpic; | ||
| } | ||
|
|
||
| @Override | ||
| public Subtask createSubtask(Subtask subtask) { | ||
| Subtask newSubtask = super.createSubtask(subtask); | ||
| saveToFile(); | ||
| return newSubtask; | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTask(Task task) { | ||
1EVILGUN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super.updateTask(task); | ||
| saveToFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateEpic(Epic epic) { | ||
| super.updateEpic(epic); | ||
| saveToFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateSubtask(Subtask subTask) { | ||
| super.updateSubtask(subTask); | ||
| saveToFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteTask(int id) { | ||
1EVILGUN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super.deleteTask(id); | ||
| saveToFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteEpic(int id) { | ||
| super.deleteEpic(id); | ||
| saveToFile(); | ||
| } | ||
|
|
||
1EVILGUN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public void deleteSubtask(int id) { | ||
| super.deleteSubtask(id); | ||
| saveToFile(); | ||
| } | ||
|
|
||
| public static String toString(Task task) { | ||
| return task.getId() + "," + task.getType() + "," + task.getName() + "," + task.getStatus() + "," + | ||
| task.getDescription() + "," + (task.getType().equals(TaskType.SUBTASK) ? task.getEpicId() : ""); | ||
| } | ||
|
|
||
|
|
||
| public static Task taskFromString(String value) { | ||
| final String[] values = value.split(","); | ||
| final int id = Integer.parseInt(values[0]); | ||
| final TaskType type = TaskType.valueOf(values[1]); | ||
| final String name = values[2]; | ||
| final Status status = Status.valueOf(values[3]); | ||
| final String description = values[4]; | ||
| if (type == TaskType.TASK) { | ||
| return new Task(id, name, description, status); | ||
| } | ||
| if (type == TaskType.SUBTASK) { | ||
| final int epicId = Integer.parseInt(values[5]); | ||
| return new Subtask(id, name, description, status, epicId); | ||
| } | ||
|
|
||
| return new Epic(id, name, description, status); | ||
| } | ||
|
|
||
| protected void saveToFile() { | ||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | ||
| writer.write(HEADER); | ||
| writer.newLine(); | ||
|
|
||
| for (Map.Entry<Integer, Task> entry : tasks.entrySet()) { | ||
| final Task task = entry.getValue(); | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| for (Map.Entry<Integer, Subtask> entry : subtasks.entrySet()) { | ||
| final Task task = entry.getValue(); | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| for (Map.Entry<Integer, Epic> entry : epics.entrySet()) { | ||
| final Task task = entry.getValue(); | ||
| writer.write(toString(task)); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| writer.newLine(); | ||
| } catch (IOException e) { | ||
| throw new ManagerSaveException("Ошибка сохранения файла: " + file.getName(), e); | ||
| } | ||
| } | ||
| } | ||
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.