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

public class Task {
String taskName;
boolean isCompeted;

public Task(String taskName, boolean isCompeted) {
this.taskName = taskName;
this.isCompeted = isCompeted;
}

public String getTaskName() {
return this.taskName;
}

public boolean getIsCompleted() {
return this.isCompeted;
}
}
73 changes: 73 additions & 0 deletions src/main/java/com/booleanuk/core/TodoList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.Collections;

public class TodoList {
ArrayList<Task> tasks = new ArrayList<>();

public boolean addTask(Task task) {
return tasks.add(task);
}

public void printTasks() {
System.out.println("Current tasks:");
for(Task task : this.tasks) {
System.out.println(task.taskName + " : " + task.isCompeted);
}
System.out.println();
}

public boolean changeStatus(Task task) {
if(!tasks.contains(task)) return false;
task.isCompeted = !task.isCompeted;
return true;
}

public ArrayList<Task> getCompletedTasks() {
ArrayList<Task> completedTasks = new ArrayList<>();
for(Task task : this.tasks) {
if(task.isCompeted) completedTasks.add(task);
}
return completedTasks;
}

public ArrayList<Task> getIncompleteTasks() {
ArrayList<Task> incompleteTasks = new ArrayList<>();
for(Task task : this.tasks) {
if(!task.isCompeted) incompleteTasks.add(task);
}
return incompleteTasks;
}

public Task getTask(String taskName) {
for(Task task : this.tasks) {
if(task.taskName.equals(taskName)) return task;
} return null;
}

public boolean removeTask(String taskName) {
for(Task task : this.tasks) {
if(task.taskName.equals(taskName)) {
this.tasks.remove(task);
return true;
}
}
return false;
}

public void printTasksAscending() {
ArrayList<Task> sortedTasks = new ArrayList<>(this.tasks);
sortedTasks.sort(((o1, o2) -> o1.getTaskName().compareTo(o2.getTaskName())));
System.out.println("All tasks (descending):");
for(Task task : sortedTasks) {
System.out.println(task.taskName + " : " + task.isCompeted);
}
System.out.println();
}

public void printTasksDescending() {
ArrayList<Task> sortedTasks = new ArrayList<>(this.tasks);
sortedTasks.sort(((o2, o1) -> o1.getTaskName().compareTo(o2.getTaskName())));
System.out.println("All tasks (descending):");
for(Task task : sortedTasks) {
System.out.println(task.taskName + " : " + task.isCompeted);
}
System.out.println();
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/booleanuk/core/domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Java ToDo List

| Classes | Members | Methods | Scenarios | Outputs |
|----------|-----------------------|--------------------------------------|------------------------------------------------|---------------------------------|
| Task | String taskName | String getTaskName() | Returns name | this.taskName |
| | boolean isCompleted | boolean getIsCompleted() | Returns isCompleted | this.isCompleted |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| TodoList | ArrayList<Task> tasks | boolean addTask(Task task) | Successfully adds task at the end of tasks | true |
| | | | Is unable to add the task | false |
| | | void printTasks() | prints the list of tasks to the terminal | |
| | | boolean changeStatus(Task task) | The task exists in tasks and is updated | true |
| | | | The task does not exist and can't be updated | false |
| | | ArrayList<Task> getCompletedTasks() | Filters complete tasks in a new list | ArrayList<Task> completeTasks |
| | | ArrayList<Task> getIncompleteTasks() | Filters incomplete tasks in a new list | ArrayList<Task> incompleteTasks |
| | | Task getTask(String taskName) | Task exists and the user is informed | task |
| | | | Task doesn't exist, and the user is informed | null |
| | | boolean removeTask(String taskName) | Task exists, and is successfully deleted | true |
| | | | Task doesn't exists, and thus can't be deleted | false |
| | | void printTasksAscending() | prints tasks in ascending order | |
| | | void printTasksDescending() | prints tasks in descending order | |
| | | | | |
| | | | | |
22 changes: 22 additions & 0 deletions src/main/java/com/booleanuk/extension/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.booleanuk.extension;

import java.util.UUID;

public class Task {
String taskName;
boolean isCompeted;
UUID id = UUID.randomUUID();

public Task(String taskName, boolean isCompeted) {
this.taskName = taskName;
this.isCompeted = isCompeted;
}

public String getTaskName() {
return this.taskName;
}

public boolean getIsCompleted() {
return this.isCompeted;
}
}
103 changes: 103 additions & 0 deletions src/main/java/com/booleanuk/extension/TodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.booleanuk.extension;

import java.util.ArrayList;
import java.util.UUID;

public class TodoList {
ArrayList<Task> tasks = new ArrayList<>();

public boolean addTask(Task task) {
return tasks.add(task);
}

public void printTasks() {
System.out.println("Current tasks:");
for(Task task : this.tasks) {
System.out.println(task.taskName + " : " + task.isCompeted);
}
System.out.println();
}

public boolean changeStatus(Task task) {
if(!tasks.contains(task)) return false;
task.isCompeted = !task.isCompeted;
return true;
}

public ArrayList<Task> getCompletedTasks() {
ArrayList<Task> completedTasks = new ArrayList<>();
for(Task task : this.tasks) {
if(task.isCompeted) completedTasks.add(task);
}
return completedTasks;
}

public ArrayList<Task> getIncompleteTasks() {
ArrayList<Task> incompleteTasks = new ArrayList<>();
for(Task task : this.tasks) {
if(!task.isCompeted) incompleteTasks.add(task);
}
return incompleteTasks;
}

public Task getTask(String taskName) {
for(Task task : this.tasks) {
if(task.taskName.equals(taskName)) return task;
} return null;
}


public boolean removeTask(String taskName) {
for(Task task : this.tasks) {
if(task.taskName.equals(taskName)) {
this.tasks.remove(task);
return true;
}
}
return false;
}

public void printTasksAscending() {
ArrayList<Task> sortedTasks = new ArrayList<>(this.tasks);
sortedTasks.sort(((o1, o2) -> o1.getTaskName().compareTo(o2.getTaskName())));
System.out.println("All tasks (descending):");
for(Task task : sortedTasks) {
System.out.println(task.taskName + " : " + task.isCompeted);
}
System.out.println();
}

public void printTasksDescending() {
ArrayList<Task> sortedTasks = new ArrayList<>(this.tasks);
sortedTasks.sort(((o2, o1) -> o1.getTaskName().compareTo(o2.getTaskName())));
System.out.println("All tasks (descending):");
for(Task task : sortedTasks) {
System.out.println(task.taskName + " : " + task.isCompeted);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String concatenation in a loop is not very efficient, as strings are immutable, so a new instance is created each iteration. Consder using stringbuilder instead

}
System.out.println();
}

public Task getTaskByID(UUID id) {
for(Task task : this.tasks) {
if(task.id == id) return task;
} return null;
}

public boolean updateTaskNameByID(UUID id, String newTaskName) {
for(Task task : this.tasks) {
if(task.id == id) {
task.taskName = newTaskName; // should have a setter-function
return true;
}
} return false;
}

public boolean updateTaskStatusByID(UUID id, boolean status) {
for(Task task : this.tasks) {
if(task.id == id) {
task.isCompeted = status; // should have a setter-function
return true;
}
} return false;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/booleanuk/extension/domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Java ToDo List

| Classes | Members | Methods | Scenarios | Outputs |
|----------|-----------------------|----------------------------------------------------------------|------------------------------------------------|---------------------------------|
| Task | String taskName | String getTaskName() | Returns name | this.taskName |
| | boolean isCompleted | boolean getIsCompleted() | Returns isCompleted | this.isCompleted |
| | UUID id | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| TodoList | ArrayList<Task> tasks | boolean addTask(Task task) | Successfully adds task at the end of tasks | true |
| | | | Is unable to add the task | false |
| | | void printTasks() | prints the list of tasks to the terminal | |
| | | boolean changeStatus(Task task) | The task exists in tasks and is updated | true |
| | | | The task does not exist and can't be updated | false |
| | | ArrayList<Task> getCompletedTasks() | Filters complete tasks in a new list | ArrayList<Task> completeTasks |
| | | ArrayList<Task> getIncompleteTasks() | Filters incomplete tasks in a new list | ArrayList<Task> incompleteTasks |
| | | Task getTask(String taskName) | Task exists and the user is informed | task |
| | | | Task doesn't exist, and the user is informed | null |
| | | boolean removeTask(String taskName) | Task exists, and is successfully deleted | true |
| | | | Task doesn't exists, and thus can't be deleted | false |
| | | void printTasksAscending() | prints tasks in ascending order | |
| | | void printTasksDescending() | prints tasks in descending order | |
| | | public Task getTaskByID(UUID id) | Task is found and returned | true |
| | | | Task is not found | false |
| | | public boolean updateTaskNameByID(UUID id, String newTaskName) | Task is found and name is updated | true |
| | | | Task is not found | false |
| | | public boolean updateTaskStatusByID(UUID id, boolean status) | Task is found and status is updated | true |
| | | | Task is not found | false |
| | | | | |
31 changes: 31 additions & 0 deletions src/test/java/com/booleanuk/core/TaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TaskTest {

@Test
public void getCorrectTaskNameTest() {
Task task = new Task("laundry", false);
Assertions.assertEquals("laundry", task.getTaskName());
}

@Test
public void getIncorrectTaskNameTest() {
Task task = new Task("laundry", false);
Assertions.assertNotEquals("shower", task.getTaskName());
}

@Test
public void getCorrectCompletedTest() {
Task task = new Task("laundry", false);
Assertions.assertEquals(false, task.getIsCompleted());
}

@Test
public void getIncorrectCompletedTest() {
Task task = new Task("laundry", false);
Assertions.assertNotEquals(true, task.getIsCompleted());
}
}
Loading
Loading