diff --git a/Domain Model.md b/Domain Model.md new file mode 100644 index 000000000..918e82e9e --- /dev/null +++ b/Domain Model.md @@ -0,0 +1,70 @@ + +- I want to add tasks to my todo list. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ---------------- | ------------------------ | ------ | +| TodoList | HashMap taskList | add(String task) | Empty string | false | +| | | | task name already exists | false | +| | | | task name doesn't exist | true | + + +- I want to see all the tasks in my todo list. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ------- | --------------------- | ----------------- | +| TodoList | HashMap taskList | show() | taskList is empty | Empty list | +| | | | taskList is not empty | List of all tasks | + +- I want to change the status of a task between incomplete and complete. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | -------------------------------------------- | ------------------------------------------------------------- | ------ | +| TodoList | HashMap taskList | changeStatus(String taskName, boolean compl) | compl is true and valid taskName | true | +| | | | compl is false and valid taskName | true | +| | | | if tasks already got the status that you want to change it to | true | +| | | | taskName not in list or empty | false | + +- I want to be able to get only the complete tasks. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | --------------- | ------------------------- | ------------------------ | +| TodoList | HashMap taskList | getCompleted()) | taskList is empty | empty list | +| | | | There are completed tasks | list of incomplete tasks | +| | | | no completed task | empty list | + +- I want to be able to get only the incomplete tasks. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ---------------- | -------------------------- | ------------------------ | +| TodoList | HashMap taskList | getIncompleted() | taskList is empty | empty list | +| | | | There are incomplete tasks | list of incomplete tasks | +| | | | no incomplete task | empty list | + +- I want to search for a task and receive a message that says it wasn't found if it doesn't exist. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ------------------- | ----------------- | -------------------- | +| TodoList | HashMap taskList | search(String task) | taskList is empty | "No Tasks" | +| | | | task not in list | "Task was not found" | +| | | | task in list | task | +- I want to remove tasks from my list. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ------------------- | ---------------------- | ------ | +| TodoList | HashMap taskList | remove(String task) | String is empty string | false | +| | | | Task not in list | false | +| | | | Task in list | true | + +- I want to see all the tasks in my list ordered alphabetically in ascending order. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ---------------- | -------------- | --------------------------------------------- | +| TodoList | HashMap taskList | tasksAscending() | list is empty | Empty list | +| | | | list not empty | alphabetically sorted list in ascending order | + +- I want to see all the tasks in my list ordered alphabetically in descending order. + +| Classes | Member var. | Methods | Scenario | Output | +| -------- | --------------------------------- | ---------------- | -------------- | ---------------------------------------------- | +| TodoList | HashMap taskList | tasksAscending() | list is empty | Empty list | +| | | | list not empty | alphabetically sorted list in descending order | diff --git a/src/main/java/com/booleanuk/core/TodoList.java b/src/main/java/com/booleanuk/core/TodoList.java index 675adaf0b..182609b63 100644 --- a/src/main/java/com/booleanuk/core/TodoList.java +++ b/src/main/java/com/booleanuk/core/TodoList.java @@ -1,5 +1,92 @@ package com.booleanuk.core; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + public class TodoList { + private HashMap taskMap; + + public TodoList(){ + this.taskMap = new HashMap<>(); + } + + public HashMap getTaskMap(){ + return taskMap; + } + + public boolean add(String task){ + if(task.isEmpty() || task.trim().isEmpty() || taskMap.containsKey(task)) + return false; + + taskMap.put(task, false); + + return true; + } + + public ArrayList showTasks(){ + return new ArrayList<>(taskMap.keySet()); + } + + public boolean changeStatus(String task, boolean complete){ + if (taskMap.containsKey(task)) { + taskMap.put(task, complete); + return true; + } + return false; + } + + public ArrayList getCompleted(){ + ArrayList completed = new ArrayList<>(); + for(String task : taskMap.keySet()){ + if(taskMap.get(task)){ + completed.add(task); + } + } + return completed; + } + + public ArrayList getIncompleted(){ + ArrayList inComplete = new ArrayList<>(); + for(String task : taskMap.keySet()){ + if(!taskMap.get(task)){ + inComplete.add(task); + } + } + return inComplete; + } + + public String search(String task){ + if(taskMap.isEmpty()) + return "No tasks"; + + if(taskMap.containsKey(task)){ + return task; + } + + return "Task wasn't found"; + } + + public boolean remove(String task){ + if(taskMap.containsKey(task)) { + taskMap.remove(task); + return true; + } + return false; + } + + public ArrayList tasksAscending(){ + ArrayList sortedTasks = new ArrayList<>(taskMap.keySet()); + Collections.sort(sortedTasks); + return sortedTasks; + } + + public ArrayList tasksDescending(){ + ArrayList sortedTasks = new ArrayList<>(taskMap.keySet()); + Collections.sort(sortedTasks); + Collections.reverse(sortedTasks); + return sortedTasks; + } } diff --git a/src/test/java/com/booleanuk/core/TodoListTest.java b/src/test/java/com/booleanuk/core/TodoListTest.java index 0bef779a4..6ba1b2a6c 100644 --- a/src/test/java/com/booleanuk/core/TodoListTest.java +++ b/src/test/java/com/booleanuk/core/TodoListTest.java @@ -3,11 +3,177 @@ 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 shouldAddTask() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.add("clean")); + Assertions.assertEquals(1, testList.getTaskMap().size()); + Assertions.assertTrue(testList.getTaskMap().containsKey("clean")); + + Assertions.assertTrue(testList.add("wash")); + Assertions.assertEquals(2, testList.getTaskMap().size()); + Assertions.assertTrue(testList.getTaskMap().containsKey("wash")); + Assertions.assertTrue(testList.getTaskMap().containsKey("clean")); + + Assertions.assertFalse(testList.add("clean")); + Assertions.assertFalse(testList.add("")); + Assertions.assertFalse(testList.add(" ")); + } + + @Test + public void shouldInitializeFalse() { + TodoList testList = new TodoList(); + + testList.add("clean"); + + Assertions.assertFalse(testList.getTaskMap().get("clean")); + } + + @Test + public void shouldShowList() { + TodoList testList = new TodoList(); + + testList.add("clean"); + testList.add("wash"); + + List expectedList = List.of("clean", "wash"); + Assertions.assertEquals(expectedList.size(), testList.showTasks().size()); + Assertions.assertTrue(testList.showTasks().containsAll(expectedList)); + } + + @Test + public void shouldShowEmptyList() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.showTasks().isEmpty()); } + + @Test + public void shouldChangeStatus() { + TodoList testList = new TodoList(); + + testList.add("clean"); + testList.changeStatus("clean", true); + Assertions.assertTrue(testList.getTaskMap().get("clean")); + + testList.changeStatus("clean", false); + Assertions.assertFalse(testList.getTaskMap().get("clean")); + + testList.changeStatus("clean", false); + Assertions.assertFalse(testList.getTaskMap().get("clean")); + } + + @Test + public void shouldShowCompletedTasks() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.getCompleted().isEmpty()); + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + + Assertions.assertTrue(testList.getCompleted().isEmpty()); + + testList.changeStatus("clean", true); + testList.changeStatus("wash", true); + + List expectedCompleted = List.of("clean", "wash"); + Assertions.assertEquals(expectedCompleted.size(), testList.getCompleted().size()); + Assertions.assertTrue(testList.getCompleted().containsAll(expectedCompleted)); + Assertions.assertFalse(testList.getCompleted().contains("cook")); + } + + @Test + public void shouldShowIncompletedTasks() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.getIncompleted().isEmpty()); + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + + testList.changeStatus("clean", true); + + List expectedIncompleted = List.of("wash", "cook"); + Assertions.assertEquals(expectedIncompleted.size(), testList.getIncompleted().size()); + Assertions.assertTrue(testList.getIncompleted().containsAll(expectedIncompleted)); + Assertions.assertFalse(testList.getIncompleted().contains("clean")); + + testList.changeStatus("wash", true); + testList.changeStatus("cook", true); + + Assertions.assertTrue(testList.getIncompleted().isEmpty()); + } + + @Test + public void shouldSearch() { + TodoList testList = new TodoList(); + + Assertions.assertEquals("No tasks", testList.search("clean")); + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + + Assertions.assertEquals("clean", testList.search("clean")); + Assertions.assertEquals("Task wasn't found", testList.search("iron")); + } + + @Test + public void shouldRemove() { + TodoList testList = new TodoList();; + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + testList.remove("cook"); + + List expectedList = List.of("clean", "wash"); + + Assertions.assertEquals(expectedList.size(), testList.getTaskMap().size()); + Assertions.assertTrue(testList.showTasks().containsAll(expectedList)); + Assertions.assertFalse(testList.getTaskMap().containsKey("cook")); + + Assertions.assertFalse(testList.remove("iron")); + Assertions.assertFalse(testList.remove("")); + Assertions.assertFalse(testList.remove(" ")); + } + + @Test + public void shouldGiveTasksInAscendingOrder() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.tasksAscending().isEmpty()); + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + + List expected = List.of("clean", "cook", "wash"); + + Assertions.assertEquals(expected, testList.tasksAscending()); + } + + @Test + public void shouldGiveTasksInDescendingOrder() { + TodoList testList = new TodoList(); + + Assertions.assertTrue(testList.tasksDescending().isEmpty()); + + testList.add("clean"); + testList.add("wash"); + testList.add("cook"); + + List expected = List.of("wash", "cook", "clean"); + + Assertions.assertEquals(expected, testList.tasksDescending()); + } + }