Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
68faaaa
Add domain model draft
pudkipz Jan 9, 2025
5af6ca9
Add test for adding a task
pudkipz Jan 9, 2025
261aacd
Add method add()
pudkipz Jan 9, 2025
472fabb
Add test for adding duplicate tasks
pudkipz Jan 9, 2025
08b9cb6
Implement add() method
pudkipz Jan 9, 2025
8f69b6e
Add test for listing empty todo list
pudkipz Jan 9, 2025
23d8c63
Add trivial implementation of listTasks
pudkipz Jan 9, 2025
8aa68f4
Add test for listing multiple tasks
pudkipz Jan 9, 2025
0f70d8a
Change test for listing multiple tasks so that it's predictable
pudkipz Jan 9, 2025
5885bb0
Implement listTasks() to list all tasks with status
pudkipz Jan 9, 2025
593479f
Add test for updating tasks not in the todo list
pudkipz Jan 9, 2025
91b801d
Implement updateTaskStatus method
pudkipz Jan 9, 2025
7322663
Add test to make sure updating task completion works
pudkipz Jan 9, 2025
d869af7
Make class member todo private, adjust test accordingly
pudkipz Jan 9, 2025
c3ce186
Add test for searching for tasks
pudkipz Jan 9, 2025
baeb620
Implement search method
pudkipz Jan 9, 2025
6b709c1
Add test for removing non-existent tasks
pudkipz Jan 9, 2025
49f95b2
Add method for removing tasks
pudkipz Jan 9, 2025
a15ae92
Add test for removing tasks from list
pudkipz Jan 9, 2025
e898b34
Implement removeTask method
pudkipz Jan 9, 2025
97676de
Add method and test for listing descending tasks
pudkipz Jan 9, 2025
3776a9e
Implement method and test for listing descending tasks
pudkipz Jan 9, 2025
1546934
Add method and test for listing ascending tasks
pudkipz Jan 9, 2025
b508034
Implement method for listing ascending tasks
pudkipz Jan 9, 2025
a9e2664
Remove example test
pudkipz Jan 9, 2025
ecf22dd
Remove unused imports
pudkipz Jan 9, 2025
4bea65e
Update domain model for extension
pudkipz Jan 9, 2025
85bcd98
Copy core files to use as base for extension
pudkipz Jan 9, 2025
50aaa94
Add test for getting completed tasks, update domain model
pudkipz Jan 9, 2025
cb23c53
Implement getCompletedTasks
pudkipz Jan 9, 2025
3bc4157
Add test for and implement getCompletedTasks
pudkipz Jan 9, 2025
1ae3b9e
Add some things I'd missed, add getTaskId to domain model, add test f…
pudkipz Jan 9, 2025
d0bae33
Add trivial method getTaskId
pudkipz Jan 9, 2025
97f3eb6
Add test for ID of 2nd item
pudkipz Jan 9, 2025
dae499a
Add class Task
pudkipz Jan 9, 2025
24835f0
Adjust ID tests so that they are actually possible, implement IDs in …
pudkipz Jan 9, 2025
9ccdd0e
Add simple test for getting task by ID
pudkipz Jan 9, 2025
f974bc2
Implement getTaskById
pudkipz Jan 11, 2025
39c90e3
Add test for trying to get ID of nonexistent tasks
pudkipz Jan 11, 2025
594ab1c
Add test for updating name of nonexistent task
pudkipz Jan 11, 2025
57c43d4
Add method for updating task name
pudkipz Jan 11, 2025
c869877
Add test to verify that task name is updated
pudkipz Jan 11, 2025
e069f49
Make sure task name is updated
pudkipz Jan 11, 2025
3d37809
Add test for updating status of nonexistent task
pudkipz Jan 11, 2025
dbe08b8
Add test for verifying updating status by ID and fix updating name by ID
pudkipz Jan 11, 2025
7b128fa
Add test for verifying updating status by ID and fix updating name by ID
pudkipz Jan 11, 2025
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
81 changes: 81 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Core

## Class: TodoList

