Skip to content

Commit 7af24ef

Browse files
authored
Merge pull request #2 from 1EVILGUN1/develop
Java sprint 7 v1.0
2 parents ba7db61 + df2672e commit 7af24ef

File tree

12 files changed

+298
-23
lines changed

12 files changed

+298
-23
lines changed

resources/task.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id,type,name,status,description,epic
2+
1,TASK,Дом,NEW,Убраться в кухни и ванной,
3+
2,TASK,Работа,IN_PROGRESS,Сделать куча рутины и пойти домой:),
4+
3,EPIC,Прогулка,NEW,Прежде чем погулять нужно:,
5+
4,EPIC,Приготовить кофе,NEW,Пойти на кухню и:,
6+

src/ru/yandex/javacource/golotin/schedule/Main.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import ru.yandex.javacource.golotin.schedule.model.Status;
55
import ru.yandex.javacource.golotin.schedule.model.Subtask;
66
import ru.yandex.javacource.golotin.schedule.model.Task;
7-
import ru.yandex.javacource.golotin.schedule.service.InMemoryTaskManager;
7+
8+
import ru.yandex.javacource.golotin.schedule.service.Manager;
89
import ru.yandex.javacource.golotin.schedule.service.TaskManager;
910

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

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

33-
taskManager.deleteTask(1);
34-
taskManager.deleteEpic(1);
35-
taskManager.deleteSubtask(1);
36-
37-
taskManager.cleanTasks();
38-
taskManager.cleanSubtasks();
39-
taskManager.cleanEpics();
4034

