diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 000000000..02d2f1e9e --- /dev/null +++ b/domain-model.md @@ -0,0 +1,62 @@ +## Todo + +| Method | Member Variable | Scenario | Result | +|-----------------------------------------------|--------------------------------|-------------------------------------------------------------------------------|------------------------------------| +| add(String name) | HashMap tasks | Task with the provided name is not already in the todolist | true | +| | | Task with the provided name is already in the todolist | false | +| | | | | +| listTasks( ) | | There are tasks in the todolist | string with all tasks | +| | | There are no tasks in the todolist | string with error message | +| | | | | +| updateTaskStatus(String name, Boolean status) | | There is a task with the provided name and has the requested status | true | +| | | There is no task with the provided name or does not have the requested status | false | +| | | | | +| getCompletedTasks() | | There are completed tasks in the todolist | string with all completed tasks | +| | | There are no completed tasks in the todolist | string with error message | +| | | | | +| getUncompletedTasks() | | There are uncompleted tasks in the todolist | string with all uncompleted tasks | +| | | There are no uncompleted tasks in the todolist | string with error message | +| | | | | +| SearchTask(String) | | There was a task with the provided name | string with the task | +| | | There was no task with the provided name | string with error message | +| | | | | +| removeTask(String) | | Task with the provided name is not in the todolist | true | +| | | Task with the provided name is in the todolist | false | +| | | | | +| taskDescending() | | There are tasks in the todolist | string in descending order by name | +| | | There are no tasks in the todolist | string with error message | +| | | | | +| taskAscending() | | There are tasks in the todolist | string in ascending order by name | +| | | There are no tasks in the todolist | string with error message | + + + +# Extension + +## Todo + +| Method | Member Variable | Scenario | Result | +|----------------------------------------------|-----------------------|----------|--------| +| add(String name) | ArrayList tasks | | | +| | id | | | +| listTasks( ) | | | | +| updateTaskStatus(Task task, Boolean state) | | | | +| | | | | +| getCompletedTasks() | | | | +| getNotCompletedTasks() | | | | +| SearchTask(Task task) | | | | +| removeTask(Task task) | | | | +| taskDescending() | | | | +| taskAscending() | | | | +| getTaskById(int id) | | | | +| updateTaskName(int id, String newName) | | | | +| updateTaskStatusById(int id, Boolean status) | | | | +| | | | | + +## Task + +| Method | Member Variable | Scenario | Result | +|--------|------------------|----------|--------| +| | | | | +| | Boolean complete | | | +| | String name | | | \ No newline at end of file diff --git a/src/main/java/com/booleanuk/core/TodoList.java b/src/main/java/com/booleanuk/core/TodoList.java index 675adaf0b..8232be440 100644 --- a/src/main/java/com/booleanuk/core/TodoList.java +++ b/src/main/java/com/booleanuk/core/TodoList.java @@ -1,5 +1,143 @@ package com.booleanuk.core; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + public class TodoList { + HashMap tasks; + + public TodoList(HashMap tasks){ + this.tasks = tasks; + } + + + // Task 1 + public boolean add(String taskName){ + if(!this.tasks.containsKey(taskName)){ + this.tasks.put(taskName, false); + return true; + } + return false; + } + + + // Task 2 + public String listTasks(){ + if(this.tasks.isEmpty()){ + return "The todolist does not contain any tasks!"; + } + String listOfTasks = ""; + for(String task : this.tasks.keySet()){ + listOfTasks += task + ", "; + } + return listOfTasks; + } + + + // Task 3 + public boolean updateTaskStatus(String task, Boolean status){ + if(this.tasks.containsKey(task)){ + this.tasks.put(task, status); + return true; + } + return false; + } + + + // Task 4 + public String getCompletedTasks(){ + String completedTasks = ""; + for(String task : this.tasks.keySet()){ + if(this.tasks.get(task).equals(true)){ + completedTasks += task + ", "; + } + } + if(!completedTasks.equals("")){ + return completedTasks; + } + return "There are no completed tasks!"; + } + + + // Task 5 + public String getUncompletedTasks(){ + String uncompletedTasks = ""; + for(String task : this.tasks.keySet()){ + if(this.tasks.get(task).equals(false)){ + uncompletedTasks += task + ", "; + } + } + if(!uncompletedTasks.equals("")){ + return uncompletedTasks; + } + return "There are no uncompleted tasks!"; + } + + + // Task 6 + public String SearchTask(String task){ + if(this.tasks.containsKey(task)){ + return "The task exist!"; + } + return "The task does not exist!"; + } + + + // Task 7 + public Boolean removeTask(String task){ + if(this.tasks.containsKey(task)){ + this.tasks.remove(task); + return true; + } + return false; + } + + + // Task 8 + public String taskDescending(){ + if(this.tasks.isEmpty()){ + return "The todolist does not contain any tasks!"; + } + else{ + ArrayList sortedKeys = new ArrayList<>(this.tasks.keySet()); + Collections.sort(sortedKeys); + Collections.reverse(sortedKeys); + String tasksDescending = ""; + + for(String task : sortedKeys){ + if(!task.equals(sortedKeys.getLast())){ + tasksDescending += task + ", "; + } + else{ + tasksDescending += task; + } + } + return tasksDescending; + } + } + + + // Task 9 + public String taskAscending(){ + if(this.tasks.isEmpty()){ + return "The todolist does not contain any tasks!"; + } + else{ + ArrayList sortedKeys = new ArrayList<>(this.tasks.keySet()); + Collections.sort(sortedKeys); + String tasksAscending = ""; + + for(String task : sortedKeys){ + if(!task.equals(sortedKeys.getLast())){ + tasksAscending += task + ", "; + } + else{ + tasksAscending += task; + } + } + return tasksAscending; + } + } } diff --git a/src/main/java/com/booleanuk/extension/TodoItem.java b/src/main/java/com/booleanuk/extension/TodoItem.java new file mode 100644 index 000000000..47f31d885 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/TodoItem.java @@ -0,0 +1,42 @@ +package com.booleanuk.extension; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +public class TodoItem { + private String name; + private Boolean completed; + private final Integer todoID; + private LocalDateTime dateTime; + + public TodoItem(Integer todoID, String name, Boolean completed, LocalDateTime dateTime){ + this.todoID = todoID; + this.name = name; + this.completed = false; + this.dateTime = dateTime; + } + + public LocalDateTime getDateTime() { + return dateTime; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getCompleted() { + return completed; + } + + public void setCompleted(Boolean completed) { + this.completed = completed; + } + + public Integer getTodoID() { + return todoID; + } +} diff --git a/src/main/java/com/booleanuk/extension/TodoListExtension.java b/src/main/java/com/booleanuk/extension/TodoListExtension.java new file mode 100644 index 000000000..5478fbb45 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/TodoListExtension.java @@ -0,0 +1,60 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.HashMap; + +public class TodoListExtension { + private ArrayList tasks; + private static long idCounter = 0; + + public TodoListExtension(ArrayList tasks){ + this.tasks = tasks; + } + + public String getTaskByID(Integer id){ + for(TodoItem item : tasks){ + if(item.getTodoID().equals(id)){ + if(item.getCompleted()){ + return item.getName() + ", completed"; + } else { + return item.getName() + ", not completed"; + } + } + } + return "ID is not in todo list"; + } + + public String updateNameByID(Integer id, String newName){ + for(TodoItem item : tasks){ + if(item.getTodoID().equals(id)){ + item.setName(newName); + return "Task with ID " + id + " has a new name: " + newName; + } + } + return "ID is not in todo list"; + } + + public String updateStatusByID(Integer id, Boolean status){ + for(TodoItem item : tasks){ + if(item.getTodoID().equals(id)){ + if(status){ + return "Task with ID " + id + " has a new status: completed"; + }else { + return "Task with ID " + id + " has a new status: incomplete"; + } + } + } + return "ID is not in todo list"; + } + + public String showDateTimeForEachTask(){ + String tasks = ""; + if(!this.tasks.isEmpty()){ + for(TodoItem item : this.tasks){ + tasks += "Task with id: " + item.getTodoID() + ", " + item.getName() + ", was created: " + item.getDateTime().toString() + "\n"; + } + return tasks; + } + return "There are no tasks in the list!"; + } +} diff --git a/src/test/java/com/booleanuk/core/TodoListTest.java b/src/test/java/com/booleanuk/core/TodoListTest.java index 0bef779a4..2053fee9a 100644 --- a/src/test/java/com/booleanuk/core/TodoListTest.java +++ b/src/test/java/com/booleanuk/core/TodoListTest.java @@ -3,11 +3,173 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + class TodoListTest { + private HashMap items = new HashMap<>(){{ + put("Clean room", false); + put("Buy groceries", false); + put("Write CV", true); + }}; + @Test public void exampleTest() { String hello = "Hello"; Assertions.assertEquals("Hello", hello); Assertions.assertNotEquals("Goodbye", hello); } + + //Task 1 + @Test + public void addTaskThatDoesNotExist(){ + TodoList todoList = new TodoList(items); + String taskName = "Go do exercise"; + + Assertions.assertTrue(todoList.add(taskName)); + } + + @Test + public void addTaskThatDoesExist(){ + TodoList todoList = new TodoList(items); + String taskName = "Clean room"; + + Assertions.assertFalse(todoList.add(taskName)); + } + + + + //Task 2 + @Test + public void tasksExistInTodoList(){ + TodoList todoList = new TodoList(items); + + Assertions.assertFalse(todoList.listTasks().isEmpty()); + } + + @Test + public void tasksDoesNotExistInTodoList(){ + HashMap emptyItemSet = new HashMap<>(); + TodoList todoList = new TodoList(emptyItemSet); + + Assertions.assertEquals("The todolist does not contain any tasks!", todoList.listTasks()); + } + + + + //Task 3 + @Test + public void doesStatusUpdateIfTaskExist(){ + TodoList todoList = new TodoList(items); + String task = "Clean room"; + + Assertions.assertTrue(todoList.updateTaskStatus(task, true)); + } + + @Test + public void doesStatusUpdateIfTaskDoesNotExist(){ + TodoList todoList = new TodoList(items); + String task = "Go do exercise"; + + Assertions.assertFalse(todoList.updateTaskStatus(task, true)); + } + + + // Task 4 + @Test + public void doesExistCompletedTasks(){ + TodoList todoList = new TodoList(items); + + if(!todoList.getCompletedTasks().isEmpty()){ + Assertions.assertFalse(todoList.getCompletedTasks().isEmpty()); + } + } + + @Test + public void doesNotExistCompletedTasks(){ + TodoList todoList = new TodoList(items); + + if(todoList.getCompletedTasks().isEmpty()){ + Assertions.assertEquals("There are no completed tasks!", todoList.getCompletedTasks()); + } + } + + + // Task 5 + @Test + public void UncompletedTasksExist(){ + TodoList todoList = new TodoList(items); + + if(!todoList.getUncompletedTasks().isEmpty()){ + Assertions.assertFalse(todoList.getUncompletedTasks().isEmpty()); + } + } + + @Test + public void doesNotExistUncompletedTasks(){ + TodoList todoList = new TodoList(items); + + if(todoList.getUncompletedTasks().isEmpty()){ + Assertions.assertEquals("There are no uncompleted tasks!", todoList.getUncompletedTasks()); + } + } + + + // Task 6 + @Test + public void taskDoesExist(){ + TodoList todoList = new TodoList(items); + String task = "Buy groceries"; + + Assertions.assertEquals("The task exist!", todoList.SearchTask(task)); + } + + @Test + public void taskDoesNotExist(){ + TodoList todoList = new TodoList(items); + String task = "Play the piano"; + + Assertions.assertEquals("The task does not exist!", todoList.SearchTask(task)); + } + + + // Task 7 + @Test + public void taskIsRemoved(){ + TodoList todoList = new TodoList(items); + String task = "Clean room"; + + Assertions.assertTrue(todoList.removeTask(task)); + } + + @Test + public void taskIsNotRemoved(){ + TodoList todoList = new TodoList(items); + String task = "Play the piano"; + + Assertions.assertFalse(todoList.removeTask(task)); + } + + + // Test 8 + @Test + public void tasksAreDescending(){ + TodoList todoList = new TodoList(items); + + Assertions.assertEquals(todoList.taskDescending(), "Write CV, Clean room, Buy groceries"); + } + + + // Test 9 + @Test + public void tasksAreAscending(){ + TodoList todoList = new TodoList(items); + + Assertions.assertEquals(todoList.taskAscending(), "Buy groceries, Clean room, Write CV"); + } + + + } diff --git a/src/test/java/com/booleanuk/extension/TodoListExtensionTest.java b/src/test/java/com/booleanuk/extension/TodoListExtensionTest.java new file mode 100644 index 000000000..ae28c5e0d --- /dev/null +++ b/src/test/java/com/booleanuk/extension/TodoListExtensionTest.java @@ -0,0 +1,97 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashMap; + +public class TodoListExtensionTest { + private ArrayList items = new ArrayList<>(){{ + LocalDateTime dateTime1 = LocalDateTime.of(2024, 10, 25, 8, 50, 11); + TodoItem todoItem1 = new TodoItem(1, "Clean room", true, dateTime1); + add(todoItem1); + + LocalDateTime dateTime2 = LocalDateTime.of(2024, 10, 26, 10, 22, 18); + TodoItem todoItem2 = new TodoItem(2, "Buy groceries", false, dateTime2); + add(todoItem2); + + LocalDateTime dateTime3 = LocalDateTime.of(2024, 10, 30, 17, 30, 44); + TodoItem todoItem3 = new TodoItem(3, "Write CV", false, dateTime3); + add(todoItem3); + }}; + + // EXTENSION 1 + @Test + public void enteringNonExistingID(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 4; + + Assertions.assertEquals("ID is not in todo list", todoList.getTaskByID(nonExistingID)); + } + + @Test + public void enteringExistingID(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 3; + + Assertions.assertEquals("Write CV, not completed", todoList.getTaskByID(nonExistingID)); + } + + // EXTENSION 2 + @Test + public void enteringNonExistingIDWhenUpdatingName(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 4; + + Assertions.assertEquals("ID is not in todo list", todoList.updateNameByID(nonExistingID, "Do homework")); + } + + @Test + public void enteringExistingIDWhenUpdatingName(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 1; + + Assertions.assertEquals("Task with ID 1 has a new name: Do homework", todoList.updateNameByID(nonExistingID, "Do homework")); + } + + // EXTENSION 3 + @Test + public void enteringNonExistingIDWhenUpdatingStatus(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 4; + + Assertions.assertEquals("ID is not in todo list", todoList.updateStatusByID(nonExistingID, true)); + } + + @Test + public void enteringExistingIDWhenUpdatingStatus(){ + TodoListExtension todoList = new TodoListExtension(items); + Integer nonExistingID = 3; + + Assertions.assertEquals("Task with ID 3 has a new status: completed", todoList.updateStatusByID(nonExistingID, true)); + } + + // EXTENSION 4 + @Test + public void noTasksAreInTheList(){ + ArrayList emptyList = new ArrayList<>(){}; + TodoListExtension todoList = new TodoListExtension(emptyList); + + Assertions.assertEquals("There are no tasks in the list!", todoList.showDateTimeForEachTask()); + } + + @Test + public void thereExistsTasksInTheList(){ + TodoListExtension todoList = new TodoListExtension(items); + String correctString = + "Task with id: 1, Clean room, was created: 2024-10-25T08:50:11\n" + + "Task with id: 2, Buy groceries, was created: 2024-10-26T10:22:18\n" + + "Task with id: 3, Write CV, was created: 2024-10-30T17:30:44\n"; + + + + Assertions.assertEquals(correctString, todoList.showDateTimeForEachTask()); + } +}