Skip to content
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

Сринт 7. Добавить функцию чтения и записи задач в CSV файл #3

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ru/alexgur/kanban/exceptions/ManageLoadException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.alexgur.kanban.exceptions;

public class ManageLoadException extends RuntimeException {
public ManageLoadException(final String message) {
super(message);
}
}
7 changes: 7 additions & 0 deletions src/ru/alexgur/kanban/exceptions/ManagerSaveException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.alexgur.kanban.exceptions;

public class ManagerSaveException extends RuntimeException {
public ManagerSaveException(final String message) {
super(message);
}
}
17 changes: 16 additions & 1 deletion src/ru/alexgur/kanban/model/Epic.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package ru.alexgur.kanban.model;

import java.util.List;

import ru.alexgur.kanban.service.TaskType;

import java.util.ArrayList;

public class Epic extends Task {

private List<Integer> subTasksIds = new ArrayList<>();

public Epic() {
super();
}

public Epic(int id) {
super(id);
}

public List<Integer> getSubTasksIds() {
return subTasksIds;
}
Expand All @@ -29,6 +39,11 @@ public void deleteSubTask(Integer subTasksId) {
subTasksIds.remove(subTasksId);
}

@Override
public TaskType getType() {
return TaskType.EPIC;
}

@Override
public String toString() {
return "Task [id=" + id + ", name=" + getName() + ", text=" + getText() + ", status=" + getStatus()
Expand Down
16 changes: 15 additions & 1 deletion src/ru/alexgur/kanban/model/SubTask.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package ru.alexgur.kanban.model;

public class SubTask extends Task {
import ru.alexgur.kanban.service.TaskType;

public class SubTask extends Task {
private int epicId = -1;

public SubTask() {
super();
}

public SubTask(int id) {
super(id);
}

public SubTask setEpicId(int epicId) {
if (this.epicId == -1 && this.id != epicId) {
this.epicId = epicId;
Expand All @@ -15,6 +24,11 @@ public int getEpicId() {
return this.epicId;
}

@Override
public TaskType getType() {
return TaskType.SUBTASK;
}

@Override
public String toString() {
return "Task [id=" + id + ", name=" + getName() + ", text=" + getText() + ", status=" + getStatus()
Expand Down
13 changes: 13 additions & 0 deletions src/ru/alexgur/kanban/model/Task.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.alexgur.kanban.model;

import ru.alexgur.kanban.service.Status;
import ru.alexgur.kanban.service.TaskType;

public class Task {
private static int globalId = 0; // значение для генерации id экземпляров
Expand All @@ -14,6 +15,18 @@ public Task() {
id = ++globalId;
}

public Task(int id) {
this.status = Status.NEW;
if (globalId < id) {
globalId = id;
}
this.id = id;
}

public TaskType getType() {
return TaskType.TASK;
}

public int getId() {
return id;
}
Expand Down
216 changes: 216 additions & 0 deletions src/ru/alexgur/kanban/service/FileBackedTaskManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package ru.alexgur.kanban.service;

import java.util.List;

import ru.alexgur.kanban.exceptions.ManageLoadException;
import ru.alexgur.kanban.exceptions.ManagerSaveException;
import ru.alexgur.kanban.model.Epic;
import ru.alexgur.kanban.model.SubTask;
import ru.alexgur.kanban.model.Task;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileBackedTaskManager extends InMemoryTaskManager {
private File fileToSave;
private String eol = "\n";
private String csvSplitter = ";";

@Override
public int addTask(Task task) {
int newId = super.addTask(task);
save();
return newId;
}

@Override
public int addEpic(Epic epic) {
int newId = super.addEpic(epic);
save();
return newId;
}

@Override
public int addSubTask(SubTask subTask) {
int newId = super.addSubTask(subTask);
save();
return newId;
}

@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();
}

@Override
public void deleteTask(int id) {
super.deleteTask(id);
save();
}

@Override
public void deleteTasks() {
super.deleteTasks();
save();
}

@Override
public void deleteSubTask(int id) {
super.deleteSubTask(id);
save();
}

@Override
public void deleteSubTasks() {
super.deleteSubTasks();
save();
}

@Override
public void deleteEpic(int id) {
super.deleteEpic(id);
save();
}

@Override
public void deleteEpics() {
super.deleteEpics();
save();
}

public File getFile() {
return fileToSave;
}

public void setFile(File fileToSave) {
this.fileToSave = fileToSave;
}

public String getEol() {
return eol;
}

public void setEol(String eol) {
this.eol = eol;
}

public String getCsvSplitter() {
return csvSplitter;
}

public void setCsvSplitter(String csvSplitter) {
this.csvSplitter = csvSplitter;
}

public static FileBackedTaskManager loadFromFile(File file) {
FileBackedTaskManager taskManager = new FileBackedTaskManager();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("id") || line.isEmpty()) {
continue;
}

Task task = (Task) taskManager.fromString(line);

if (task.getType() == TaskType.TASK) {
taskManager.addTask(task);
} else if (task.getType() == TaskType.SUBTASK) {
taskManager.addSubTask((SubTask) task);
} else if (task.getType() == TaskType.EPIC) {
taskManager.addEpic((Epic) task);
}
}
} catch (IOException e) {
throw new ManageLoadException("Произошла ошибка во время загрузки файла.");
}

taskManager.updateEpicsSubTasksIds();
taskManager.setFile(file);
return taskManager;
}

private void save() {
if (fileToSave == null) {
return;
}

try (FileWriter fileWriter = new FileWriter(fileToSave)) {
String csvHeader = "id,type,name,status,description,epic";
fileWriter.write(csvHeader + eol);

for (List<? extends Task> target : List.of(getTasks(), getSubTasks(), getEpics())) {
for (Task task : target) {
fileWriter.write(toString(task));
}
}
} catch (IOException e) {
throw new ManagerSaveException("Произошла ошибка во время записи файла.");
}
}

private <T extends Task> String toString(T task) {
String epicId = "";
if (task instanceof SubTask) {
epicId = String.valueOf(((SubTask) task).getEpicId());
}
return task.id
+ csvSplitter
+ task.getType()
+ csvSplitter
+ task.getName()
+ csvSplitter
+ task.getStatus()
+ csvSplitter
+ task.getText()
+ csvSplitter
+ epicId
+ csvSplitter
+ eol;
}

private Object fromString(String line) {
String[] args = line.trim().split(csvSplitter);

int id = Integer.valueOf(args[0]);
TaskType type = TaskType.valueOf(args[1]);
Status status = Status.valueOf(args[3]);
String name = args[2];
String text = args[4];

switch (type) {
case TASK:
Task task = new Task(id);
task.setName(name).setText(text).setStatus(status);
return task;
case SUBTASK:
int epicId = Integer.valueOf(args[5]);

SubTask subTask = new SubTask(id);
subTask.setName(name).setText(text).setStatus(status);
subTask.setEpicId(epicId);
return subTask;
case EPIC:
Epic epic = new Epic(id);
epic.setName(name).setText(text);
return epic;
}
return null;
}
}
Loading
Loading