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
6 changes: 6 additions & 0 deletions resources/task.csv
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,Пойти на кухню и:,

12 changes: 3 additions & 9 deletions src/ru/yandex/javacource/golotin/schedule/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import ru.yandex.javacource.golotin.schedule.model.Status;
import ru.yandex.javacource.golotin.schedule.model.Subtask;
import ru.yandex.javacource.golotin.schedule.model.Task;
import ru.yandex.javacource.golotin.schedule.service.InMemoryTaskManager;

import ru.yandex.javacource.golotin.schedule.service.Manager;
import ru.yandex.javacource.golotin.schedule.service.TaskManager;

public class Main {
public static void main(String[] args) {
TaskManager taskManager = new InMemoryTaskManager();
TaskManager taskManager = Manager.getDefault();
taskManager.createTask(new Task("Дом", Status.NEW, "Убраться в кухни и ванной"));
taskManager.createTask(new Task("Работа", Status.IN_PROGRESS, "Сделать куча рутины и пойти домой:)"));

Expand All @@ -30,13 +31,6 @@ public static void main(String[] args) {
System.out.println(taskManager.getTasks());
System.out.println(taskManager.getEpics());

taskManager.deleteTask(1);
taskManager.deleteEpic(1);
taskManager.deleteSubtask(1);

taskManager.cleanTasks();
taskManager.cleanSubtasks();
taskManager.cleanEpics();

}
}
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);
}
}
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);
}
}
13 changes: 11 additions & 2 deletions src/ru/yandex/javacource/golotin/schedule/model/Epic.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package ru.yandex.javacource.golotin.schedule.model;

import java.util.ArrayList;
import java.util.List;

public class Epic extends Task {

private final ArrayList<Integer> subtaskIds = new ArrayList<>();
private final List<Integer> subtaskIds = new ArrayList<>();

public Epic(String name, Status status, String description) {
super(name, status, description);
}

public Epic(int id, String name, String description, Status status) {
super(name, status, description);
setId(id);
}

public void addSubtaskId(int id) {
subtaskIds.add(id);
Expand All @@ -23,10 +28,14 @@ public void removeSubtask(int id) {
subtaskIds.remove(id);
}

public ArrayList<Integer> getSubtaskIds() {
public List<Integer> getSubtaskIds() {
return subtaskIds;
}

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

@Override
public String toString() {
return "Epic{" +
Expand Down
14 changes: 13 additions & 1 deletion src/ru/yandex/javacource/golotin/schedule/model/Subtask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ public class Subtask extends Task {

private Integer epicId;

public Subtask(String name, Status status, String description,int epicId) {
public Subtask(String name, Status status, String description, int epicId) {

super(name, status, description);
setEpicId(epicId);
}

public Subtask(int id, String name, String description, Status status, Integer epicId) {
super(name, status, description);
setId(id);
setEpicId(epicId);
}

@Override
public Integer getEpicId() {
return epicId;
}

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

public void setEpicId(Integer epicId) {
this.epicId = epicId;
}
Expand Down
15 changes: 15 additions & 0 deletions src/ru/yandex/javacource/golotin/schedule/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public Task(String name, Status status, String description) {
this.description = description;
}

public Task(int id, String name, String description, Status status) {
setId(id);
this.name = name;
this.status = status;
this.description = description;
}

public int getId() {
return id;
}
Expand All @@ -26,6 +33,10 @@ public String getName() {
return name;
}

public Integer getEpicId() {
return null;
}

public void setName(String name) {
this.name = name;
}
Expand All @@ -34,6 +45,10 @@ public String getDescription() {
return description;
}

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

public void setDescription(String description) {
this.description = description;
}
Expand Down
7 changes: 7 additions & 0 deletions src/ru/yandex/javacource/golotin/schedule/model/TaskType.java
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
}
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 {

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) {
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) {
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) {
super.deleteTask(id);
saveToFile();
}

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

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