diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 000000000..245362b83 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,57 @@ +## Core TodoList class + +| 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) | | 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 | +| | | | | +| listCompletedTasks() | | There are completed tasks in the todolist | string with all completed tasks | +| | | There are no completed tasks in the todolist | string with error message | +| | | | | +| listNotCompletedTasks() | | There are uncompleted tasks in the todolist | string with all uncompleted tasks | +| | | There are no uncompleted tasks in the todolist | string with error message | +| | | | | +| searchForTask(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 was in todolist and has been removed | string with confirmation of removal | +| | | Task was not in todolist or could not be removed | string with error message | +| | | | | +| 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 TodoList class + +| Method | Member Variables | Scenario | Result | +|-------------------------------------|------------------|------------------------------------------|-----------------------------| +| | ArrayList | | | +| | | | | +| getTaskById(int id) | | incomplete task | message string | +| | | completed task | message string | +| updateTaskName(int id, String name) | | incomplete task | message string | +| | | completed task | message string | +| updateTaskStatus(int id) | | task exists and has been completed | true | +| | | task exists and is not completed anymore | false | +| showDateTime() | | list has tasks | string of tasks | +| | | last has no tasks | string message for no tasks | + +## Extension Task class + +| Method | Member variables | Scenario | Result | +|---------|------------------|----------|--------| +| | int id | | | +| | String name | | | +| | boolean status | | | +| | String time | | | +| getters | | | | +| setters | | | | + diff --git a/src/main/java/com/booleanuk/core/TodoList.java b/src/main/java/com/booleanuk/core/TodoList.java index 675adaf0b..42839bdcd 100644 --- a/src/main/java/com/booleanuk/core/TodoList.java +++ b/src/main/java/com/booleanuk/core/TodoList.java @@ -1,5 +1,114 @@ package com.booleanuk.core; +import java.util.*; + public class TodoList { + private HashMap tasks; + + public TodoList(HashMap tasks) { + this.tasks = new HashMap<>(tasks); + } + + public Boolean add(String name) { + Boolean status = false; + if (!tasks.containsKey(name)) { + tasks.put(name, status); + return true; + } + return false; + } + + public String listTasks() { + String output; + if (tasks.isEmpty()) { + output = "Todo-list is empty"; + return output; + } + output = tasks.keySet().toString(); + return output; + } + + public Boolean updateTaskStatus(String name) { + if (!tasks.containsKey(name)) { + return false; + } + if (tasks.get(name).equals(false)) { + tasks.put(name, true); + return true; + } else if (tasks.get(name).equals(true)) { + tasks.put(name, false); + return true; + } + return false; + } + + public String listCompletedTasks() { + if (tasks.containsValue(true)) { + StringBuilder completed = new StringBuilder(); + for (Map.Entry task : tasks.entrySet()) { + if (task.getValue()) { + completed.append(task.getKey()).append(", "); + } + } + return completed.toString(); + } + return "You have no completed tasks"; + } + + public String listNotCompletedTasks() { + if (tasks.containsValue(false)) { + StringBuilder completed = new StringBuilder(); + for (Map.Entry task : tasks.entrySet()) { + if (!task.getValue()) { + completed.append(task.getKey()).append(", "); + } + } + return completed.toString(); + } + return "You have no incomplete tasks"; + } + + public String searchForTask(String task) { + if (!tasks.containsKey(task)) { + return "Task could not be found"; + } + return "Here's your task: " + task; + } + + public String removeTask(String task) { + if (!tasks.containsKey(task)) { + return "This task is not on your todolist"; + } + tasks.remove(task); + if (!tasks.containsKey(task)) { + return "Task removed successfully"; + } + return "Could not remove task"; + } + + public String listAscending() { + if (!tasks.isEmpty()) { + StringBuilder ascending = new StringBuilder(); + List tasksNames = new ArrayList<>(tasks.keySet()); + Collections.sort(tasksNames); + for (String task : tasksNames) { + ascending.append(task).append(", "); + } + return ascending.toString(); + } + return "Your todolist is empty"; + } + public String listDescending() { + if (!tasks.isEmpty()) { + StringBuilder ascending = new StringBuilder(); + List tasksNames = new ArrayList<>(tasks.keySet()); + Collections.sort(tasksNames, Collections.reverseOrder()); + for (String task : tasksNames) { + ascending.append(task).append(", "); + } + return ascending.toString(); + } + return "Your todolist is empty"; + } } diff --git a/src/main/java/com/booleanuk/extension/ExtensionTodoList.java b/src/main/java/com/booleanuk/extension/ExtensionTodoList.java new file mode 100644 index 000000000..0cccbb7ef --- /dev/null +++ b/src/main/java/com/booleanuk/extension/ExtensionTodoList.java @@ -0,0 +1,50 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; + +public class ExtensionTodoList { + + private ArrayList tasks; + + public ExtensionTodoList(ArrayList tasks) { + this.tasks = new ArrayList<>(tasks); + } + + public String getTaskById(int id) { + String output; + Task task = this.tasks.get(id); + if (task.isStatus()) { + output = task.getName() + ", completed"; + return output; + } + output = task.getName() + ", incomplete"; + return output; + } + + public String updateTaskName(int id, String name) { + Task task = this.tasks.get(id); + task.setName(name); + return "Task successfully updated to: " + task.getName(); + } + + public boolean updateTaskStatus(int id) { + Task task = this.tasks.get(id); + if (!task.isStatus()) { + task.setStatus(true); + return task.isStatus(); + } + task.setStatus(false); + return task.isStatus(); + } + + public String showDateTime() { + StringBuilder output = new StringBuilder(); + if (this.tasks.isEmpty()) { + return "Todolist is empty"; + } + for (Task task : tasks) { + output.append("Task: ").append(task.getName()).append(", Created on: ").append(task.getTime()).append("\n"); + } + return output.toString(); + } +} 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..fa59ddc80 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Task.java @@ -0,0 +1,49 @@ +package com.booleanuk.extension; + +public class Task { + private static int nextId = 1; + private int id; + private String name; + private boolean status; + private String time; + + public Task(String name, boolean status, String time) { + this.id = nextId; + nextId++; + this.name = name; + this.status = status; + this.time = time; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } +} diff --git a/src/test/java/com/booleanuk/core/TodoListTest.java b/src/test/java/com/booleanuk/core/TodoListTest.java index 0bef779a4..89d0da39c 100644 --- a/src/test/java/com/booleanuk/core/TodoListTest.java +++ b/src/test/java/com/booleanuk/core/TodoListTest.java @@ -1,13 +1,90 @@ package com.booleanuk.core; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.HashMap; + class TodoListTest { + + private TodoList todoList; + + @BeforeEach + public void setUp() { + HashMap tasks = new HashMap<>() {{ + put("Tidy up", false); + put("Lunch", true); + }}; + todoList = new TodoList(tasks); + } + + @Test + public void testAddNewTaskToList() { + Assertions.assertTrue(todoList.add("Sleep")); + } + + @Test + public void testAddOldTaskToList() { + Assertions.assertFalse(todoList.add("Lunch")); + } + + @Test + public void testListAllTasks() { + Assertions.assertEquals("[Tidy up, Lunch]", todoList.listTasks()); + } + + @Test + public void testListNoTask() { + HashMap noTasks = new HashMap<>(); + TodoList todoList = new TodoList(noTasks); + Assertions.assertEquals("Todo-list is empty", todoList.listTasks()); + } + + @Test + public void testUpdateTaskStatus() { + Assertions.assertTrue(todoList.updateTaskStatus("Tidy up")); + Assertions.assertTrue(todoList.updateTaskStatus("Lunch")); + } + + @Test + public void testListCompletedTasks() { + Assertions.assertEquals("Lunch, ", todoList.listCompletedTasks()); + todoList.removeTask("Lunch"); + Assertions.assertEquals("You have no completed tasks", todoList.listCompletedTasks()); + } + + @Test + public void testListNotCompletedTasks() { + Assertions.assertEquals("Tidy up, ", todoList.listNotCompletedTasks()); + todoList.removeTask("Tidy up"); + Assertions.assertEquals("You have no incomplete tasks", todoList.listNotCompletedTasks()); + } + + @Test + public void testSearchForTask() { + Assertions.assertEquals("Task could not be found", todoList.searchForTask("Shower")); + } + + @Test + public void testRemoveTask() { + Assertions.assertEquals("Task removed successfully", todoList.removeTask("Lunch")); + Assertions.assertEquals("This task is not on your todolist", todoList.removeTask("Shower")); + } + + @Test + public void testListsAscending() { + Assertions.assertEquals("Lunch, Tidy up, ", todoList.listAscending()); + todoList.removeTask("Lunch"); + todoList.removeTask("Tidy up"); + Assertions.assertEquals("Your todolist is empty", todoList.listAscending()); + } + @Test - public void exampleTest() { - String hello = "Hello"; - Assertions.assertEquals("Hello", hello); - Assertions.assertNotEquals("Goodbye", hello); + public void testListsDescending() { + Assertions.assertEquals("Tidy up, Lunch, ", todoList.listDescending()); + todoList.removeTask("Lunch"); + todoList.removeTask("Tidy up"); + Assertions.assertEquals("Your todolist is empty", todoList.listDescending()); } } diff --git a/src/test/java/com/booleanuk/extension/ExtensionTodoListTest.java b/src/test/java/com/booleanuk/extension/ExtensionTodoListTest.java new file mode 100644 index 000000000..4a2da2d05 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/ExtensionTodoListTest.java @@ -0,0 +1,46 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class ExtensionTodoListTest { + private ExtensionTodoList todoList; + + @BeforeEach + public void setUp() { + ArrayList tasks = new ArrayList<>() {{ + add(new Task("Eat", false, "2025-01-19T10:00:00")); + add(new Task("Sleep", true, "2025-01-20T09:00:00")); + }}; + todoList = new ExtensionTodoList(tasks); + } + + @Test + public void testGetTaskById() { + Assertions.assertEquals("Eat, incomplete", todoList.getTaskById(0)); + Assertions.assertEquals("Sleep, completed", todoList.getTaskById(1)); + } + + @Test + public void testUpdateTaskName() { + Assertions.assertEquals("Task successfully updated to: Dinner", todoList.updateTaskName(0, "Dinner")); + Assertions.assertEquals("Task successfully updated to: sleep", todoList.updateTaskName(1, "sleep")); + } + + @Test + public void testUpdateTaskStatus() { + Assertions.assertTrue(todoList.updateTaskStatus(0)); + Assertions.assertFalse(todoList.updateTaskStatus(1)); + } + + @Test + public void testShowDateTime() { + Assertions.assertEquals("Task: Eat, Created on: 2025-01-19T10:00:00\nTask: Sleep, Created on: 2025-01-20T09:00:00\n", todoList.showDateTime()); + ArrayList empty = new ArrayList<>(); + ExtensionTodoList emptyTodoList = new ExtensionTodoList(empty); + Assertions.assertEquals("Todolist is empty", emptyTodoList.showDateTime()); + } +}