diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 000000000..082780687 --- /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 | List with all tasks | +| | | There are no tasks in the todolist | List with error message | +| | | | | +| updateTaskStatus(String name, Boolean status) | | There is a task with the provided name | true | +| | | There is no task with the provided name | false | +| | | | | +| getCompletedTasks() | | There are completed tasks in the todolist | List with all completed tasks | +| | | There are no completed tasks in the todolist | List with error message | +| | | | | +| getNotCompletedTasks() | | There are uncompleted tasks in the todolist | List with all uncompleted tasks | +| | | There are no uncompleted tasks in the todolist | List with error message | +| | | | | +| SearchTask(String) | | There was a task with the provided name | string with a message | +| | | 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 | List in descending order by name | +| | | There are no tasks in the todolist | List with error message | +| | | | | +| taskAscending() | | There are tasks in the todolist | List in ascending order by name | +| | | There are no tasks in the todolist | List with error message | + + + +# Extension + +## Todo + +| Method | Member Variable | Scenario | Result | +|----------------------------------------------|-----------------------|---------------------------------------------------|--------| +| | ArrayList tasks | | | +| | | | | +| | | | | +| | | | | +| getTaskById(int id) | | Search after a task with an existing ID | String | +| | | Search after a task with no existing ID | String | +| | | | | +| updateTaskName(int id, String newName) | | Update a task that exists with a new name | true | +| | | Update a task that does not exist with a new name | false | +| | | | | +| updateTaskStatusById(int id, Boolean status) | | Update a task that exists to wished status | true | +| | | Update a task that exists to wished status | false | + +## Task + +| Method | Member Variable | Scenario | Result | +|---------------------|----------------------|----------|--------| +| | Static int idCounter | | | +| | int id | | | +| | String name | | | +| | Boolean complete | | | +| | String details | | | +| Getters and setters | | | | \ 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..b1bcb9f09 100644 --- a/src/main/java/com/booleanuk/core/TodoList.java +++ b/src/main/java/com/booleanuk/core/TodoList.java @@ -1,5 +1,133 @@ package com.booleanuk.core; +import java.util.*; + public class TodoList { + public HashMap tasks = new HashMap<>(); + + public boolean add(String taskName){ + if(tasks.containsKey(taskName)){ + return false; + } + tasks.put(taskName, false); + return true; + } + + public List listTasks(){ + List strings = new ArrayList() {}; + String errorMsg = "Your todolist is empty"; + + + strings.addAll(tasks.keySet()); + + + if(strings.isEmpty()) { + strings.add(errorMsg); + return strings; + } + + return strings; + } + + public boolean updateTaskStatus(String taskName, Boolean taskCompletion){ + if(tasks.containsKey(taskName)){ + tasks.replace(taskName, taskCompletion); + return true; + } + return false; + } + + public List getCompletedTasks(){ + List strings = new ArrayList() {}; + String errorMsg = "You have no completed tasks"; + + + for (String task : tasks.keySet()){ + if(tasks.get(task)){ + strings.add(task); + + } + } + + if(strings.isEmpty()) { + strings.add(errorMsg); + return strings; + } + + return strings; + } + + public List getUnCompletedTasks(){ + List strings = new ArrayList() {}; + String errorMsg = "You have no uncompleted tasks"; + + + for (String task : tasks.keySet()){ + if(!tasks.get(task)){ + strings.add(task); + + } + } + + if(strings.isEmpty()) { + strings.add(errorMsg); + return strings; + } + + return strings; + } + + public List searchTask(String taskName){ + List strings = new ArrayList() {}; + String message = "There was no task with that name"; + + + if(tasks.containsKey(taskName)){ + message = "There is a task with that name"; + strings.add(message); + return strings; + } + strings.add(message); + + return strings; + } + + public boolean removeTask(String taskName){ + if(tasks.containsKey(taskName)){ + tasks.remove(taskName); + return true; + } + return false; + } + + public List tasksDescending(){ + List strings = new ArrayList(tasks.keySet()) {}; + String errorMsg = "No tasks available"; + + Collections.sort(strings); + + if(strings.isEmpty()){ + strings.add(errorMsg); + return strings; + } + + return strings; + } + + public List tasksAscending(){ + List strings = new ArrayList(tasks.keySet()) {}; + String errorMsg = "No tasks available"; + + Collections.sort(strings); + + Collections.reverse(strings); + + if(strings.isEmpty()){ + strings.add(errorMsg); + return strings; + } + + return strings; + } } diff --git a/src/main/java/com/booleanuk/extension/Task.java b/src/main/java/com/booleanuk/extension/Task.java new file mode 100644 index 000000000..47a460a93 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Task.java @@ -0,0 +1,42 @@ +package com.booleanuk.extension; + +public class Task { + + public static int idCounter = 0; + private int id; + private String name; + private Boolean complete; + private String details; + + public Task(String name, String details){ + id = idCounter++; + this.name = name; + this.details = details; + complete = false; + } + + public Boolean getComplete() { + return complete; + } + + public void setComplete(Boolean complete) { + this.complete = complete; + } + + public String getDetails() { + return details; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return id; + } + +} 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..19b10edca --- /dev/null +++ b/src/main/java/com/booleanuk/extension/TodoListExtension.java @@ -0,0 +1,58 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +public class TodoListExtension { + + public ArrayList extensionTask = new ArrayList<>(); + + public String getTaskByID(int id){ + String output = "No task with that ID was found"; + for(Task task : extensionTask){ + if(id == task.getId()){ + output = "ID: " + task.getId() + ", Name: " + task.getName() + ", Complete: " + task.getComplete() + ", Details: " + task.getDetails(); + } + } + + return output; + } + + public Boolean updateTaskName(int id, String newName){ + for(Task task : extensionTask){ + if(id == task.getId()){ + task.setName(newName); + } + } + + for(Task task : extensionTask){ + if(task.getName() == newName){ + task.setName(newName); + return true; + } + } + + return false; + } + + public Boolean updateTaskStatusById(int id, Boolean status){ + for(Task task : extensionTask){ + if(id == task.getId()){ + task.setComplete(status); + } + } + + for(Task task : extensionTask){ + if(id == task.getId()){ + if(task.getComplete() == status){ + return true; + } + } + } + return false; + } + + +} diff --git a/src/test/java/com/booleanuk/core/TodoListTest.java b/src/test/java/com/booleanuk/core/TodoListTest.java index 0bef779a4..d78ee7f92 100644 --- a/src/test/java/com/booleanuk/core/TodoListTest.java +++ b/src/test/java/com/booleanuk/core/TodoListTest.java @@ -3,11 +3,285 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + class TodoListTest { @Test - public void exampleTest() { - String hello = "Hello"; - Assertions.assertEquals("Hello", hello); - Assertions.assertNotEquals("Goodbye", hello); + public void addTaskToListThatAlreadyExist() { + TodoList todoList = new TodoList(); + String taskName = "clean"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + Assertions.assertFalse(todoList.add(taskName)); + } + + @Test + public void addTaskToListThatDoesNotAlreadyExist() { + TodoList todoList = new TodoList(); + String taskName = "run"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + Assertions.assertTrue(todoList.add(taskName)); + } + + @Test + public void listAllTasksInTodolist() { + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.addAll(todoList.tasks.keySet()); + + Assertions.assertLinesMatch(strings, todoList.listTasks() ); + } + + @Test + public void listAllTasksInTodolistWhenItIsEmpty() { + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + strings.add("Your todolist is empty"); + + Assertions.assertLinesMatch(strings, todoList.listTasks() ); + } + + @Test + public void updateTaskStatusWithExistingName(){ + TodoList todoList = new TodoList(); + String taskName = "clean"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + Assertions.assertTrue(todoList.updateTaskStatus(taskName , true)); + + } + + @Test + public void updateTaskStatusWithNonExistingName(){ + TodoList todoList = new TodoList(); + String taskName = "watch tv"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + Assertions.assertFalse(todoList.updateTaskStatus(taskName , true)); + + } + + @Test + public void getCompletedTasksWhenNotEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + todoList.updateTaskStatus("clean" , true); + todoList.updateTaskStatus("wash" , true); + + strings.add("wash"); + strings.add("clean"); + + Assertions.assertLinesMatch(strings, todoList.getCompletedTasks() ); + } + + @Test + public void getCompletedTasksWhenEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.add("You have no completed tasks"); + + Assertions.assertLinesMatch(strings, todoList.getCompletedTasks() ); + } + + @Test + public void getUnCompletedTasksWhenEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + todoList.updateTaskStatus("clean" , true); + todoList.updateTaskStatus("wash" , true); + todoList.updateTaskStatus("vacuum" , true); + todoList.updateTaskStatus("walk the dog" , true); + + strings.add("You have no uncompleted tasks"); + + Assertions.assertLinesMatch(strings, todoList.getUnCompletedTasks() ); + } + + @Test + public void getUnCompletedTasksWhenNotEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + todoList.updateTaskStatus("clean" , true); + todoList.updateTaskStatus("wash" , true); + + strings.add("vacuum"); + strings.add("walk the dog"); + + Assertions.assertLinesMatch(strings, todoList.getUnCompletedTasks() ); + } + + @Test + public void searchTaskThatDoesNotExist(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + String taskName = "watch tv"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.add("There was no task with that name"); + + Assertions.assertLinesMatch(strings, todoList.searchTask(taskName) ); + + } + + @Test + public void searchTaskThatDoesExist(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + String taskName = "wash"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.add("There is a task with that name"); + + Assertions.assertLinesMatch(strings, todoList.searchTask(taskName) ); + + } + + @Test + public void removeTaskThatDoesExist(){ + TodoList todoList = new TodoList(); + String taskName = "wash"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + + Assertions.assertTrue(todoList.removeTask(taskName)); + + } + + @Test + public void removeTaskThatDoesNotExist(){ + TodoList todoList = new TodoList(); + String taskName = "watch tv"; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + + Assertions.assertFalse(todoList.removeTask(taskName)); + + } + + @Test + public void tasksDescendingWhenNotEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.add("clean"); + strings.add("vacuum"); + strings.add("walk the dog"); + strings.add("wash"); + + Assertions.assertLinesMatch(strings, todoList.tasksDescending() ); + + } + + @Test + public void tasksDescendingWhenEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + strings.add("No tasks available"); + + Assertions.assertLinesMatch(strings, todoList.tasksDescending() ); + + } + + @Test + public void tasksAscendingWhenNotEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + todoList.add("clean"); + todoList.add("wash"); + todoList.add("vacuum"); + todoList.add("walk the dog"); + + strings.add("wash"); + strings.add("walk the dog"); + strings.add("vacuum"); + strings.add("clean"); + + Assertions.assertLinesMatch(strings, todoList.tasksAscending() ); + + } + + @Test + public void tasksAscendingWhenEmpty(){ + TodoList todoList = new TodoList(); + List strings = new ArrayList<>() {}; + + strings.add("No tasks available"); + + Assertions.assertLinesMatch(strings, todoList.tasksAscending() ); + } } 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..e1b016841 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/TodoListExtensionTest.java @@ -0,0 +1,93 @@ +package com.booleanuk.extension; + +import com.booleanuk.core.TodoList; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TodoListExtensionTest { + + @BeforeEach + public void reset(){ + Task.idCounter = 0; + } + + @Test + public void getTaskByUniqueIdThatDoesExist(){ + TodoListExtension todoList = new TodoListExtension(); + Task task1 = new Task("Clean", "Vacuum"); + Task task2 = new Task("Wash", "Wash clothes"); + Task task3 = new Task("Dishes", "Empty the dishwasher"); + Task task4 = new Task("Garbage", "Take out the garbage"); + + todoList.extensionTask.add(task1); + todoList.extensionTask.add(task2); + todoList.extensionTask.add(task3); + todoList.extensionTask.add(task4); + + String output = "ID: " + task2.getId() + ", Name: " + task2.getName() + ", Complete: " + task2.getComplete() + ", Details: " + task2.getDetails(); + + Assertions.assertEquals(output, todoList.getTaskByID(1)); + + + } + + @Test + public void getTaskByUniqueIdThatDoesNotExist(){ + TodoListExtension todoList = new TodoListExtension(); + Task task1 = new Task("Clean", "Vacuum"); + Task task2 = new Task("Wash", "Wash clothes"); + Task task3 = new Task("Dishes", "Empty the dishwasher"); + Task task4 = new Task("Garbage", "Take out the garbage"); + + todoList.extensionTask.add(task1); + todoList.extensionTask.add(task2); + todoList.extensionTask.add(task3); + todoList.extensionTask.add(task4); + + String output = "No task with that ID was found"; + + Assertions.assertEquals(output, todoList.getTaskByID(100)); + + + } + + @Test + public void updateNameWithTaskId(){ + TodoListExtension todoList = new TodoListExtension(); + Task task1 = new Task("Clean", "Vacuum"); + Task task2 = new Task("Wash", "Wash clothes"); + Task task3 = new Task("Dishes", "Empty the dishwasher"); + Task task4 = new Task("Garbage", "Take out the garbage"); + + todoList.extensionTask.add(task1); + todoList.extensionTask.add(task2); + todoList.extensionTask.add(task3); + todoList.extensionTask.add(task4); + + Assertions.assertTrue(todoList.updateTaskName(2, "Dishwasher")); + + + } + + @Test + public void changeStatusWithTaskId(){ + TodoListExtension todoList = new TodoListExtension(); + Task task1 = new Task("Clean", "Vacuum"); + Task task2 = new Task("Wash", "Wash clothes"); + Task task3 = new Task("Dishes", "Empty the dishwasher"); + Task task4 = new Task("Garbage", "Take out the garbage"); + + todoList.extensionTask.add(task1); + todoList.extensionTask.add(task2); + todoList.extensionTask.add(task3); + todoList.extensionTask.add(task4); + + Assertions.assertTrue(todoList.updateTaskStatusById(3, true)); + + + } +}