Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Domain Model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

- I want to add tasks to my todo list.

| Classes | Member var. | Methods | Scenario | Output |
| -------- | --------------------------------- | ---------------- | ------------------------ | ------ |
| TodoList | HashMap<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> taskList | tasksAscending() | list is empty | Empty list |
| | | | list not empty | alphabetically sorted list in descending order |
87 changes: 87 additions & 0 deletions src/main/java/com/booleanuk/core/TodoList.java
Original file line number Diff line number Diff line change
@@ -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<String, Boolean> taskMap;

public TodoList(){
this.taskMap = new HashMap<>();
}

public HashMap<String, Boolean> 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<String> 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<String> getCompleted(){
ArrayList<String> completed = new ArrayList<>();
for(String task : taskMap.keySet()){
if(taskMap.get(task)){
completed.add(task);
}
}
return completed;
}

public ArrayList<String> getIncompleted(){
ArrayList<String> 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<String> tasksAscending(){
ArrayList<String> sortedTasks = new ArrayList<>(taskMap.keySet());
Collections.sort(sortedTasks);
return sortedTasks;
}

public ArrayList<String> tasksDescending(){
ArrayList<String> sortedTasks = new ArrayList<>(taskMap.keySet());
Collections.sort(sortedTasks);
Collections.reverse(sortedTasks);
return sortedTasks;
}

}
174 changes: 170 additions & 4 deletions src/test/java/com/booleanuk/core/TodoListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> 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<String> 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<String> expected = List.of("wash", "cook", "clean");

Assertions.assertEquals(expected, testList.tasksDescending());
}

}
Loading