| Method | Member Variable | Scenario | Result |
|-------------------------------------------------------|--------------------------------|------------------------------------------------------------|------------------------------------|
| boolean add(String name) | HashMap<String, Boolean> tasks | Task with the provided name is not already in the todolist | true |
| | | Task with the provided name is already in the todolist | false |
| | | | |
| String listTasks() | | There are tasks in the todolist | string with all tasks |
| | | There are no tasks in the todolist | string with error message |
| | | | |
| boolean updateTaskStatus(String name, Boolean status) | | There is a task with the provided name | true |
| | | There is no task with the provided name | false |
| | | | |
| ArrayList<String> getCompletedTasks() | | There are completed tasks in the todolist | string with all completed tasks |
| | | There are no completed tasks in the todolist | string with error message |
| | | | |
| ArrayList<String> getUnCompletedTasks() | | There are uncompleted tasks in the todolist | List with all uncompleted tasks |
| | | There are no uncompleted tasks in the todolist | Empty list |
| | | | |
| boolean searchTask(String name) | | There was a task with the provided name | true |
| | | There was no task with the provided name | false |
| | | | |
| boolean removeTask(String name) | | Task with the provided name is not in the todolist | false |
| | | Task with the provided name is in the todolist | true |
| | | | |
| 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 |

*Note: "error message" means something like "The list is empty".*

# Extension

## Class: TodoList

| Method | Member Variable | Scenario | Result |
|-------------------------------------------------------|-----------------------------|------------------------------------------------------------|------------------------------------|
| boolean add(String name) | HashMap<String, Task> tasks | Task with the provided name is not already in the todolist | true |
| | | Task with the provided name is already in the todolist | false |
| | | | |
| String listTasks() | | There are tasks in the todolist | string with all tasks |
| | | There are no tasks in the todolist | string with error message |
| | | | |
| boolean updateTaskStatus(String name, Boolean status) | | There is a task with the provided name | true |
| | | There is no task with the provided name | false |
| | | | |
| ArrayList<String> getCompletedTasks() | | There are completed tasks in the todolist | string with all completed tasks |
| | | There are no completed tasks in the todolist | string with error message |
| | | | |
| ArrayList<String> getUnCompletedTasks() | | There are uncompleted tasks in the todolist | List with all uncompleted tasks |
| | | There are no uncompleted tasks in the todolist | Empty list |
| | | | |
| boolean searchTask(String name) | | There was a task with the provided name | true |
| | | There was no task with the provided name | false |
| | | | |
| boolean removeTask(String name) | | Task with the provided name is not in the todolist | false |
| | | Task with the provided name is in the todolist | true |
| | | | |
| 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 |
| | | | |
| getTaskById(int id) | | Task with the given ID exists | return task object |
| | | Task with the given ID does not exist | return null |
| updateTaskName(int id, String newName) | | Task with ID exists/does not exist | return true/false |
| updateTaskStatusById(int id, Boolean status) | | Task with ID exists/does not exist | return true/false |
| getTaskId(String name) | | Task with name exists/does not exist | Return int/return -1 |

## Task

