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
1 change: 1 addition & 0 deletions .idea/dictionaries/project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/ru/yandex/javacourse/model/Epic.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package ru.yandex.javacourse.model;

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

public class Epic extends Task {
private ArrayList<Integer> subtasksIdList = new ArrayList<>();
private LocalDateTime endTime;

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

@Override
public LocalDateTime getEndTime() {
return endTime;
}

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

public ArrayList<Integer> getSubtasksIdList() {
return subtasksIdList;
}
Expand Down Expand Up @@ -38,6 +49,8 @@ public Epic getCopy() {
epicCopy.setId(this.getId());
epicCopy.setStatus(this.getStatus());
epicCopy.setSubtasksIdList(new ArrayList<>(this.getSubtasksIdList()));
epicCopy.setDuration(this.getDuration());
epicCopy.setStartTime(this.getStartTime());
return epicCopy;
}

Expand All @@ -49,6 +62,8 @@ public String toString() {
", description='" + getDescription() + '\'' +
", id=" + getId() +
", status=" + getStatus() + '\'' +
", startTime=" + getStartTime() +
", duration =" + getDuration() +
", subtasksIdList=" + subtasksIdList +
'}';
}
Expand Down
19 changes: 18 additions & 1 deletion src/ru/yandex/javacourse/model/Subtask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.yandex.javacourse.model;

import java.time.Duration;
import java.time.LocalDateTime;

public class Subtask extends Task {
private final int epicId;

Expand All @@ -13,13 +16,25 @@ public Subtask(String name, String description, TaskStatus status, int epicId) {
this.epicId = epicId;
}

public Subtask(String name, String description, TaskStatus status, int epicId, Duration duration,
LocalDateTime startTime) {
super(name, description, status, duration, startTime);
this.epicId = epicId;
}

public Subtask(String name, String description, int epicId, Duration duration, LocalDateTime startTime) {
super(name, description, duration, startTime);
this.epicId = epicId;
}

public int getEpicId() {
return epicId;
}

@Override
public Subtask getCopy() {
Subtask copySubtask = new Subtask(this.getName(), this.getDescription(), this.getStatus(), this.getEpicId());
Subtask copySubtask = new Subtask(this.getName(), this.getDescription(), this.getStatus(), this.getEpicId(),
this.getDuration(), this.getStartTime());
copySubtask.setId(this.getId());
return copySubtask;
}
Expand All @@ -32,6 +47,8 @@ public String toString() {
", id=" + getId() +
", status=" + getStatus() + '\'' +
", epicId=" + epicId +
", startTime=" + getStartTime() +
", duration =" + getDuration() +
'}';
}
}
47 changes: 46 additions & 1 deletion src/ru/yandex/javacourse/model/Task.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package ru.yandex.javacourse.model;

import java.time.Duration;
import java.time.LocalDateTime;

public class Task {
private String name;
private String description;
private int id;
private TaskStatus status;
private Duration duration;
private LocalDateTime startTime;


public Task(String name, String description) {
Expand All @@ -19,6 +24,21 @@ public Task(String name, String description, TaskStatus status) {
this.status = status;
}

public Task(String name, String description, Duration duration, LocalDateTime startTime) {
this.name = name;
this.description = description;
this.duration = duration;
this.startTime = startTime;
}

public Task(String name, String description, TaskStatus status, Duration duration, LocalDateTime startTime) {
this.name = name;
this.description = description;
this.status = status;
this.duration = duration;
this.startTime = startTime;
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -51,8 +71,31 @@ public void setStatus(TaskStatus status) {
this.status = status;
}

public Duration getDuration() {
return duration;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут корректно, плюсую


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

public LocalDateTime getStartTime() {
return startTime;
}

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

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

public Task getCopy() {
Task copyTask = new Task(this.name, this.description, this.status);
Task copyTask = new Task(this.name, this.description, this.status, this.duration, this.startTime);
copyTask.setId(this.id);
return copyTask;
}
Expand All @@ -78,6 +121,8 @@ public String toString() {
", description='" + description + '\'' +
", id=" + id +
", status=" + status +
", startTime=" + startTime +
", duration =" + duration +
'}';
}
}
62 changes: 51 additions & 11 deletions src/ru/yandex/javacourse/service/FileBackedTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import ru.yandex.javacourse.model.*;

import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Optional;
import java.util.TreeSet;

public class FileBackedTaskManager extends InMemoryTaskManager {
private final File file;
Expand All @@ -14,19 +19,30 @@ public static void main(String[] args) throws IOException {
System.out.println("Файл создан: " + file.getAbsolutePath());
FileBackedTaskManager fileBackedTaskManager = Managers.getDefaultFileBacked(file);

Task task = fileBackedTaskManager.addTask(new Task("Задача", "Описание задачи"));
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextHour = now.plusHours(1);
LocalDateTime nextDay = now.plusDays(1);
Duration duration30 = Duration.ofMinutes(30);
Duration duration40 = Duration.ofMinutes(40);

Task task = fileBackedTaskManager.addTask(new Task("Задача", "Описание задачи", TaskStatus.NEW,
duration30, now));
Epic epic = fileBackedTaskManager.addEpic(new Epic("Эпик", "Описание эпика"));
Subtask subtask = fileBackedTaskManager.addSubtask(new Subtask("Подзадача",
"Описание подзадачи", epic.getId()));
"Описание подзадачи", TaskStatus.NEW, epic.getId(), duration30, nextHour));

FileBackedTaskManager newFileBackedTaskManager = loadFromFile(file);
Task newTask = newFileBackedTaskManager.addTask(new Task("Новая задача",
"Описание новой задачи"));
"Описание новой задачи", TaskStatus.IN_PROGRESS, duration30, nextDay));

Subtask updatedSubtask = new Subtask("Подзадача", "Описание подзадачи", epic.getId());
Subtask updatedSubtask = new Subtask("Подзадача", "Описание подзадачи", epic.getId(), duration40,
now);
updatedSubtask.setId(subtask.getId());
updatedSubtask.setStatus(TaskStatus.DONE);
newFileBackedTaskManager.updateSubtask(updatedSubtask);

TreeSet<Task> prTasks = newFileBackedTaskManager.getPrioritizedTasks();
prTasks.forEach(System.out::println);
}

public FileBackedTaskManager(File file) {
Expand All @@ -39,7 +55,7 @@ public FileBackedTaskManager(File file) {
*/
public void save() throws ManagerSaveException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write("id,type,name,status,description,epic");
bw.write("id,type,name,status,description,epic,startTime,duration(min)");
bw.newLine();

ArrayList<Task> tasks = getAllTasks();
Expand Down Expand Up @@ -216,7 +232,7 @@ public String toString(Task task) {
}

TaskType taskType;
String epicId = "";
String epicId = "без эпика";
if (task instanceof Subtask subtask) {
taskType = TaskType.SUBTASK;
epicId = Integer.toString(subtask.getEpicId());
Expand All @@ -225,8 +241,23 @@ public String toString(Task task) {
} else {
taskType = TaskType.TASK;
}
return String.format("%s,%s,%s,%s,%s,%s",
task.getId(), taskType, task.getName(), task.getStatus(), task.getDescription(), epicId);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String taskStartTimeString = "";
Optional<LocalDateTime> taskStartTime = Optional.ofNullable(task.getStartTime());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

используешь Optional это лайк

if (taskStartTime.isPresent()) {
taskStartTimeString = taskStartTime.get().format(formatter);
}

String taskDurationString = "";
Optional<Duration> taskDuration = Optional.ofNullable(task.getDuration());
if (taskDuration.isPresent()) {
taskDurationString = String.valueOf(taskDuration.get().toMinutes());
}

return String.format("%s,%s,%s,%s,%s,%s,%s,%s",
task.getId(), taskType, task.getName(), task.getStatus(), task.getDescription(), epicId,
taskStartTimeString, taskDurationString);
}

/**
Expand All @@ -239,20 +270,29 @@ public Task fromString(String value) {
String taskName = splitValues[2];
TaskStatus taskStatus = TaskStatus.valueOf(splitValues[3]);
String taskDescription = splitValues[4];
LocalDateTime taskStartTime = null;
Duration taskDuration = null;

if (splitValues.length > 6 && !splitValues[6].isEmpty()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

магические числа лучше выносить в отдельные константы

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
taskStartTime = LocalDateTime.parse(splitValues[6], formatter);
}
if (splitValues.length > 7 && !splitValues[7].isEmpty()) {
taskDuration = Duration.ofMinutes(Long.parseLong(splitValues[7]));
}

if (taskType.equals(TaskType.SUBTASK)) {
int epicId = Integer.parseInt(splitValues[5]);
Subtask subTask = new Subtask(taskName, taskDescription, taskStatus, epicId);
Subtask subTask = new Subtask(taskName, taskDescription, taskStatus, epicId, taskDuration, taskStartTime);
subTask.setId(taskId);
return subTask;
} else if (taskType.equals(TaskType.EPIC)) {
Epic epic = new Epic(taskName, taskDescription);
epic.setId(taskId);
return epic;
} else {
Task task = new Task(taskName, taskDescription);
Task task = new Task(taskName, taskDescription, taskStatus, taskDuration, taskStartTime);
task.setId(taskId);
task.setStatus(taskStatus);
return task;
}
}
Expand Down
Loading