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

Sprint 7 solution in file manager #2

Merged
merged 15 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# java-kanban
Repository for homework project.

2 changes: 2 additions & 0 deletions resources/tasksBackup.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,type,name,status,description,epic
history
10 changes: 10 additions & 0 deletions resources/test/tasksData.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id,type,name,status,description,epic
1,TASK,task,NEW,t description,
6,TASK,task2,NEW,t description,
7,TASK,task3,NEW,t description,
2,EPIC,epic,IN_PROGRESS,e description,
4,EPIC,epic2,NEW,e description,
3,SUBTASK,sbtask,NEW,st description,2
5,SUBTASK,sbtask2,DONE,st description,2
history
2,EPIC,epic,IN_PROGRESS,e description,
Empty file added resources/test/useCaseFile.csv
Empty file.
111 changes: 98 additions & 13 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import ru.yandex.practicum.tasktracker.model.Epic;
import ru.yandex.practicum.tasktracker.model.Subtask;
import ru.yandex.practicum.tasktracker.model.Task;
import ru.yandex.practicum.tasktracker.model.TaskStatus;
import ru.yandex.practicum.tasktracker.service.FileBackedTaskManager;
import ru.yandex.practicum.tasktracker.service.InMemoryTaskManager;
import ru.yandex.practicum.tasktracker.service.TaskManager;

/**
* Use Case 1. Create: Two tasks; Epic with three subtasks; Epic without subtask. 2. Request (GET)
* the created tasks multiple times in different orders. 3. Display the history view and ensure
* that there are no duplicates. 4. Delete: a task that exists in the history; Verify: it does not
* appear when printing history of viewing. 5. Delete: epic with three subtasks; Verify: both the
* epic itself and all its subtasks are removed from the history.
*/