| Method | Member Variable | Scenario | Result |
|--------|-------------------|----------|--------|
| | int id | | |
| | boolean completed | | |
| | String name | | |
103 changes: 103 additions & 0 deletions src/main/java/com/booleanuk/core/TodoList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,108 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TodoList {
private Map<String, Boolean> todo = new HashMap<>();
// <name, status>

public boolean add(String name) {
if (this.todo.containsKey(name)) {
return false;
}
this.todo.put(name, false);
return true;
}

public String listTasks() {
if (todo.isEmpty())
return "There is nothing to do!";

StringBuilder sb = new StringBuilder();
for (String s : todo.keySet()) {
sb.append(s)
.append(": ")
.append(todo.get(s) ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public boolean updateTaskStatus(String name, boolean updatedStatus) {
if (!todo.containsKey(name))
return false;
todo.replace(name, updatedStatus);
return true;
}

public boolean getTaskStatus(String name) {
if (!todo.containsKey(name))
return false;
return todo.get(name);
}

public boolean searchTask(String name) {
return todo.containsKey(name);
}

public boolean removeTask(String name) {
if (!todo.containsKey(name)) {
return false;
}
todo.remove(name);
return true;
}

public String taskDescending() {
if (todo.isEmpty())
return "There is nothing to do!";

List<String> sortedKeys = todo.keySet().stream().sorted().toList();
StringBuilder sb = new StringBuilder();
for (String s : sortedKeys) {
sb.append(s)
.append(": ")
.append(todo.get(s) ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public String taskAscending() {
if (todo.isEmpty())
return "There is nothing to do!";

List<String> sortedKeys = todo.keySet().stream().sorted().toList().reversed();
StringBuilder sb = new StringBuilder();
for (String s : sortedKeys) {
sb.append(s)
.append(": ")
.append(todo.get(s) ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public ArrayList<String> getCompletedTasks() {
ArrayList<String> tasks = new ArrayList<>();
for (String k : todo.keySet()) {
if (todo.get(k)) {
tasks.add(k);
}
}
return tasks;
}

public ArrayList<String> getUncompletedTasks() {
ArrayList<String> tasks = new ArrayList<>();
for (String k : todo.keySet()) {
if (!todo.get(k)) {
tasks.add(k);
}
}
return tasks;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/booleanuk/extension/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.booleanuk.extension;

public class Task {
private static int nextId = 0;
private int id;
private boolean completed;
private String name;

public Task(String name) {
completed = false;
this.id = nextId;
nextId++;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public boolean isCompleted() {
return completed;
}

public void setCompleted(boolean completed) {
this.completed = completed;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
151 changes: 151 additions & 0 deletions src/main/java/com/booleanuk/extension/TodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.booleanuk.extension;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TodoList {
private Map<String, Task> todo = new HashMap<>();
// <name, status>

public boolean add(String name) {
if (this.todo.containsKey(name)) {
return false;
}
this.todo.put(name, new Task(name));
return true;
}

public String listTasks() {
if (todo.isEmpty())
return "There is nothing to do!";

StringBuilder sb = new StringBuilder();
for (String s : todo.keySet()) {
sb.append(s)
.append(": ")
.append(todo.get(s).isCompleted() ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public boolean updateTaskStatus(String name, boolean updatedStatus) {
if (!todo.containsKey(name))
return false;
todo.get(name).setCompleted(updatedStatus);
return true;
}

public boolean getTaskStatus(String name) {
if (!todo.containsKey(name))
return false;
return todo.get(name).isCompleted();
}

public boolean searchTask(String name) {
return todo.containsKey(name);
}

public boolean removeTask(String name) {
if (!todo.containsKey(name)) {
return false;
}
todo.remove(name);
return true;
}

public String taskDescending() {
if (todo.isEmpty())
return "There is nothing to do!";

List<String> sortedKeys = todo.keySet().stream().sorted().toList();
StringBuilder sb = new StringBuilder();
for (String s : sortedKeys) {
sb.append(s)
.append(": ")
.append(todo.get(s).isCompleted() ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public String taskAscending() {
if (todo.isEmpty())
return "There is nothing to do!";

List<String> sortedKeys = todo.keySet().stream().sorted().toList().reversed();
StringBuilder sb = new StringBuilder();
for (String s : sortedKeys) {
sb.append(s)
.append(": ")
.append(todo.get(s).isCompleted() ? "completed" : "uncompleted")
.append("\n");
}
return sb.toString();
}

public ArrayList<String> getCompletedTasks() {
ArrayList<String> tasks = new ArrayList<>();
for (String k : todo.keySet()) {
if (todo.get(k).isCompleted()) {
tasks.add(k);
}
}
return tasks;
}

public ArrayList<String> getUncompletedTasks() {
ArrayList<String> tasks = new ArrayList<>();
for (String k : todo.keySet()) {
if (!todo.get(k).isCompleted()) {
tasks.add(k);
}
}
return tasks;
}

public int getTaskId(String name) {
if (!todo.containsKey(name)) {
return -1;
}
return todo.get(name).getId();
}

public Task getTaskById(int id) {
for (String name : todo.keySet()) {
if (todo.get(name).getId() == id) {
return todo.get(name);
}
}
return null;
}

public boolean updateTaskName(int id, String newName) {
Task task = null;
boolean found = false;
for (String name : todo.keySet()) {
if (todo.get(name).getId() == id) {
task = todo.get(name);
task.setName(newName);
found = true;
}
}
if (found) {
todo.remove(task.getName());
todo.put(newName, task);
}
return found;
}

public boolean updateTaskStatusById(int id, boolean newStatus) {
for (String name : todo.keySet()) {
if (todo.get(name).getId() == id) {
todo.get(name).setCompleted(newStatus);
return true;
}
}
return false;
}
}
Loading
Loading