Our teacher gave us a new task and somehow I am not able to figure out the problem.
There are 3 different java classes and I am only allowed to change the ToDoList class. There, I want to add a new List so that the main class is able to add new Items to my todo list. As you can see below, I tried to initialize a new list but that did not work.
Where is my mistake?
public class ToDoListEntry {
String task;
LocalDate date;
ToDoListEntry next;
public ToDoListEntry(LocalDate date, String task) {
this.task = task;
this.date = date;
}
}
Then comes the next where I tried to add an array but which did not work:
public class ToDoList {
ToDoListEntry first;
public ArrayList<ToDoListEntry> todolist;
public ToDoList (){
todolist = new ArrayList<ToDoListEntry>();
}
public void add(ToDoListEntry newTask) {
todolist.add(newTask);
}
public String print() {
String result = "";
if (first == null) {
result = "Empty list!\n";
} else {
ToDoListEntry pointer = first;
while (pointer != null) {
result += "Until " + pointer.date + " Task: "
+ pointer.task +"\n";
pointer = pointer.next;
}
}
System.out.println(result);
return result;
}
}
And in the end, the main class should supposed to create a new ToDo List and print it out (Note that I did not include the print() Method):
public static void main(String[] args) {
System.out.println("Test 00: Empty List");
ToDoList list2016 = new ToDoList();
list2016.print();
System.out.println("Test 01: add");
list2016.add(new ToDoListEntry(LocalDate.of(2016, 8, 15), "Do workout"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 6, 3), "Buy apples"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Books"));
list2016.print();
next, of type ToDoListEntry, I think you're not supposed to use an ArrayList, but to chain entries to each other (i.e. understand the principle of a linked list).