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
83 changes: 83 additions & 0 deletions src/main/java/com/booleanuk/core/TodoList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
package com.booleanuk.core;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class TodoList {
List<Task> todoList;

public TodoList(){
this.todoList = new ArrayList<>();
}

public static class Task {
String task;
String status;

public Task(String task, String status){
this.task = task;
this.status = status;
}
}

public boolean addTask(String task){
if (task.isEmpty() || !task.matches("[a-zA-Z]+"))
return false;
else {
todoList.add(new Task(task, "incomplete"));
return true;
}
}

public List<Task> seeTodoList(){
return (todoList.isEmpty()) ? null : todoList;
}

public Task setStatus(String task, String status){

List<Task> targets = todoList.stream().filter(it -> it.task.equals(task)).toList();
Task updatedTask = new Task(task, status);
if (targets.isEmpty())
return null;
else {
this.todoList.set(todoList.indexOf(targets.getFirst()), updatedTask);
return updatedTask;
}
}

public List<Task> getCompleteTasks() {
List<Task> completedTasks = todoList.stream().filter(it -> it.status.equals("complete")).toList();
return completedTasks.isEmpty() ? null : completedTasks;
}

public List<Task> getIncompleteTasks() {
List<Task> completedTasks = todoList.stream().filter(it -> it.status.equals("incomplete")).toList();
return completedTasks.isEmpty() ? null : completedTasks;
}

public Task searchForTask(String task) {
List<Task> targets = todoList.stream().filter(it -> it.task.equals(task)).toList();
return (targets.isEmpty()) ? null : targets.getFirst();
}

public boolean removeTask(String task) {
List<Task> targets = todoList.stream().filter(it -> it.task.equals(task)).toList();
if (targets.isEmpty())
return false;
else {
todoList.remove(targets.getFirst());
return true;
}
}

public List<Task> seeTasksInAscendingAndAlphabeticalOrder() {
List<Task> res = new ArrayList<>(todoList);
res.sort(Comparator.comparing(s -> s.task));
return res.isEmpty() ? null : res;
}

public List<Task> seeTasksInDescendingAndAlphabeticalOrder() {
List<Task> res = new ArrayList<>(todoList);
res.sort((s1, s2) -> s2.task.compareTo(s1.task));
return res.isEmpty() ? null : res;
}


}
84 changes: 84 additions & 0 deletions src/main/java/com/booleanuk/core/domain-models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@







1. Add tasks to my todo list.

| Classes | Methods | Scenario | Outputs |
|------------|--------------------------------|----------------------------|---------|
| `TodoList` | `boolean addTask(String task)` | If task is null or invalid | false |
| | | If task is provided | true |


2. See all the tasks in my todo list.


| Classes | Methods | Scenario | Outputs |
|------------|----------------------------|----------------------|------------|
| `TodoList` | `List<Task> seeTodoList()` | If list is empty | null |
| | | If list is not empty | List<Task> |


3. Change the status of a task between incomplete and complete.

| Classes | Methods | Scenario | Outputs |
|------------|----------------------------------------------|------------------------------|---------|
| `TodoList` | `Task setStatus(String task, String status)` | If status or task is invalid | null |
| | | If status and task is valid | Task |


4. Get only the complete tasks.


| Classes | Methods | Scenario | Outputs |
|------------|---------------------------------|------------------------------|------------|
| `TodoList` | `List<Task> getCompleteTasks()` | If no tasks are complete | null |
| | | If there are completed tasks | List<Task> |


5. Get only the incomplete tasks.


| Classes | Methods | Scenario | Outputs |
|------------|-----------------------------------|-------------------------------|------------|
| `TodoList` | `List<Task> getIncompleteTasks()` | If no tasks are incomplete | null |
| | | If there are incomplete tasks | List<Task> |


6. Search for a task and receive a message that says it wasn't found if it doesn't exist.


| Classes | Methods | Scenario | Outputs |
|------------|-----------------------------------|-----------------------|-------------------------------------------|
| `TodoList` | `Task searchForTask(String task)` | If task doesn't exist | print "not found" message and return null |
| | | If task exists | return task |


7. Remove tasks from my list.


| Classes | Methods | Scenario | Outputs |
|------------|-----------------------------------|-----------------------|------------------------|
| `TodoList` | `boolean removeTask(String task)` | If task doesn't exist | false |
| | | If task does exist | remove and return true |


8. See all the tasks in my list ordered alphabetically in ascending order.


| Classes | Methods | Scenario | Outputs |
|------------|-------------------------------------------------------|---------------------------|------------|
| `TodoList` | `List<Task> seeTasksInAscendingAndAlpabeticalOrder()` | If list of tasks is empty | null |
| | | If list is not empty | List<Task> |


9. See all the tasks in my list ordered alphabetically in descending order.


| Classes | Methods | Scenario | Outputs |
|------------|---------------------------------------------------------|---------------------------|------------|
| `TodoList` | `List<Task> seeTasksInDescendingAndAlphabeticalOrder()` | If list of tasks is empty | null |
| | | If list is not empty | List<Task> |
64 changes: 64 additions & 0 deletions src/main/java/com/booleanuk/extension/TodoListExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.booleanuk.extension;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class TodoListExtension {
List<TaskExt> todoList;

public TodoListExtension() {
this.todoList = new ArrayList<>();
}

public static class TaskExt {
String task;
String status;
int id;
LocalDateTime timeCreated;

public TaskExt(String task, int id) {
this.task = task;
this.status = "incomplete";
this.id = id;
this.timeCreated = LocalDateTime.now();
}
}

public void addTaskExt(String task, int id){
todoList.add(new TaskExt(task, id));
}

public TaskExt getTaskByUniqueId(int id){
List<TaskExt> res = todoList.stream().filter(it -> it.id == id).toList();
return res.isEmpty() ? null : res.getFirst();
}

public TaskExt updateNameBasedOnId(int id, String task){
List<TaskExt> targets = todoList.stream().filter(it -> it.id == id).toList();
TaskExt updatedTask = new TaskExt(task, id);
if (targets.isEmpty())
return null;
else {
this.todoList.set(todoList.indexOf(targets.getFirst()), updatedTask);
return updatedTask;
}
}

public TaskExt updateStatusBasedOnId(int id, String newStatus) {
List<TaskExt> targets = todoList.stream().filter(it -> it.id == id).toList();
TaskExt updatedTask = new TaskExt(targets.getFirst().task, id);
updatedTask.status = newStatus;
if (targets.isEmpty())
return null;
else {
this.todoList.set(todoList.indexOf(targets.getFirst()), updatedTask);
return updatedTask;
}
}

public LocalDateTime whenTaskCreated(int id) {
List<TaskExt> targets = todoList.stream().filter(it -> it.id == id).toList();
return targets.isEmpty() ? null : targets.getFirst().timeCreated;
}
}
Loading
Loading