4135
}
4236
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.yandex.javacource.golotin.schedule.exception;
2+
3+
public class ManagerSaveException extends RuntimeException {
4+
public ManagerSaveException() {
5+
}
6+
7+
public ManagerSaveException(String message) {
8+
super(message);
9+
}
10+
11+
public ManagerSaveException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ManagerSaveException(Throwable cause) {
16+
super(cause);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.yandex.javacource.golotin.schedule.exception;
2+
3+
public class NotFoundException extends RuntimeException {
4+
public NotFoundException() {
5+
}
6+
7+
public NotFoundException(String message) {
8+
super(message);
9+
}
10+
11+
public NotFoundException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public NotFoundException(Throwable cause) {
16+
super(cause);
17+
}
18+
}

src/ru/yandex/javacource/golotin/schedule/model/Epic.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package ru.yandex.javacource.golotin.schedule.model;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
public class Epic extends Task {
67

7-
private final ArrayList<Integer> subtaskIds = new ArrayList<>();
8+
private final List<Integer> subtaskIds = new ArrayList<>();
89

910
public Epic(String name, Status status, String description) {
1011
super(name, status, description);
1112
}
1213

14+
public Epic(int id, String name, String description, Status status) {
15+
super(name, status, description);
16+
setId(id);
17+
}
1318

1419
public void addSubtaskId(int id) {
1520
subtaskIds.add(id);
@@ -23,10 +28,14 @@ public void removeSubtask(int id) {
2328
subtaskIds.remove(id);
2429
}
2530

26-
public ArrayList<Integer> getSubtaskIds() {
31+
public List<Integer> getSubtaskIds() {
2732
return subtaskIds;
2833
}
2934

35+
public TaskType getType() {
36+
return TaskType.EPIC;
37+
}
38+
3039
@Override
3140
public String toString() {
3241
return "Epic{" +

src/ru/yandex/javacource/golotin/schedule/model/Subtask.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@ public class Subtask extends Task {
44

55
private Integer epicId;
66

7-
public Subtask(String name, Status status, String description,int epicId) {
7+
public Subtask(String name, Status status, String description, int epicId) {
88

99
super(name, status, description);
1010
setEpicId(epicId);
1111
}
1212

13+
public Subtask(int id, String name, String description, Status status, Integer epicId) {
14+
super(name, status, description);
15+
setId(id);
16+
setEpicId(epicId);
17+
}
18+
19+
@Override
1320
public Integer getEpicId() {
1421
return epicId;
1522
}
1623

24+
@Override
25+
public TaskType getType() {
26+
return TaskType.SUBTASK;
27+
}
28+
1729
public void setEpicId(Integer epicId) {
1830
this.epicId = epicId;
1931
}

src/ru/yandex/javacource/golotin/schedule/model/Task.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public Task(String name, Status status, String description) {
1414
this.description = description;
1515
}
1616

17+
public Task(int id, String name, String description, Status status) {
18+
setId(id);
19+
this.name = name;
20+
this.status = status;
21+
this.description = description;
22+
}
23+
1724
public int getId() {
1825
return id;
1926
}
@@ -26,6 +33,10 @@ public String getName() {
2633
return name;
2734
}
2835

36+
public Integer getEpicId() {
37+
return null;
38+
}
39+
2940
public void setName(String name) {
3041
this.name = name;
3142
}
@@ -34,6 +45,10 @@ public String getDescription() {
3445
return description;
3546
}
3647

48+
public TaskType getType() {
49+
return TaskType.TASK;
50+
}
51+
3752
public void setDescription(String description) {
3853
this.description = description;
3954
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.yandex.javacource.golotin.schedule.model;
2+
3+
public enum TaskType {
4+
TASK,
5+
SUBTASK,
6+
EPIC
7+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package ru.yandex.javacource.golotin.schedule.service;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import java.io.*;
6+
7+
import java.nio.file.Files;
8+
9+
import java.util.Map;
10+
11+
import ru.yandex.javacource.golotin.schedule.exception.ManagerSaveException;
12+
import ru.yandex.javacource.golotin.schedule.model.*;
13+
14+
public class FileBackedTaskManager extends InMemoryTaskManager {
15+
16+
private static final String HEADER = "id,type,name,status,description,epic";
17+
private final File file;
18+
19+
public FileBackedTaskManager(File file) {
20+
super(Manager.getDefaultHistory());
21+
this.file = file;
22+
}
23+
24+
public static FileBackedTaskManager loadFromFile(File file) {
25+
final FileBackedTaskManager taskManager = new FileBackedTaskManager(file);
26+
try {
27+
final String csv = Files.readString(file.toPath());
28+
final String[] lines = csv.split(System.lineSeparator());
29+
int generatorId = 0;
30+
for (int i = 1; i < lines.length; i++) {
31+
String line = lines[i];
32+
if (line.isEmpty()) {
33+
break;
34+
}
35+
final Task task = taskFromString(line);
36+
final int id = task.getId();
37+
if (id > generatorId) {
38+
generatorId = id;
39+
}
40+
if (task.getType() == TaskType.TASK) {
41+
taskManager.createTask(task);
42+
} else if (task.getType() == TaskType.SUBTASK) {
43+
taskManager.createSubtask(new Subtask(task.getId(), task.getName(), task.getDescription(),
44+
task.getStatus(), task.getEpicId()));
45+
} else if (task.getType() == TaskType.EPIC) {
46+
taskManager.createEpic(new Epic(task.getId(), task.getName(), task.getDescription(),
47+
task.getStatus()));
48+
for (Subtask subtask : taskManager.subtasks.values()) {// Поиск подзадач эпика
49+
if (subtask.getEpicId() == task.getId()) {
50+
Epic epic = taskManager.epics.get(task.getId());
51+
epic.addSubtaskId(subtask.getId());
52+
}
53+
}
54+
}
55+
}
56+
for (Map.Entry<Integer, Subtask> e : taskManager.subtasks.entrySet()) {
57+
final Subtask subtask = e.getValue();
58+
final Epic epic = taskManager.epics.get(subtask.getEpicId());
59+
epic.addSubtaskId(subtask.getId());
60+
}
61+
taskManager.counterId = generatorId;
62+
} catch (IOException e) {
63+
throw new ManagerSaveException("Невозможно прочитать файл: " + file.getName(), e);
64+
}
65+
return taskManager;
66+
}
67+
68+
@Override
69+
public Task createTask(Task task) {
70+
Task newTask = super.createTask(task);
71+
saveToFile();
72+
return newTask;
73+
}
74+
75+
@Override
76+
public Epic createEpic(Epic epic) {
77+
Epic newEpic = super.createEpic(epic);
78+
saveToFile();
79+
return newEpic;
80+
}
81+
82+
@Override
83+
public Subtask createSubtask(Subtask subtask) {
84+
Subtask newSubtask = super.createSubtask(subtask);
85+
saveToFile();
86+
return newSubtask;
87+
}
88+
89+
@Override
90+
public void updateTask(Task task) {
91+
super.updateTask(task);
92+
saveToFile();
93+
}
94+
95+
@Override
96+
public void updateEpic(Epic epic) {
97+
super.updateEpic(epic);
98+
saveToFile();
99+
}
100+
101+
@Override
102+
public void updateSubtask(Subtask subTask) {
103+
super.updateSubtask(subTask);
104+
saveToFile();
105+
}
106+
107+
@Override
108+
public void deleteTask(int id) {
109+
super.deleteTask(id);
110+
saveToFile();
111+
}
112+
113+
@Override
114+
public void deleteEpic(int id) {
115+
super.deleteEpic(id);
116+
saveToFile();
117+
}
118+
119+
@Override
120+
public void deleteSubtask(int id) {
121+
super.deleteSubtask(id);
122+
saveToFile();
123+
}
124+
125+
public static String toString(Task task) {
126+
return task.getId() + "," + task.getType() + "," + task.getName() + "," + task.getStatus() + "," +
127+
task.getDescription() + "," + (task.getType().equals(TaskType.SUBTASK) ? task.getEpicId() : "");
128+
}
129+
130+
131+
public static Task taskFromString(String value) {
132+
final String[] values = value.split(",");
133+
final int id = Integer.parseInt(values[0]);
134+
final TaskType type = TaskType.valueOf(values[1]);
135+
final String name = values[2];
136+
final Status status = Status.valueOf(values[3]);
137+
final String description = values[4];
138+
if (type == TaskType.TASK) {
139+
return new Task(id, name, description, status);
140+
}
141+
if (type == TaskType.SUBTASK) {
142+
final int epicId = Integer.parseInt(values[5]);
143+
return new Subtask(id, name, description, status, epicId);
144+
}
145+
146+
return new Epic(id, name, description, status);
147+
}
148+
149+
protected void saveToFile() {
150+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
151+
writer.write(HEADER);
152+
writer.newLine();
153+
154+
for (Map.Entry<Integer, Task> entry : tasks.entrySet()) {
155+
final Task task = entry.getValue();
156+
writer.write(toString(task));
157+
writer.newLine();
158+
}
159+
160+
for (Map.Entry<Integer, Subtask> entry : subtasks.entrySet()) {
161+
final Task task = entry.getValue();
162+
writer.write(toString(task));
163+
writer.newLine();
164+
}
165+
166+
for (Map.Entry<Integer, Epic> entry : epics.entrySet()) {
167+
final Task task = entry.getValue();
168+
writer.write(toString(task));
169+
writer.newLine();
170+
}
171+
172+
writer.newLine();
173+
} catch (IOException e) {
174+
throw new ManagerSaveException("Ошибка сохранения файла: " + file.getName(), e);
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)