-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoList.java
More file actions
55 lines (46 loc) · 1.34 KB
/
ToDoList.java
File metadata and controls
55 lines (46 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// ToDoList.java
import java.util.ArrayList;
public class ToDoList {
private final ArrayList<String> todos = new ArrayList<>();
public boolean add(String task) {
if (task == null) return false;
task = task.trim();
if (task.isEmpty()) return false;
todos.add(task);
return true;
}
public boolean isEmpty() {
return todos.isEmpty();
}
public void print() {
if (todos.isEmpty()) {
System.out.println("No tasks.");
return;
}
System.out.println("\nTasks:");
for (int i = 0; i < todos.size(); i++) {
System.out.println((i + 1) + ") " + todos.get(i));
}
}
public boolean remove(int oneBasedIndex) {
int idx = oneBasedIndex - 1;
if (idx < 0 || idx >= todos.size()) return false;
todos.remove(idx);
return true;
}
public boolean markDone(int oneBasedIndex) {
int idx = oneBasedIndex - 1;
if (idx < 0 || idx >= todos.size()) return false;
String t = todos.get(idx);
if (t.startsWith("[x] ")) return true;
if (t.startsWith("[ ] ")) {
todos.set(idx, "[x] " + t.substring(4));
} else {
todos.set(idx, "[x] " + t);
}
return true;
}
public void clear() {
todos.clear();
}
}