Skip to content

Commit dbb8ca4

Browse files
committed
Java sprint 7 v1.1
Удалены классы: TaskConverter, TaskData. Обновлены классы: FileBackedTaskManager,Manager. Добавлены методы в классы исключения; ManagerSaveException, NotFoundException Добавлена перегрузка конструкторов: Task,Subtask,Epic
1 parent 052b08d commit dbb8ca4

File tree

12 files changed

+169
-123
lines changed

12 files changed

+169
-123
lines changed

src/ru/yandex/javacource/golotin/schedule/converter/TaskConverter.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package ru.yandex.javacource.golotin.schedule.exception;
22

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

33
public class NotFoundException extends RuntimeException {
4+
public NotFoundException() {
5+
}
6+
47
public NotFoundException(String message) {
58
super(message);
69
}
10+
11+
public NotFoundException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public NotFoundException(Throwable cause) {
16+
super(cause);
17+
}
718
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public Epic(String name, Status status, String description) {
1111
super(name, status, description);
1212
}
1313

14-
14+
public Epic(int id,String name,String description,Status status){
15+
super(name, status, description);
16+
setId(id);
17+
}
1518
public void addSubtaskId(int id) {
1619
subtaskIds.add(id);
1720
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public Subtask(String name, Status status, String description,int epicId) {
99
super(name, status, description);
1010
setEpicId(epicId);
1111
}
12-
12+
public Subtask(int id,String name,String description,Status status,Integer epicId){
13+
super(name, status, description);
14+
setId(id);
15+
setEpicId(epicId);
16+
}
1317
@Override
1418
public Integer getEpicId() {
1519
return epicId;

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

Lines changed: 7 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
}

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/ru/yandex/javacource/golotin/schedule/service/FileBackedTaskManager.java

Lines changed: 120 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,140 +4,174 @@
44

55
import java.io.*;
66

7-
import java.util.ArrayList;
8-
import java.util.HashMap;
9-
import java.util.List;
7+
import java.nio.file.Files;
8+
109
import java.util.Map;
1110

12-
import ru.yandex.javacource.golotin.schedule.converter.TaskConverter;
1311
import ru.yandex.javacource.golotin.schedule.exception.ManagerSaveException;
1412
import ru.yandex.javacource.golotin.schedule.model.*;
1513

1614
public class FileBackedTaskManager extends InMemoryTaskManager {
17-
HashMap<TaskType, TaskConverter> converters;
18-
private final File file;
19-
public FileBackedTaskManager() {
20-
this(Manager.getDefaultHistory());
21-
}
2215

23-
public FileBackedTaskManager(HistoryManager historyManager) {
24-
this(historyManager, new File(TASK_CSV));
25-
}
16+
private static final String HEADER = "id,type,name,status,description,epic";
17+
private final File file;
2618

2719
public FileBackedTaskManager(File file) {
28-
this(Manager.getDefaultHistory(), file);
29-
}
30-
31-
32-
public FileBackedTaskManager(HistoryManager historyManager, File file) {
33-
super(historyManager);
20+
super(Manager.getDefaultHistory());
3421
this.file = file;
3522
}
3623

37-
public void initialization() {
38-
loadFromFile();
39-
}
40-
4124
public static FileBackedTaskManager loadFromFile(File file) {
42-
FileBackedTaskManager manager = new FileBackedTaskManager(file);
43-
manager.initialization();
44-
return manager;
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(STR."Невозможно прочитать файл: \{file.getName()}", e);
64+
}
65+
return taskManager;
4566
}
67+
4668
@Override
47-
public List<Task> getTasks() {
48-
return super.getTasks();
69+
public Task createTask(Task task) {
70+
Task newTask = super.createTask(task);
71+
saveToFile();
72+
return newTask;
4973
}
5074

5175
@Override
52-
public Task getTask(int id) {
53-
return super.getTask(id);
76+
public Epic createEpic(Epic epic) {
77+
Epic newEpic = super.createEpic(epic);
78+
saveToFile();
79+
return newEpic;
5480
}
5581

5682
@Override
57-
public Task createTask(Task task) {
58-
Task newTask = super.createTask(task);
83+
public Subtask createSubtask(Subtask subtask) {
84+
Subtask newSubtask = super.createSubtask(subtask);
5985
saveToFile();
60-
return newTask;
86+
return newSubtask;
6187
}
6288

6389
@Override
6490
public void updateTask(Task task) {
6591
super.updateTask(task);
92+
saveToFile();
6693
}
6794

6895
@Override
6996
public void updateEpic(Epic epic) {
7097
super.updateEpic(epic);
98+
saveToFile();
7199
}
72100

73101
@Override
74102
public void updateSubtask(Subtask subTask) {
75103
super.updateSubtask(subTask);
104+
saveToFile();
76105
}
77106

78107
@Override
79108
public void deleteTask(int id) {
80109
super.deleteTask(id);
110+
saveToFile();
111+
}
112+
113+
@Override
114+
public void deleteEpic(int id) {
115+
super.deleteEpic(id);
116+
saveToFile();
81117
}
82118

83119
@Override
84120
public void deleteSubtask(int id) {
85121
super.deleteSubtask(id);
122+
saveToFile();
86123
}
87124

88-
private String toString(Task task) {
89-
return STR."\{task.getId()},\{task.getName()},\{task.getDescription()},\{task.getEpicId()}";
125+
public static String toString(Task task) {
126+
return STR."\{task.getId()},\{task.getType()},\{task.getName()},\{task.getStatus()},\{task.getDescription()},\{task.getType().equals(TaskType.SUBTASK) ? task.getEpicId() : ""}";
90127
}
91128

92-
private Task fromString(String value) {
93-
final String[] columns = value.split(",");
94-
int id = 0;
95-
int epicId = 0;
96-
String name = "";
97-
String description = "";
98-
Status status = null;
99-
TaskType type = TaskType.valueOf(columns[1]);
100-
Task task = null;
101-
switch (type) {
102-
case TASK:
103-
task = new Task(name, status, description);
104-
break;
105-
case SUBTASK:
106-
task = new Subtask(name, status, description, epicId);
107-
break;
108-
case EPIC:
109-
task = new Epic(name, status, description);
110-
break;
111-
}
112-
return task;
113-
}
114129

115-
static String toString(HistoryManager manager) {
116-
String sb = "";
117-
manager.getAll();
118-
return sb;
119-
}
120-
121-
static List<Integer> historyFromString(String value) {
122-
final String[] ids = value.split(",");
123-
List<Integer> history = new ArrayList<>();
124-
for (String id : ids) {
125-
history.add(Integer.valueOf(id));
130+
public static Task taskFromString(String value) {
131+
final String[] values = value.split(",");
132+
final int id = Integer.parseInt(values[0]);
133+
final TaskType type = TaskType.valueOf(values[1]);
134+
final String name = values[2];
135+
final Status status = Status.valueOf(values[3]);
136+
final String description = values[4];
137+
if (type == TaskType.TASK) {
138+
return new Task(id, name, description, status);
139+
}
140+
if (type == TaskType.SUBTASK) {
141+
final int epicId = Integer.parseInt(values[5]);
142+
return new Subtask(id, name, description, status, epicId);
126143
}
127-
return history;
144+
145+
return new Epic(id, name, description, status);
128146
}
129147

148+
protected void saveToFile() {
149+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
150+
writer.write(HEADER);
151+
writer.newLine();
130152

131-
private void saveToFile() {// Сохранение в файл
132-
try (final BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
133153
for (Map.Entry<Integer, Task> entry : tasks.entrySet()) {
134-
writer.append(toString(entry.getValue()));
154+
final Task task = entry.getValue();
155+
writer.write(CSVTaskFormat.toString(task));
135156
writer.newLine();
136157
}
158+
159+
for (Map.Entry<Integer, Subtask> entry : subtasks.entrySet()) {
160+
final Task task = entry.getValue();
161+
writer.write(CSVTaskFormat.toString(task));
162+
writer.newLine();
163+
}
164+
165+
for (Map.Entry<Integer, Epic> entry : epics.entrySet()) {
166+
final Task task = entry.getValue();
167+
writer.write(CSVTaskFormat.toString(task));
168+
writer.newLine();
169+
}
170+
171+
writer.newLine();
137172
} catch (IOException e) {
138-
throw new ManagerSaveException(STR."Ошибка в файле: \{file.getAbsolutePath()}", e);
173+
throw new ManagerSaveException("Ошибка сохранения файла: " + file.getName(), e);
139174
}
140-
141175
}
142176

143177
private void loadFromFile() {// Чтение из в файла
@@ -146,10 +180,21 @@ private void loadFromFile() {// Чтение из в файла
146180
reader.readLine(); // Пропускаем заголовок
147181
while (true) {
148182
String line = reader.readLine();
149-
final Task task = fromString(line);
183+
final Task task = taskFromString(line);
150184
final int id = task.getId();
151-
if (task.getType() == TaskType.TASK) {
185+
if (task.getType() == TaskType.TASK) {// Задача
152186
tasks.put(id, task);
187+
} else if (task.getType() == TaskType.SUBTASK) {// Подзадачи
188+
subtasks.put(id, new Subtask(task.getId(), task.getName(), task.getDescription(), task.getStatus(),
189+
task.getEpicId()));
190+
} else if (task.getType() == TaskType.EPIC) {// Эпики
191+
epics.put(id, new Epic(task.getId(), task.getName(), task.getDescription(), task.getStatus()));
192+
for (Subtask subtask : subtasks.values()) {// Поиск подзадач эпика
193+
if (subtask.getEpicId() == task.getId()) {
194+
Epic epic = epics.get(task.getId());
195+
epic.addSubtaskId(subtask.getId());
196+
}
197+
}
153198
}
154199
if (maxId < id) {
155200
maxId = id;
@@ -158,12 +203,9 @@ private void loadFromFile() {// Чтение из в файла
158203
break;
159204
}
160205
}
161-
String line = reader.readLine();// История
162206
} catch (IOException e) {// Отлавливаем ошибки
163207
throw new ManagerSaveException(STR."Ошибка при чтении файла: \{file.getAbsolutePath()}", e);
164208
}
165209
counterId = maxId;// генератор
166210
}
167-
168-
public static final String TASK_CSV = "task.csv";
169211
}

0 commit comments

Comments
 (0)