Skip to content

Commit 400a3cd

Browse files
committed
Java sprint 8 v1.1
исправлен startTime,duration. Поправлена работа файлового менеджера. Дописал тест к новым методам менеджера. Также добавил класс тест для файлового менеджера.
1 parent 767fb14 commit 400a3cd

File tree

9 files changed

+196
-93
lines changed

9 files changed

+196
-93
lines changed

resources/task.csv

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

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
public class Main {
1212
public static void main(String[] args) {
1313
TaskManager taskManager = Manager.getDefault();
14-
taskManager.createTask(new Task("Дом", Status.NEW, "Убраться в кухни и ванной"));
15-
taskManager.createTask(new Task("Работа", Status.IN_PROGRESS, "Сделать куча рутины и пойти домой:)"));
16-
17-
taskManager.createEpic(new Epic("Прогулка", Status.NEW, "Прежде чем погулять нужно:"));
18-
taskManager.createSubtask(new Subtask("Уборка", Status.NEW, "Убраться в квартире", 1));
19-
taskManager.createSubtask(new Subtask("Одежда", Status.NEW, "Подготовить одежду к прогулке", 1));
20-
21-
taskManager.createEpic(new Epic("Приготовить кофе", Status.NEW, "Пойти на кухню и:"));
22-
taskManager.createSubtask(new Subtask("Сделать кофе", Status.NEW, "Налить в кружку горячую воду и наспать кофе", 2));
23-
24-
taskManager.updateTask(new Task("Дом", Status.IN_PROGRESS, "Уборка в кухни и ванной"));
25-
26-
taskManager.updateEpic(new Epic("Прогулка", Status.NEW, "Не пойду гулять"));
27-
taskManager.updateSubtask(new Subtask("Уборка", Status.IN_PROGRESS, "Убираюсь)", 1));
28-
taskManager.updateSubtask(new Subtask("Сделать кофе", Status.DONE, "Кофе приготовлено", 1));
14+
// taskManager.createTask(new Task("Дом", Status.NEW, "Убраться в кухни и ванной"));
15+
// taskManager.createTask(new Task("Работа", Status.IN_PROGRESS, "Сделать куча рутины и пойти домой:)"));
16+
//
17+
// taskManager.createEpic(new Epic("Прогулка", Status.NEW, "Прежде чем погулять нужно:"));
18+
// taskManager.createSubtask(new Subtask("Уборка", Status.NEW, "Убраться в квартире", 1));
19+
// taskManager.createSubtask(new Subtask("Одежда", Status.NEW, "Подготовить одежду к прогулке", 1));
20+
//
21+
// taskManager.createEpic(new Epic("Приготовить кофе", Status.NEW, "Пойти на кухню и:"));
22+
// taskManager.createSubtask(new Subtask("Сделать кофе", Status.NEW, "Налить в кружку горячую воду и наспать кофе", 2));
23+
//
24+
// taskManager.updateTask(new Task("Дом", Status.IN_PROGRESS, "Уборка в кухни и ванной"));
25+
//
26+
// taskManager.updateEpic(new Epic("Прогулка", Status.NEW, "Не пойду гулять"));
27+
// taskManager.updateSubtask(new Subtask("Уборка", Status.IN_PROGRESS, "Убираюсь)", 1));
28+
// taskManager.updateSubtask(new Subtask("Сделать кофе", Status.DONE, "Кофе приготовлено", 1));
2929

3030

3131
System.out.println(taskManager.getTasks());

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import java.time.Duration;
44
import java.time.Instant;
5+
import java.time.LocalDateTime;
56
import java.util.ArrayList;
67
import java.util.List;
78

89
public class Epic extends Task {
910

1011
private final List<Integer> subtaskIds = new ArrayList<>();
12+
private LocalDateTime endTime;
1113

12-
public Epic(String name, Status status, String description, Instant startTime, int duration) {
14+
public Epic(String name, Status status, String description, LocalDateTime startTime, long duration) {
1315
super(name, status, description, startTime, duration);
1416
}
1517

16-
public Epic(int id, String name, String description, Status status, Instant startTime, int duration) {
18+
public Epic(int id, String name, String description, Status status, LocalDateTime startTime, long duration) {
1719
super(name, status, description, startTime, duration);
1820
setId(id);
1921
}
@@ -38,15 +40,15 @@ public TaskType getType() {
3840
return TaskType.EPIC;
3941
}
4042

41-
public void setSumDurationSubtasks(Duration duration) {
42-
setDuration(getDuration().plus(duration).toMinutesPart());
43+
public void setEndTime(LocalDateTime endTime) {
44+
this.endTime = endTime;
4345
}
4446

4547
@Override
4648
public String toString() {
4749
return "Epic{" +
4850
"subtaskIds=" + subtaskIds +
51+
", endTime=" + endTime +
4952
'}';
5053
}
51-
5254
}

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

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

33
import java.time.Instant;
4+
import java.time.LocalDateTime;
45

56
public class Subtask extends Task {
67

78
private Integer epicId;
89

9-
public Subtask(String name, Status status, String description, Instant startTime, int duration, int epicId) {
10+
public Subtask(String name, Status status, String description, LocalDateTime startTime, long duration, int epicId) {
1011

1112
super(name, status, description, startTime, duration);
1213
setEpicId(epicId);
1314
}
1415

15-
public Subtask(int id, String name, String description, Status status, Instant startTime, int duration, Integer epicId) {
16+
public Subtask(int id, String name, String description, Status status, LocalDateTime startTime, long duration, Integer epicId) {
1617
super(name, status, description, startTime, duration);
1718
setId(id);
1819
setEpicId(epicId);

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

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
package ru.yandex.javacource.golotin.schedule.model;
22

33
import java.time.Duration;
4+
import java.time.LocalDateTime;
45
import java.util.Objects;
56
import java.time.Instant;
6-
import java.time.temporal.ChronoUnit;
77

88
public class Task {
99
private int id;
1010
private String name;
1111
private String description;
1212
private Status status;
1313

14-
private Instant startTime; // LocalDateTime
14+
private LocalDateTime startTime; // LocalDateTime
1515
private Duration duration; // минуты или Duration
16-
private Instant endTime;
1716

18-
public Task(String name, Status status, String description, Instant startTime, int duration) {
17+
public Task(String name, Status status, String description, LocalDateTime startTime, long duration) {
1918
this.name = name;
2019
this.status = status;
2120
this.description = description;
22-
this.startTime = startTime;
21+
this.startTime = LocalDateTime.from(startTime);
2322
this.duration = Duration.ofMinutes(duration);
24-
this.endTime = startTime.plus(duration, ChronoUnit.MINUTES);
2523
}
2624

27-
public Task(int id, String name, String description, Status status, Instant startTime, int duration) {
25+
public Task(int id, String name, String description, Status status, LocalDateTime startTime, long duration) {
2826
setId(id);
2927
this.name = name;
3028
this.status = status;
3129
this.description = description;
3230
this.startTime = startTime;
3331
this.duration = Duration.ofMinutes(duration);
34-
this.endTime = startTime.plus(duration, ChronoUnit.MINUTES);
3532
}
3633

3734
public int getId() {
@@ -74,42 +71,26 @@ public void setStatus(Status status) {
7471
this.status = status;
7572
}
7673

77-
public Instant getStartTime() {
74+
public LocalDateTime getStartTime() {
7875
return startTime;
7976
}
8077

81-
public void setStartTime(Instant startTime) {
78+
public void setStartTime(LocalDateTime startTime) {
8279
this.startTime = startTime;
8380
}
8481

85-
public Duration getDuration() {
86-
return duration;
82+
public long getDuration() {
83+
return duration.toMinutesPart();
8784
}
8885

89-
public void setDuration(int duration) {
86+
public void setDuration(long duration) {
9087
this.duration = Duration.ofMinutes(duration);
9188
}
9289

93-
public Instant getEndTime() {
94-
return endTime;
95-
}
96-
97-
public void setEndTime(Instant endTime) {
98-
this.endTime = endTime;
90+
public LocalDateTime getEndTime() {
91+
return startTime.plus(duration);
9992
}
10093

101-
@Override
102-
public boolean equals(Object o) {
103-
if (this == o) return true;
104-
if (o == null || getClass() != o.getClass()) return false;
105-
Task task = (Task) o;
106-
return id == task.id;
107-
}
108-
109-
@Override
110-
public int hashCode() {
111-
return Objects.hash(id);
112-
}
11394

11495
@Override
11596
public String toString() {
@@ -118,8 +99,21 @@ public String toString() {
11899
", name='" + name + '\'' +
119100
", description='" + description + '\'' +
120101
", status=" + status +
102+
", startTime=" + startTime +
103+
", endTime=" + getEndTime() +
121104
'}';
122105
}
123106

107+
@Override
108+
public boolean equals(Object o) {
109+
if (this == o) return true;
110+
if (o == null || getClass() != o.getClass()) return false;
111+
Task task = (Task) o;
112+
return id == task.id && Objects.equals(name, task.name) && Objects.equals(description, task.description) && status == task.status && Objects.equals(startTime, task.startTime) && Objects.equals(duration, task.duration);
113+
}
124114

115+
@Override
116+
public int hashCode() {
117+
return Objects.hash(id, name, description, status, startTime, duration);
118+
}
125119
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.time.Duration;
1010
import java.time.Instant;
11+
import java.time.LocalDateTime;
1112
import java.util.Map;
1213

1314
import ru.yandex.javacource.golotin.schedule.exception.ManagerSaveException;
@@ -43,10 +44,10 @@ public static FileBackedTaskManager loadFromFile(File file) {
4344
taskManager.createTask(task);
4445
} else if (task.getType() == TaskType.SUBTASK) {
4546
taskManager.createSubtask(new Subtask(task.getId(), task.getName(), task.getDescription(),
46-
task.getStatus(), task.getStartTime(), task.getDuration().toMinutesPart(), task.getEpicId()));
47+
task.getStatus(), task.getStartTime(), task.getDuration(), task.getEpicId()));
4748
} else if (task.getType() == TaskType.EPIC) {
4849
taskManager.createEpic(new Epic(task.getId(), task.getName(), task.getDescription(),
49-
task.getStatus(), task.getStartTime(), task.getDuration().toMinutesPart()));
50+
task.getStatus(), task.getStartTime(), task.getDuration()));
5051
for (Subtask subtask : taskManager.subtasks.values()) {// Поиск подзадач эпика
5152
if (subtask.getEpicId() == task.getId()) {
5253
Epic epic = taskManager.epics.get(task.getId());
@@ -126,7 +127,7 @@ public void deleteSubtask(int id) {
126127

127128
public static String toString(Task task) {
128129
return task.getId() + "," + task.getType() + "," + task.getName() + "," + task.getStatus() + "," +
129-
task.getDescription() + "," + (task.getType().equals(TaskType.SUBTASK) ? task.getEpicId() : "");
130+
task.getDescription() + "," + (task.getType().equals(TaskType.SUBTASK) ? task.getEpicId() : ""+task.getStartTime()+","+task.getEndTime());
130131
}
131132

132133

@@ -137,8 +138,8 @@ public static Task taskFromString(String value) {
137138
final String name = values[2];
138139
final Status status = Status.valueOf(values[3]);
139140
final String description = values[4];
140-
final Instant startTime = Instant.parse(values[5]);
141-
final Duration duration = Duration.parse(values[6]);
141+
final LocalDateTime startTime = LocalDateTime.parse(values[5]);
142+
final Duration duration = Duration.between(LocalDateTime.parse(values[5]), LocalDateTime.parse(values[6]));
142143
if (type == TaskType.TASK) {
143144
return new Task(id, name, description, status, startTime, duration.toMinutesPart());
144145
}

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

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ru.yandex.javacource.golotin.schedule.model.Subtask;
88
import ru.yandex.javacource.golotin.schedule.model.Task;
99

10+
import java.time.LocalDateTime;
1011
import java.util.*;
1112
import java.util.stream.Collectors;
1213

@@ -17,13 +18,14 @@ public class InMemoryTaskManager implements TaskManager {
1718
protected final Map<Integer, Epic> epics;
1819
protected final Map<Integer, Subtask> subtasks;
1920
protected final HistoryManager historyManager;
20-
protected final Set<Task> prioritizedTasks = new TreeSet<>(Comparator.comparing(Task::getStartTime));
21+
protected final Set<Task> prioritizedTasks;
2122

2223
public InMemoryTaskManager(HistoryManager historyManager) {
2324
this.historyManager = historyManager; // 3
2425
this.tasks = new HashMap<>();
2526
this.epics = new HashMap<>();
2627
this.subtasks = new HashMap<>();
28+
this.prioritizedTasks = new TreeSet<>(Comparator.comparing(Task::getStartTime));
2729
}
2830

2931
@Override
@@ -52,7 +54,7 @@ public Subtask createSubtask(Subtask subtask) {// создание Subtask
5254
if (epic == null) {
5355
return null;
5456
}
55-
epic.setSumDurationSubtasks(subtask.getDuration());
57+
updateEpicDuration(epic);
5658
final int id = ++counterId;
5759
subtask.setId(id);
5860
addPriorityTask(subtask);
@@ -97,7 +99,7 @@ public void updateSubtask(Subtask subtask) {// обновление Subtask
9799
}
98100
addPriorityTask(savedSubtask);
99101
subtasks.put(subtask.getId(), subtask);
100-
updateEpicStatus(epicId);// обновление статуса у Epic
102+
updateEpic(epicId);// обновление статуса у Epic
101103
}
102104

103105
@Override
@@ -160,12 +162,7 @@ public List<Epic> getEpics() {
160162
@Override
161163
public List<Subtask> getEpicSubtasks(int epicId) {// получаем список Epic с Subtasks
162164
Epic epic = epics.get(epicId);
163-
// ArrayList<Subtask> getSubtasks = null;
164-
// for (Integer subtaskId : epic.getSubtaskIds()) {
165-
// if (this.subtasks.containsKey(subtaskId)) {
166-
// getSubtasks.add(this.subtasks.get(subtaskId));
167-
// }
168-
// }
165+
updateEpic(epicId);
169166
return epic.getSubtaskIds().stream().map(subtasks::get).collect(Collectors.toList());
170167
}
171168

@@ -211,11 +208,6 @@ private void updateEpicStatus(int epicId) {// обновление статус
211208
.filter(this.subtasks::containsKey)
212209
.map(this.subtasks::get)
213210
.toList();
214-
// for (Integer subtaskId : epic.getSubtaskIds()) {
215-
// if (this.subtasks.containsKey(subtaskId)) {
216-
// subtasks.add(this.subtasks.get(subtaskId));
217-
// }
218-
// }
219211
for (Subtask statusSubtask : subtasks) {
220212
short subtaskNew = 0;
221213
short subtaskDone = 0;
@@ -240,12 +232,54 @@ private void updateEpicStatus(int epicId) {// обновление статус
240232
}
241233
}
242234

235+
private void updateEpicDuration(Epic epic) {
236+
List<Integer> subs = epic.getSubtaskIds();
237+
if (subs.isEmpty()) {
238+
epic.setDuration(0L);
239+
return;
240+
}
241+
LocalDateTime start = LocalDateTime.MAX;
242+
LocalDateTime end = LocalDateTime.MIN;
243+
long duration = 0L;
244+
for (int id : subs) {
245+
final Subtask subtask = subtasks.get(id);
246+
final LocalDateTime startTime = subtask.getStartTime();
247+
final LocalDateTime endTime = subtask.getEndTime();
248+
if (startTime.isBefore(start)) {
249+
start = startTime;
250+
}
251+
if (endTime.isAfter(end)) {
252+
end = endTime;
253+
}
254+
duration += subtask.getDuration();
255+
}
256+
epic.setDuration(duration);
257+
epic.setStartTime(start);
258+
epic.setEndTime(end);
259+
}
260+
261+
protected void updateEpic(int epicId) {
262+
Epic epic = epics.get(epicId);
263+
updateEpicStatus(epicId);
264+
updateEpicDuration(epic);
265+
}
266+
243267
private void addPriorityTask(Task task) {
268+
final LocalDateTime startTime = task.getStartTime();
269+
final LocalDateTime endTime = task.getEndTime();
244270
for (Task t : prioritizedTasks) {
245-
if (t.getStartTime() == task.getStartTime()) {
246-
throw new ManagerSaveException("Пересечение с задачей " + task.toString());
271+
final LocalDateTime existStart = t.getStartTime();
272+
final LocalDateTime existEnd = t.getEndTime();
273+
if (!endTime.isAfter(existStart)) {
274+
continue;
247275
}
276+
if (!existEnd.isAfter(startTime)) {
277+
continue;
278+
}
279+
280+
throw new ManagerSaveException("Задача пересекаются с id=" + t.getId() + " c " + existStart + " по " + existEnd);
248281
}
282+
249283
prioritizedTasks.add(task);
250284
}
251285

0 commit comments

Comments
 (0)