diff --git a/bin/main/com/booleanuk/core/TodoList.class b/bin/main/com/booleanuk/core/TodoList.class new file mode 100644 index 000000000..2fac433d1 Binary files /dev/null and b/bin/main/com/booleanuk/core/TodoList.class differ diff --git a/bin/main/com/booleanuk/extension/.gitkeep b/bin/main/com/booleanuk/extension/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/bin/test/com/booleanuk/core/TodoListTest.class b/bin/test/com/booleanuk/core/TodoListTest.class new file mode 100644 index 000000000..6b45238af Binary files /dev/null and b/bin/test/com/booleanuk/core/TodoListTest.class differ diff --git a/bin/test/com/booleanuk/extension/.gitkeep b/bin/test/com/booleanuk/extension/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/com/booleanuk/core/Task.java b/src/main/java/com/booleanuk/core/Task.java new file mode 100644 index 000000000..4ec92fea8 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Task.java @@ -0,0 +1,25 @@ +package com.booleanuk.core; + +public class Task { + + private String name; + private boolean completed; + + + public Task (String name) { + this.name = name; + this.completed = false; + } + + public String getName() { + return this.name; + } + + public boolean isStatus() { + return this.completed; + } + + public void setStatus(boolean completed) { + this.completed = completed; + } +} diff --git a/src/main/java/com/booleanuk/core/TodoList.java b/src/main/java/com/booleanuk/core/TodoList.java index 675adaf0b..c5a170d87 100644 --- a/src/main/java/com/booleanuk/core/TodoList.java +++ b/src/main/java/com/booleanuk/core/TodoList.java @@ -1,5 +1,111 @@ package com.booleanuk.core; +import java.util.ArrayList; +import java.util.List; + public class TodoList { + private List tasks; + + public TodoList() { + this.tasks = new ArrayList<>(); + } + + public void add(String name) { + this.tasks.add(new Task(name)); + } + + public List getTasks() { + return this.tasks; + } + + public void changeStatus(String name, boolean status) { + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + task.setStatus(status); + } + } + } + + public List getCompleted() { + List completedTasks = new ArrayList(); + for (Task task : this.tasks) { + if (task.isStatus() == true) { + completedTasks.add(task); + } + + } + return completedTasks; + } + + public List getIncompleted() { + List IncompletedTasks = new ArrayList(); + for (Task task : this.tasks) { + if (task.isStatus() == false) { + IncompletedTasks.add(task); + } + + } + return IncompletedTasks; + } + + public String searchTask(String name) { + String searchedTask = "Task not found"; + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + searchedTask = name; + } + + } + return searchedTask; + } + + public void deleteTask(String name) { + Task taskToDelete = null; + + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + taskToDelete = task; + } + } + if (taskToDelete != null) { + this.tasks.remove(taskToDelete); + } + } + + public List getAscendingList() { + List sorted = new ArrayList<>(this.tasks); + + for (int i = 0; i < sorted.size() - 1; i++) { + for (int j = 0; j < sorted.size() - 1 - i; j++) { + Task current = sorted.get(j); + Task next = sorted.get(j + 1); + + if (current.getName().compareTo(next.getName()) > 0) { + sorted.set(j, next); + sorted.set(j + 1, current); + } + } + } + return sorted; + } + + + public List getDescendingList() { + List sorted = new ArrayList<>(this.tasks); + + for (int i = 0; i < sorted.size() - 1; i++) { + for (int j = 0; j < sorted.size() - 1 - i; j++) { + Task current = sorted.get(j); + Task next = sorted.get(j + 1); + + if (current.getName().compareTo(next.getName()) < 0) { + sorted.set(j, next); + sorted.set(j + 1, current); + } + } + } + return sorted; + } + } 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..c5a597ca3 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Task.java @@ -0,0 +1,43 @@ +package com.booleanuk.extension; + +import java.time.LocalDateTime; + +public class Task { + + private String name; + private boolean completed; + private final int id; // assigned once, never again. + private final LocalDateTime createdAt; + + + public Task (String name, int id) { + this.name = name; + this.completed = false; + this.id = id; + this.createdAt = LocalDateTime.now(); + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return this.id; + } + + public LocalDateTime getCreatedAt() { + return this.createdAt; + } + + public boolean isStatus() { + return this.completed; + } + + public void setStatus(boolean completed) { + this.completed = completed; + } +} diff --git a/src/main/java/com/booleanuk/extension/TodoList.java b/src/main/java/com/booleanuk/extension/TodoList.java new file mode 100644 index 000000000..e1e2b7d12 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/TodoList.java @@ -0,0 +1,139 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.List; + +public class TodoList { + + private List tasks; + private int nextId = 1; + + public TodoList() { + this.tasks = new ArrayList<>(); + } + + public void add(String name) { + this.tasks.add(new Task(name, nextId)); + this.nextId++; + } + + public List getTasks() { + return this.tasks; + } + + public void changeStatus(String name, boolean status) { + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + task.setStatus(status); + } + } + } + + public List getCompleted() { + List completedTasks = new ArrayList(); + for (Task task : this.tasks) { + if (task.isStatus() == true) { + completedTasks.add(task); + } + + } + return completedTasks; + } + + public List getIncompleted() { + List IncompletedTasks = new ArrayList(); + for (Task task : this.tasks) { + if (task.isStatus() == false) { + IncompletedTasks.add(task); + } + + } + return IncompletedTasks; + } + + public String searchTask(String name) { + String searchedTask = "Task not found"; + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + searchedTask = name; + } + + } + return searchedTask; + } + + public void deleteTask(String name) { + Task taskToDelete = null; + + for (Task task : this.tasks) { + if (task.getName().equals(name)) { + taskToDelete = task; + } + } + if (taskToDelete != null) { + this.tasks.remove(taskToDelete); + } + } + + public List getAscendingList() { + List sorted = new ArrayList<>(this.tasks); + + for (int i = 0; i < sorted.size() - 1; i++) { + for (int j = 0; j < sorted.size() - 1 - i; j++) { + Task current = sorted.get(j); + Task next = sorted.get(j + 1); + + if (current.getName().compareTo(next.getName()) > 0) { + sorted.set(j, next); + sorted.set(j + 1, current); + } + } + } + return sorted; + } + + + public List getDescendingList() { + List sorted = new ArrayList<>(this.tasks); + + for (int i = 0; i < sorted.size() - 1; i++) { + for (int j = 0; j < sorted.size() - 1 - i; j++) { + Task current = sorted.get(j); + Task next = sorted.get(j + 1); + + if (current.getName().compareTo(next.getName()) < 0) { + sorted.set(j, next); + sorted.set(j + 1, current); + } + } + } + return sorted; + } + + public Task getTaskById(int id) { + for (Task task : this.tasks) { + if (task.getId() == id) { + return task; + } + } + return null; + + } + + public void changeNameById(int id, String newName) { + Task task = getTaskById(id); + + if (task != null) { + task.setName(newName); + } + } + + public void changeStatus(int id) { + Task task = getTaskById(id); + if (task != null) { + task.setStatus(!task.isStatus()); + } + } + + +} diff --git a/src/test/java/com/booleanuk/core/TaskTest.java b/src/test/java/com/booleanuk/core/TaskTest.java new file mode 100644 index 000000000..5728742cf --- /dev/null +++ b/src/test/java/com/booleanuk/core/TaskTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class TaskTest { + +} diff --git a/src/test/java/com/booleanuk/core/TodoListTest.java b/src/test/java/com/booleanuk/core/TodoListTest.java index 0bef779a4..df3502bab 100644 --- a/src/test/java/com/booleanuk/core/TodoListTest.java +++ b/src/test/java/com/booleanuk/core/TodoListTest.java @@ -1,13 +1,145 @@ package com.booleanuk.core; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class TodoListTest { + + @Test public void exampleTest() { String hello = "Hello"; Assertions.assertEquals("Hello", hello); Assertions.assertNotEquals("Goodbye", hello); } + + @Test + public void addTest() { + TodoList todoList = new TodoList(); + todoList.add("Wash the dog"); + Assertions.assertEquals("Wash the dog", todoList.getTasks().get(0).getName()); + } + + @Test + public void getAllTasksTest() { + TodoList todoList = new TodoList(); + todoList.add("Wash the car"); + todoList.add("code some Java"); + todoList.add("eat pizza"); + + int taskAmount = todoList.getTasks().size(); + + Assertions.assertEquals(3, taskAmount); + Assertions.assertEquals("Wash the car", todoList.getTasks().get(0).getName()); + Assertions.assertEquals("code some Java", todoList.getTasks().get(1).getName()); + Assertions.assertEquals("eat pizza", todoList.getTasks().get(2).getName()); + } + + @Test + public void changeStatusTest() { + TodoList todoList = new TodoList(); + todoList.add("Wash the car"); + + Assertions.assertFalse(todoList.getTasks().get(0).isStatus()); + + todoList.changeStatus("Wash the car", true); + + Assertions.assertTrue(todoList.getTasks().get(0).isStatus()); + + todoList.changeStatus("Wash the car", false); + + Assertions.assertFalse(todoList.getTasks().get(0).isStatus()); + } + + @Test + public void getCompletedTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + todoList.add("eat ice cream"); + todoList.changeStatus("wash the car", true); + + List completed = todoList.getCompleted(); + + Assertions.assertEquals(1, completed.size()); + Assertions.assertEquals("wash the car", completed.get(0).getName()); + + } + + @Test + public void getIncompletedTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + todoList.add("eat ice cream"); + todoList.changeStatus("wash the car", true); + + List completed = todoList.getIncompleted(); + + Assertions.assertEquals(1, completed.size()); + Assertions.assertEquals("eat ice cream", completed.get(0).getName()); + } + + @Test + public void searchTaskTest() { + TodoList todoList = new TodoList(); + String carTask = "wash the car"; + todoList.add(carTask); + + Assertions.assertEquals(carTask, todoList.searchTask(carTask)); + Assertions.assertEquals("Task not found", todoList.searchTask("walk the dog")); + } + + @Test + public void deleteTaskTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + todoList.add("eat pizza"); + todoList.deleteTask("eat pizza"); + + int taskAmount = todoList.getTasks().size(); + + Assertions.assertEquals(1, taskAmount); + Assertions.assertEquals("wash the car", todoList.getTasks().get(0).getName()); + } + + @Test + public void getAscendingListTest() { + TodoList todoList = new TodoList(); + + todoList.add("wash the car"); + todoList.add("eat pizza"); + todoList.add("code some java"); + + List sorted = todoList.getAscendingList(); + + Assertions.assertEquals(3, sorted.size()); + Assertions.assertEquals("code some java", sorted.get(0).getName()); + Assertions.assertEquals("eat pizza", sorted.get(1).getName()); + Assertions.assertEquals("wash the car", sorted.get(2).getName()); + + } + + @Test + public void getDescendingListTest() { + TodoList todoList = new TodoList(); + + todoList.add("wash the car"); + todoList.add("eat pizza"); + todoList.add("code some java"); + + List sorted = todoList.getDescendingList(); + + Assertions.assertEquals(3, sorted.size()); + Assertions.assertEquals("wash the car", sorted.get(0).getName()); + Assertions.assertEquals("eat pizza", sorted.get(1).getName()); + Assertions.assertEquals("code some java", sorted.get(2).getName()); + + } + + + + } diff --git a/src/test/java/com/booleanuk/extension/TaskTest.java b/src/test/java/com/booleanuk/extension/TaskTest.java new file mode 100644 index 000000000..42a340604 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/TaskTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.extension; + +public class TaskTest { + +} diff --git a/src/test/java/com/booleanuk/extension/TodoListTest.java b/src/test/java/com/booleanuk/extension/TodoListTest.java new file mode 100644 index 000000000..5a7a0e115 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/TodoListTest.java @@ -0,0 +1,62 @@ + package com.booleanuk.extension; + + import java.time.LocalDateTime; + + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class TodoListTest { + + @Test + public void getTaskByIdTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + todoList.add("eat pizza"); + + int id = todoList.getTasks().get(1).getId(); + Task found = todoList.getTaskById(id); + + Assertions.assertEquals("eat pizza", found.getName()); + + } + + @Test + public void changeNameByIdTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + + int id = todoList.getTasks().get(0).getId(); + todoList.changeNameById(id, "wash the bike"); + Assertions.assertEquals("wash the bike", todoList.getTaskById(id).getName()); + } + + @Test + public void changeStatusByIdTest() { + TodoList todoList = new TodoList(); + todoList.add("wash the car"); + int id = todoList.getTasks().get(0).getId(); + + Assertions.assertFalse(todoList.getTaskById(id).isStatus()); + todoList.changeStatus(id); + Assertions.assertTrue(todoList.getTaskById(id).isStatus()); + + todoList.changeStatus(id); + + Assertions.assertFalse(todoList.getTaskById(id).isStatus()); + } + + @Test + public void createdAtTest() { + TodoList todoList = new TodoList(); + + LocalDateTime before = LocalDateTime.now(); + todoList.add("wash the car"); + LocalDateTime after = LocalDateTime.now(); + + LocalDateTime createdAt = todoList.getTasks().get(0).getCreatedAt(); + + Assertions.assertNotNull(createdAt); + Assertions.assertFalse(createdAt.isBefore(before)); + Assertions.assertFalse(createdAt.isAfter(after)); + } +} \ No newline at end of file