In: Computer Science
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
CODE
import java.util.ArrayList;
class Task {
private String message;
private boolean done;
public Task(String message, boolean done) {
super();
this.message = message;
this.done = done;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
}
class ToDoList {
private ArrayList<Task> tasks;
public ToDoList() {
this.tasks = new ArrayList<>();
}
public void addTask(String message) {
Task t = new Task(message, false);
tasks.add(t);
}
public void markDone(String message) {
for(Task t : tasks) {
if (message.equalsIgnoreCase(t.getMessage())) {
t.setDone(true);
}
}
}
public void displayPendingTasks() {
for (Task t : tasks) {
if (!t.isDone()) {
System.out.println(t.getMessage());
}
}
}
public void displayCompletedTasks() {
for (Task t : tasks) {
if (t.isDone()) {
System.out.println(t.getMessage());
}
}
}
}
public class Main {
public static void main(String[] args) {
ToDoList list = new ToDoList();
list.addTask("Laundry");
list.addTask("Pick up groceries");
list.markDone("laundry");
System.out.println("List of pending tasks: ");
list.displayPendingTasks();
System.out.println("List of completed tasks: ");
list.displayCompletedTasks();
}
}