public class Main {

private static TaskManager tm;
Expand All @@ -23,20 +23,103 @@ public class Main {
private static Epic epic2;

public static void main(String[] args) {
tm = new InMemoryTaskManager();

createTasksInTaskManager();
int choice = 2;

getTasksInDifferentOrder();
switch (choice) {
case 1 -> useCase1(); /* History Saving */
case 2 -> useCaseFile(); /* File saving / restoring */
}

printWholeHistory();

deleteTaskAndCheckHistory();
}

/**
* <h2>Use Case 2 - {@link #useCaseFile()}</h2>
* <ol>
* <li> GIVEN: A {@code FileBackedTaskManager oldManager} that has 3 Tasks, 2 Epics, and 2 Subtasks for one of the Epic.</li>
* <li> WHEN: A new {@code FileBackedTaskManager newManager} was created from the file of the oldManager.</li>
* <li> THEN: This new manager has all same Tasks, Epics , and Subtasks in it. </li>
* </ol>
*/
static void useCaseFile() {
/* GIVEN */
/* Create tasks data: */
Task t = new Task();
t.setTitle("task");
t.setDescription("t description");

Epic e = new Epic();
e.setTitle("epic");
e.setDescription("e description");

Subtask sb = new Subtask();
sb.setTitle("sbtask");
sb.setDescription("st description");

Path path = Path.of("resources/test/useCaseFile.csv");
File temp = path.toFile();
TaskManager oldManager = FileBackedTaskManager.loadFromFile(temp);
oldManager.addTask(t); // add task
oldManager.addEpic(e); // add epic
sb.setEpicId(2);
oldManager.addSubtask(sb); // add subtask to epic
e.setTitle("epic2");
oldManager.addEpic(e); // add epic2
sb.setTitle("sbtask2");
oldManager.addSubtask(sb); // add subtask2 to epic
t.setTitle("task2");
oldManager.addTask(t); // add task2
t.setTitle("task3");
oldManager.addTask(t); // add task3
sb.setStatus(TaskStatus.DONE);
oldManager.updateSubtask(sb); // update subtask2 of epic
oldManager.getEpicById(2);

/* WHEN */
TaskManager newManage = FileBackedTaskManager.loadFromFile(temp);
List<Integer> taskIds = new ArrayList<>();
newManage.getAllTasks().forEach(a -> taskIds.add(a.getId()));
List<Integer> epicIds = new ArrayList<>();
newManage.getAllEpics().forEach(a -> epicIds.add(a.getId()));
List<Integer> subtaskIds = new ArrayList<>();
newManage.getAllSubtasks().forEach(a -> subtaskIds.add(a.getId()));

/* THEN */
System.out.println(
"newManage.getAllTasks().size() should be 3: " + newManage.getAllTasks().size());
System.out.println("Tasks ids should be 1,6,7 : " + taskIds);
System.out.println(
"newManage.getAllEpics().size() should be 2: " + newManage.getAllEpics().size());
System.out.println("Epics ids should be 2, 4 : " + epicIds);
System.out.println(
"newManage.getAllSubtasks().size() should be 2: " + newManage.getAllSubtasks().size());
System.out.println("Subtasks ids should 3, 5 : " + subtaskIds);
System.out.println("newManage.getHistory() should have epic id=2: " + newManage.getHistory());

deleteEpicWithSubtaskAndCheckHistory();

}

/**
* <h2>Use Case 1 - {@link #useCase1()}</h2>
* <ol>
* <li>Create: Two tasks; Epic with three subtasks; Epic without subtask.</li>
* <li>Request ({@code .get...()} methods)the created tasks multiple times in different order.</li>
* <li>Display the history view and ensure that there are no duplicates.</li>
* <li>Delete: a task that exists in the history; Verify: it does not appear when printing history of viewing.</li>
* <li>Delete: epic with three subtasks; Verify: both the epic itself and all its subtasks are removed from the history.</li>
* </ol>
* <p>
*/
static void useCase1() {
tm = new InMemoryTaskManager();
createTasksInTaskManager();
getTasksInDifferentOrder();
printWholeHistory();
deleteTaskAndCheckHistory();
deleteEpicWithSubtaskAndCheckHistory();
}

private static void deleteEpicWithSubtaskAndCheckHistory() {
System.out.println();
System.out.println("Deleting Epic1: ...");
Expand Down Expand Up @@ -68,6 +151,8 @@ private static void printWholeHistory() {
System.out.println("Saved history:");
tm.getHistory().forEach(t -> System.out.print(t.getTitle() + " - "));
System.out.println();
System.out.println(tm.getHistory());
System.out.println();
}

private static void getTasksInDifferentOrder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.yandex.practicum.tasktracker.exception;

public class ManagerLoadException extends RuntimeException {

public ManagerLoadException() {
}

public ManagerLoadException(String message) {
super(message);
}

public ManagerLoadException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.yandex.practicum.tasktracker.exception;

public class ManagerSaveException extends RuntimeException {

public ManagerSaveException() {
}

public ManagerSaveException(String message) {
super(message);
}

public ManagerSaveException(String message, Throwable cause) {
super(message, cause);
}
}
17 changes: 15 additions & 2 deletions src/ru/yandex/practicum/tasktracker/model/Epic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class Epic extends Task {

private final Set<Subtask> subtasks = new HashSet<>();
private Set<Subtask> subtasks = new HashSet<>();

@Override
public TaskStatus getStatus() {
Expand Down Expand Up @@ -76,4 +76,17 @@ public String toString() {
", subtasks.size=" + subtasks.size() +
"}";
}
}

@Override
natalaly marked this conversation as resolved.
Show resolved Hide resolved
public Epic clone() {
Epic epic = new Epic();
epic.setId(this.getId());
epic.setTitle(this.getTitle());
epic.setDescription(this.getDescription());
epic.setStatus(TaskStatus.valueOf(this.getStatus().name()));
Set<Subtask> clonedSubtasks = new HashSet<>();
this.subtasks.forEach(s -> clonedSubtasks.add(s.clone()));
epic.subtasks = clonedSubtasks;
return epic;
}
}
11 changes: 11 additions & 0 deletions src/ru/yandex/practicum/tasktracker/model/Subtask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public String toString() {
'}';
return result;
}

@Override
public Subtask clone() {
Subtask subtask = new Subtask();
subtask.setId(this.getId());
subtask.setTitle(this.getTitle());
subtask.setDescription(this.getDescription());
subtask.setStatus(TaskStatus.valueOf(this.getStatus().name()));
subtask.setEpicId(this.epicId);
return subtask;
}
}
16 changes: 14 additions & 2 deletions src/ru/yandex/practicum/tasktracker/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public class Task {
public class Task implements Cloneable {
natalaly marked this conversation as resolved.
Show resolved Hide resolved

private int id;
private String title;
Expand Down Expand Up @@ -63,11 +63,23 @@ public int hashCode() {

@Override
public String toString() {
return "Task{" +
return this.getClass().getSimpleName() +
"{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", status=" + getStatus() +
'}';
}

public Task clone() {
Task task = new Task();
task.setId(this.id);
task.setTitle(this.title);
task.setDescription(this.description);
task.setStatus(TaskStatus.valueOf(this.status.name()));
return task;
}


}
21 changes: 21 additions & 0 deletions src/ru/yandex/practicum/tasktracker/model/TaskType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.yandex.practicum.tasktracker.model;

public enum TaskType {
TASK,
EPIC,
SUBTASK;

public static TaskType getType(Task task) {
natalaly marked this conversation as resolved.
Show resolved Hide resolved
try {
return switch (task.getClass().getSimpleName()) {
case "Task" -> TASK;
case "Epic" -> EPIC;
case "Subtask" -> SUBTASK;
default -> null;
};
} catch (NullPointerException e) {
natalaly marked this conversation as resolved.
Show resolved Hide resolved
e.printStackTrace();
}
return null;
}
}
Loading
Loading