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
Binary file added domain-model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/main/java/com/booleanuk/core/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.booleanuk.core;

public class Task {
private boolean completed = false;
private String name;

public Task(String name) {
this.name = name;
}

void complete() {
completed = true;
}

boolean isComplete() {
return completed;
}

String getName() {
return name;
}
}
80 changes: 80 additions & 0 deletions src/main/java/com/booleanuk/core/TodoList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,85 @@
package com.booleanuk.core;

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

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

ArrayList<Task> addTask(Task t) {
tasks.add(t);

Choose a reason for hiding this comment

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

I would check for duplicates before adding a task

return tasks;
}

void completeTask(String taskName) {
for (Task t : tasks) {
if (t.getName().equals(taskName)) {
t.complete();
}
}
}

ArrayList<Task> getAll() {
return tasks;
}

ArrayList<Task> getCompleted() {
ArrayList<Task> completed = new ArrayList<>();

for (Task t : tasks) {
if (t.isComplete())
completed.add(t);
}

return completed;
}

ArrayList<Task> getIncompleted() {
ArrayList<Task> incompleted = new ArrayList<>();

for (Task t : tasks) {
if (!t.isComplete())
incompleted.add(t);
}

return incompleted;
}

ArrayList<Task> getAllAscending() {
Collections.sort(tasks, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
return t1.getName().compareToIgnoreCase(t2.getName());
}
});
return tasks;
}

ArrayList<Task> getAllDescending() {
Collections.sort(tasks, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
return t2.getName().compareToIgnoreCase(t1.getName());
}
});
return tasks;
}

boolean search(String taskName) {
for (Task t : tasks) {
if (t.getName().equals(taskName))
return true;
}
return false;
}

Task removeTask(String taskName) {
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i).getName().equals(taskName)) {
return tasks.remove(i);
}
}
return null;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/booleanuk/extension/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.booleanuk.extension;

import java.time.LocalDateTime;

public class Task {
private boolean completed = false;
private String name;
private int id = -1;
private LocalDateTime timeCreated = LocalDateTime.now();

public Task(String name) {
this.name = name;
}

public Task(String name, int id) {
this.name = name;
this.id = id;

Choose a reason for hiding this comment

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

Generally., IDs shou;ld be autogenerated in a Model to ensure consistency

}


void complete() {
completed = true;
}

boolean isComplete() {
return completed;
}

String getName() {
return name;
}

int getId() {
return id;
}

void changeName(String newName) {
name = newName;
}

void setStatus(boolean completed) {
this.completed = completed;
}

LocalDateTime getTimeCreated() {
return timeCreated;
}

void setId(int id) {
this.id = id;
}
}
114 changes: 114 additions & 0 deletions src/main/java/com/booleanuk/extension/TodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.booleanuk.extension;

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

public class TodoList {
private ArrayList<Task> tasks = new ArrayList<>();
private int idCounter = 1; // gives unique ids to tasks

ArrayList<Task> addTask(Task t) {
t.setId(idCounter++);
tasks.add(t);
return tasks;
}

void completeTask(String taskName) {
for (Task t : tasks) {
if (t.getName().equals(taskName)) {
t.complete();
}
}
}

ArrayList<Task> getAll() {
return tasks;
}

ArrayList<Task> getCompleted() {
ArrayList<Task> completed = new ArrayList<>();

for (Task t : tasks) {
if (t.isComplete())
completed.add(t);
}

return completed;
}

ArrayList<Task> getIncompleted() {
ArrayList<Task> incompleted = new ArrayList<>();

for (Task t : tasks) {
if (!t.isComplete())
incompleted.add(t);
}

return incompleted;
}

ArrayList<Task> getAllAscending() {
ArrayList<Task> ascending = new ArrayList<>(tasks);
Collections.sort(ascending, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
return t1.getName().compareToIgnoreCase(t2.getName());
}
});
return ascending;
}

ArrayList<Task> getAllDescending() {
ArrayList<Task> descending = getAllAscending();
Collections.reverse(descending);
return descending;
}

boolean search(String taskName) {
for (Task t : tasks) {
if (t.getName().equals(taskName))
return true;
}
return false;

Choose a reason for hiding this comment

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

Good work, but the requirement here was "I want to search for a task and receive a message that says it wasn't found
* if it doesn't exist."

}

Task removeTask(String taskName) {
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i).getName().equals(taskName)) {
return tasks.remove(i);
}
}
return null;
}

Task getTaskId(int id) {
for (Task t : tasks) {
if (t.getId() == id)
return t;
}
return null;
}

Task changeTaskName(int id, String newName) {
Task t = getTaskId(id);

if (t != null) {
t.changeName(newName);
return t;
}

return null;
}

Task setTaskStatus(boolean completed, int id) {
Task t = getTaskId(id);

if (t != null) {
t.setStatus(completed);
return t;
}

return null;
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/booleanuk/core/TaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.booleanuk.core;

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

public class TaskTest {
@Test
void testComplete() {
Task task = new Task("task1");

Assertions.assertFalse(task.isComplete());

task.complete();

Assertions.assertTrue(task.isComplete());
}

@Test
void testGetName() {
Task task = new Task("task1");

Assertions.assertEquals("task1", task.getName());
}
}
Loading
Loading