Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Спринт 8 #4

Merged
merged 5 commits into from
Dec 21, 2024
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Верхний регистр для констант
[*.{java,scala,js,py}]
upper_case_constants = true
7 changes: 7 additions & 0 deletions src/ru/alexgur/kanban/exceptions/ManagerAddTaskException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.alexgur.kanban.exceptions;

public class ManagerAddTaskException extends RuntimeException {
public ManagerAddTaskException(final String message) {
super(message);
}
}
32 changes: 29 additions & 3 deletions src/ru/alexgur/kanban/model/Epic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import ru.alexgur.kanban.service.TaskType;

import java.time.LocalDateTime;
import java.util.ArrayList;

public class Epic extends Task {
private List<Integer> subTasksIds = new ArrayList<>();
private LocalDateTime endTime; // дата и время завершения всех задач

public Epic() {
super();
Expand All @@ -21,6 +23,14 @@ public List<Integer> getSubTasksIds() {
return subTasksIds;
}

public LocalDateTime getEndTime() {
return endTime;
}

public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}

public void setSubTasksIds(List<Integer> subTasksIds) {
List<Integer> subTasksIdsFiltered = new ArrayList<>();
for (Integer id : subTasksIds) {
Expand All @@ -46,8 +56,24 @@ public TaskType getType() {

@Override
public String toString() {
return "Task [id=" + id + ", name=" + getName() + ", text=" + getText() + ", status=" + getStatus()
+ ", subTasksIds=" + subTasksIds + "]";
}
String start = "";
if (getStartTime() != null) {
start = getStartTime().format(dateTimeFormatter);
}

long durationStr = 0;
if (getDuration().isZero()) {
durationStr = getDuration().toMinutes();
}

return "Task [id=" + id +
", type=" + getType() +
", name=" + getName() +
", text=" + getText() +
", status=" + getStatus() +
", startTime=" + start +
", duration=" + durationStr +
", subTasksIds=" + subTasksIds +
"]";
}
}
21 changes: 19 additions & 2 deletions src/ru/alexgur/kanban/model/SubTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ public TaskType getType() {

@Override
public String toString() {
return "Task [id=" + id + ", name=" + getName() + ", text=" + getText() + ", status=" + getStatus()
+ ", epycId=" + epicId + "]";
String start = "";
if (getStartTime() != null) {
start = getStartTime().format(dateTimeFormatter);
}

long durationStr = 0;
if (getDuration().isZero()) {
durationStr = getDuration().toMinutes();
}

return "Task [id=" + id +
", type=" + getType() +
", name=" + getName() +
", text=" + getText() +
", status=" + getStatus() +
", startTime=" + start +
", duration=" + durationStr +
", epycId=" + epicId +
"]";
}
}
55 changes: 54 additions & 1 deletion src/ru/alexgur/kanban/model/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ru.alexgur.kanban.model;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import ru.alexgur.kanban.service.Status;
import ru.alexgur.kanban.service.TaskType;

Expand All @@ -9,6 +13,34 @@ public class Task {
private String name; // название задачи
private String text; // текст задачи
private Status status; // статус задачи
private Duration duration = Duration.ZERO; // продолжительность задачи в минутах
private LocalDateTime startTime; // дата и время старта выполнения задачи
public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public LocalDateTime getStartTime() {
return startTime;
}

public Task setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
return this;
}

public Duration getDuration() {
return duration;
}

public LocalDateTime getEndTime() {
if (getStartTime() == null) {
return null;
}
return startTime.plus(duration);
}

public Task setDuration(Duration duration) {
this.duration = duration;
return this;
}

public Task() {
this.status = Status.NEW;
Expand Down Expand Up @@ -53,6 +85,10 @@ public String getText() {
return text;
}

public static <T extends Task> int compareToStartTimeAsc(T a, T b) {
return a.getStartTime().isBefore(b.getStartTime()) ? -1 : 1;
}

public Task setText(String text) {
this.text = text;
return this;
Expand Down Expand Up @@ -98,6 +134,23 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "Task [id=" + id + ", name=" + getName() + ", text=" + getText() + ", status=" + getStatus() + "]";
String start = "";
if (getStartTime() != null) {
start = getStartTime().format(dateTimeFormatter);
}

long durationStr = 0;
if (getDuration().isZero()) {
durationStr = getDuration().toMinutes();
}

return "Task [id=" + id +
", type=" + getType() +
", name=" + getName() +
", text=" + getText() +
", status=" + getStatus() +
", startTime=" + start +
", duration=" + durationStr +
"]";
}
}
49 changes: 40 additions & 9 deletions src/ru/alexgur/kanban/service/FileBackedTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class FileBackedTaskManager extends InMemoryTaskManager {
private File fileToSave;
private DateTimeFormatter dateTimeFormatter = Task.dateTimeFormatter;
private String eol = "\n";
private String csvSplitter = ";";

Expand Down Expand Up @@ -142,6 +146,7 @@ public static FileBackedTaskManager loadFromFile(File file) {
}

taskManager.updateEpicsSubTasksIds();
taskManager.updateEpicsDurationStartEndTime();
taskManager.setFile(file);
return taskManager;
}
Expand All @@ -152,7 +157,7 @@ private void save() {
}

try (FileWriter fileWriter = new FileWriter(fileToSave)) {
String csvHeader = "id,type,name,status,description,epic";
String csvHeader = "id,type,name,description,status,start,duration,epic";
fileWriter.write(csvHeader + eol);

for (List<? extends Task> target : List.of(getTasks(), getSubTasks(), getEpics())) {
Expand All @@ -170,15 +175,30 @@ private <T extends Task> String toString(T task) {
if (task instanceof SubTask) {
epicId = String.valueOf(((SubTask) task).getEpicId());
}

String start = "";
if (task.getStartTime() != null) {
start = task.getStartTime().format(dateTimeFormatter);
}

long duration = 0;
if (task.getDuration().isZero()) {
duration = task.getDuration().toMinutes();
}

return task.id
+ csvSplitter
+ task.getType()
+ csvSplitter
+ task.getName()
+ csvSplitter
+ task.getText()
+ csvSplitter
+ task.getStatus()
+ csvSplitter
+ task.getText()
+ start
+ csvSplitter
+ duration
+ csvSplitter
+ epicId
+ csvSplitter
Expand All @@ -190,25 +210,36 @@ private Object fromString(String line) {

int id = Integer.valueOf(args[0]);
TaskType type = TaskType.valueOf(args[1]);
Status status = Status.valueOf(args[3]);
String name = args[2];
String text = args[4];
String text = args[3];
Status status = Status.valueOf(args[4]);
LocalDateTime startTime = LocalDateTime.parse(args[5], dateTimeFormatter);
int durationMinutes = Integer.valueOf(args[6]);

switch (type) {
case TASK:
Task task = new Task(id);
task.setName(name).setText(text).setStatus(status);
task.setName(name)
.setText(text)
.setStatus(status)
.setStartTime(startTime)
.setDuration(Duration.ofMinutes(durationMinutes));
return task;
case SUBTASK:
int epicId = Integer.valueOf(args[5]);
int epicId = Integer.valueOf(args[7]);

SubTask subTask = new SubTask(id);
subTask.setName(name).setText(text).setStatus(status);
subTask.setEpicId(epicId);
subTask.setEpicId(epicId)
.setName(name)
.setText(text)
.setStatus(status)
.setStartTime(startTime)
.setDuration(Duration.ofMinutes(durationMinutes));
return subTask;
case EPIC:
Epic epic = new Epic(id);
epic.setName(name).setText(text);
epic.setName(name)
.setText(text);
return epic;
}
return null;
Expand Down
6 changes: 2 additions & 4 deletions src/ru/alexgur/kanban/service/InMemoryHistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public void add(Task task) {

@Override
public List<Task> getHistory() {
List<Task> listTasks = new ArrayList<>();
for (Node node : getTasks()) {
listTasks.add(node.getTask());
}
List<Task> listTasks = getTasks().stream()
.map(x -> x.getTask()).toList();
return listTasks;
}

Expand Down
Loading
Loading