diff --git a/src/Main.java b/src/Main.java index beead38..4b1cca9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,25 +2,37 @@ import task.Epic; import task.Status; import task.Subtask; +import task.Task; + +import java.io.File; public class Main { public static void main(String[] args) { - TaskManager taskManager = Managers.getDefault(); - Epic epic1 = new Epic("Epic-1","Epic-1",Status.NEW); - Epic epic2 = new Epic("Epic-2","Epic-2",Status.NEW); - Subtask subtask1 = new Subtask("Subtask-1","Subtask-1 for Epic-1",Status.NEW,epic1); - Subtask subtask2 = new Subtask("Subtask-2","Subtask-2 for Epic-1",Status.NEW,epic1); - Subtask subtask3 = new Subtask("Subtask-3","Subtask-3 for Epic-1",Status.NEW,epic1); + File file = new File("src/loadFile.csv"); + System.out.println(FileBackedTaskManager.loadFromFile(file).getHistory()); + FileBackedTaskManager taskManager = new FileBackedTaskManager(Managers.getDefaultHistory()); + Epic epic1 = new Epic("Epic-1", "Epic-1", Status.IN_PROGRESS); + Epic epic2 = new Epic("Epic-2", "Epic-2", Status.NEW); + Task task1 = new Task("Task-1", "description for task-1", Status.NEW); + + + Subtask subtask1 = new Subtask("Subtask-1", "Subtask-1 for Epic-1", Status.DONE, epic1); + Subtask subtask2 = new Subtask("Subtask-2", "Subtask-2 for Epic-1", Status.IN_PROGRESS, epic1); + Subtask subtask3 = new Subtask("Subtask-3", "Subtask-3 for Epic-1", Status.NEW, epic1); + taskManager.createTask(task1); taskManager.createEpic(epic1); taskManager.createEpic(epic2); + taskManager.createSubtask(subtask1); taskManager.createSubtask(subtask2); taskManager.createSubtask(subtask3); - taskManager.getEpic(1); + + + /* taskManager.getEpic(1); System.out.println(taskManager.getHistory()); taskManager.getSubtask(3); System.out.println(taskManager.getHistory()); @@ -37,7 +49,7 @@ public static void main(String[] args) { taskManager.deleteSubtask(4); System.out.println(taskManager.getHistory()); taskManager.deleteEpic(1); - System.out.println(taskManager.getHistory()); + System.out.println(taskManager.getHistory());*/ } diff --git a/src/manager/FileBackedTaskManager.java b/src/manager/FileBackedTaskManager.java new file mode 100644 index 0000000..71e7281 --- /dev/null +++ b/src/manager/FileBackedTaskManager.java @@ -0,0 +1,188 @@ +package manager; + +import task.Epic; +import task.Status; +import task.Subtask; +import task.Task; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +public class FileBackedTaskManager extends InMemoryTaskManager implements TaskManager { + private String pathToFile = "src/file.csv"; + private Path file; + + + public Path getFile() { + return file; + } + + public String getPathToFile() { + return pathToFile; + } + + public FileBackedTaskManager(HistoryManager defaultHistory, Path file) { + super(defaultHistory); + this.pathToFile = file.toAbsolutePath().toString(); + this.file = file; + } + + public FileBackedTaskManager(HistoryManager defaultHistory) { + super(defaultHistory); + this.file = Paths.get(pathToFile); + + } + + + @Override + public Task createTask(Task task) { + super.createTask(task); + + save(); + return task; + } + + @Override + public Task createEpic(Epic epic) { + super.createEpic(epic); + + save(); + return epic; + + + } + + @Override + public Task createSubtask(Subtask subtask) { + super.createSubtask(subtask); + + save(); + return subtask; + + } + + + public String toString(Task task) { + return task.getId() + "," + task.getType() + "," + task.getName() + "," + task.getDescription() + "," + task.getStatus(); + } + + + public void save() { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(pathToFile))) { + + bw.write("id,type,name,description,status,epic\n"); + for (Task task : getTasks()) { + bw.write(toString(task) + "\n"); + } + for (Task epic : getEpics()) { + bw.write(toString(epic) + "\n"); + } + for (Subtask subtask : getSubtasks()) { + bw.write(toString(subtask) + "," + subtask.getEpic().getId() + "\n"); + } + + + } catch (IOException e) { + throw new ManagerSaveException(e); + } + + } + + public Task fromString(String value) { + String[] mass = value.split(","); + int id = Integer.parseInt(mass[0]); + String type = mass[1]; + String name = mass[2]; + String description = mass[3]; + String status = mass[4]; + + + if (type.equals("TASK")) { + Status taskStatus; + if (status.equals("NEW")) { + taskStatus = Status.NEW; + } else if (status.equals("DONE")) { + taskStatus = Status.DONE; + } else { + taskStatus = Status.IN_PROGRESS; + } + Task task = new Task(id, name, description, taskStatus); + task.setType(TaskType.TASK); + createTask(task); + return task; + } else if (type.equals("EPIC")) { + Status taskStatus; + if (status.equals("NEW")) { + taskStatus = Status.NEW; + } else if (status.equals("DONE")) { + taskStatus = Status.DONE; + } else { + taskStatus = Status.IN_PROGRESS; + } + Epic epic = new Epic(id, name, description, taskStatus); + epic.setType(TaskType.EPIC); + createEpic(epic); + return epic; + } else { + Status taskStatus; + int epicId = Integer.parseInt(mass[5]); + if (status.equals("NEW")) { + taskStatus = Status.NEW; + } else if (status.equals("DONE")) { + taskStatus = Status.DONE; + } else { + taskStatus = Status.IN_PROGRESS; + } + Subtask subtask = new Subtask(id, name, description, taskStatus, getEpic(epicId)); + subtask.setType(TaskType.SUBTASK); + createSubtask(subtask); + return subtask; + } + + + } + + public static FileBackedTaskManager loadFromFile(File file) { + FileBackedTaskManager fileBackedTaskManager = new FileBackedTaskManager(Managers.getDefaultHistory(),Paths.get(file.getPath())); + //теперь запись будет происходит в тот-же файл из которого была загрузка, а не в "file.csv" + try { + List lines = Files.readAllLines(file.toPath()); + for (int i = 1; i < lines.size(); i++) { + fileBackedTaskManager.fromString(lines.get(i)); + } + + } catch (IOException e) { + throw new ManagerLoadException(e); + } + + + return fileBackedTaskManager; + } + + public class ManagerSaveException extends RuntimeException { + + + public ManagerSaveException(final Throwable cause) { + super(cause); + } + + + } + + public static class ManagerLoadException extends RuntimeException { + + + public ManagerLoadException(final Throwable cause) { + super(cause); + } + + + } +} diff --git a/src/manager/TaskType.java b/src/manager/TaskType.java new file mode 100644 index 0000000..575ce98 --- /dev/null +++ b/src/manager/TaskType.java @@ -0,0 +1,5 @@ +package manager; + +public enum TaskType { + TASK, SUBTASK, EPIC +} diff --git a/src/task/Epic.java b/src/task/Epic.java index fd9d807..38ac560 100644 --- a/src/task/Epic.java +++ b/src/task/Epic.java @@ -1,19 +1,34 @@ package task; +import manager.TaskType; + import java.util.HashMap; import java.util.Map; public class Epic extends Task { protected Map subtaskList = new HashMap<>(); + TaskType type; public Epic(String name, String description, Status status) { super(name, description, status); + this.type = TaskType.EPIC; + + } + + @Override + public void setType(TaskType type) { + this.type = type; + } + @Override + public TaskType getType() { + return type; } public Epic(Integer id, String name, String description, Status status) { super(id, name, description, status); + this.type = TaskType.EPIC; } diff --git a/src/task/Subtask.java b/src/task/Subtask.java index 36fd11d..c694b3a 100644 --- a/src/task/Subtask.java +++ b/src/task/Subtask.java @@ -1,17 +1,34 @@ package task; +import manager.TaskType; + public class Subtask extends Task { Epic epic; + @Override + public TaskType getType() { + return type; + } + + TaskType type; + public Subtask(String name, String description, Status status, Epic epic) { super(name, description, status); this.epic = epic; + this.type = TaskType.SUBTASK; } public Subtask(Integer id, String name, String description, Status status, Epic epic) { super(id, name, description, status); this.epic = epic; + this.type = TaskType.SUBTASK; + + } + + @Override + public void setType(TaskType type) { + this.type = type; } public void setEpic(Epic epic) { diff --git a/src/task/Task.java b/src/task/Task.java index 9493958..dfd908a 100644 --- a/src/task/Task.java +++ b/src/task/Task.java @@ -1,5 +1,7 @@ package task; +import manager.TaskType; + import java.util.Objects; public class Task { @@ -7,11 +9,14 @@ public class Task { private String name; private String description; private Status status; + private TaskType type; + public Task(String name, String descriprion, Status status) { this.name = name; this.description = descriprion; this.status = status; + this.type = TaskType.TASK; } public Task(Integer id, String name, String descriprion, Status status) { @@ -19,6 +24,7 @@ public Task(Integer id, String name, String descriprion, Status status) { this.name = name; this.description = descriprion; this.status = status; + this.type = TaskType.TASK; } public void setId(Integer id) { @@ -50,6 +56,14 @@ public int hashCode() { return Objects.hashCode(id); } + public void setType(TaskType type) { + this.type = type; + } + + public TaskType getType() { + return type; + } + public Integer getId() { return id; } diff --git a/test/manager/FileBackedTaskManagerTest.java b/test/manager/FileBackedTaskManagerTest.java new file mode 100644 index 0000000..41ba3b1 --- /dev/null +++ b/test/manager/FileBackedTaskManagerTest.java @@ -0,0 +1,82 @@ +package manager; + +import org.junit.jupiter.api.Test; +import task.Epic; +import task.Status; +import task.Subtask; +import task.Task; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FileBackedTaskManagerTest { + @Test + void saveInFile() throws IOException { + + + FileBackedTaskManager fileBackedTaskManager = new FileBackedTaskManager(Managers.getDefaultHistory(), + Files.createTempFile("testFile", ".csv")); + Epic epic1 = new Epic("Epic-1", "Epic-1", Status.IN_PROGRESS); + Epic epic2 = new Epic("Epic-2", "Epic-2", Status.NEW); + Task task1 = new Task("Task-1", "description for task-1", Status.NEW); + + + Subtask subtask1 = new Subtask("Subtask-1", "Subtask-1 for Epic-1", Status.DONE, epic1); + Subtask subtask2 = new Subtask("Subtask-2", "Subtask-2 for Epic-1", Status.IN_PROGRESS, epic1); + Subtask subtask3 = new Subtask("Subtask-3", "Subtask-3 for Epic-1", Status.NEW, epic1); + fileBackedTaskManager.createTask(task1); + fileBackedTaskManager.createEpic(epic1); + fileBackedTaskManager.createEpic(epic2); + + fileBackedTaskManager.createSubtask(subtask1); + fileBackedTaskManager.createSubtask(subtask2); + fileBackedTaskManager.createSubtask(subtask3); + List newFile = Files.readAllLines(fileBackedTaskManager.getFile()); + List testFile = Files.readAllLines(Paths.get("test/manager/TestFile.csv")); + for (int i = 0; i < testFile.size(); i++) { + assertTrue(newFile.get(i).equals(testFile.get(i)), newFile.get(i) + " : " + testFile.get(i)); + } + assertEquals(newFile.size(), 7); + + } + + + @Test + void loadFromFile() throws IOException { + List loadFile = null; + + loadFile = Files.readAllLines(Paths.get("test/manager/TestFile.csv")); + + FileBackedTaskManager.loadFromFile(new File("test/manager/TestFile.csv")); + + List newFile = Files.readAllLines(Paths.get("test/manager/TestFile.csv")); + + for (int i = 0; i < loadFile.size(); i++) { + assertTrue(newFile.get(i).equals(loadFile.get(i)), newFile.get(i) + " : " + loadFile.get(i)); + + assertEquals(newFile.size(), loadFile.size()); + } + } + + @Test + void loadFromEmptyFile() throws IOException { + FileBackedTaskManager.loadFromFile(new File("test/manager/emptyFile.csv")); + + + List newFile = Files.readAllLines(Paths.get("test/manager/emptyFile.csv")); + + assertEquals(newFile.size(), 0); + + } +} + + + + + diff --git a/test/manager/TestFile.csv b/test/manager/TestFile.csv new file mode 100644 index 0000000..0cfd080 --- /dev/null +++ b/test/manager/TestFile.csv @@ -0,0 +1,7 @@ +id,type,name,description,status,epic +1,TASK,Task-1,description for task-1,NEW +2,EPIC,Epic-1,Epic-1,IN_PROGRESS +3,EPIC,Epic-2,Epic-2,NEW +4,SUBTASK,Subtask-1,Subtask-1 for Epic-1,DONE,2 +5,SUBTASK,Subtask-2,Subtask-2 for Epic-1,IN_PROGRESS,2 +6,SUBTASK,Subtask-3,Subtask-3 for Epic-1,NEW,2 diff --git a/test/manager/emptyFile.csv b/test/manager/emptyFile.csv new file mode 100644 index 0000000..e69de29