Skip to content
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
28 changes: 20 additions & 8 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());*/

}

Expand Down
188 changes: 188 additions & 0 deletions src/manager/FileBackedTaskManager.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}


}
}
5 changes: 5 additions & 0 deletions src/manager/TaskType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package manager;

public enum TaskType {
TASK, SUBTASK, EPIC
}
15 changes: 15 additions & 0 deletions src/task/Epic.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package task;

import manager.TaskType;

import java.util.HashMap;
import java.util.Map;

public class Epic extends Task {
protected Map<Integer, Subtask> 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;

}

Expand Down
17 changes: 17 additions & 0 deletions src/task/Subtask.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions src/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package task;

import manager.TaskType;

import java.util.Objects;

public class Task {
private Integer id;
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) {
this.id = id;
this.name = name;
this.description = descriprion;
this.status = status;
this.type = TaskType.TASK;
}

public void setId(Integer id) {
Expand Down Expand Up @@ -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;
}
Expand Down
Loading
Loading