-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskList.java
61 lines (52 loc) · 1.89 KB
/
TaskList.java
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
56
57
58
59
60
61
package com.project.taskmanager;
import java.util.ArrayList;
/**
* Responsible for keeping the in-memory task list. This class will use an ArrayList inside it.
*/
class TaskList {
private ArrayList<Task> tasks;
TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}
TaskList() {
tasks = new ArrayList<>();
}
int size() {
return tasks.size();
}
void add(Task task) {
tasks.add(task);
}
Task get(int index) {
return tasks.get(index);
}
void changeDone(String filepath, String line, UI ui, TaskList tasks, Database database) {
line = line.substring("done".length()).trim();
try {
if (!line.isEmpty()) {
markAsDone(filepath, line, ui, tasks, database);
} else {
throw new TaskManagerException(ui.colorRed("Please type in the" +
" 'done <task index number>' format."));
}
} catch (TaskManagerException e) {
ui.printError(e.getMessage());
}
}
private void markAsDone(String filepath, String line, UI ui, TaskList tasks, Database database) {
try {
if (tasks.size() != 0) {
int index = Integer.parseInt(line);
tasks.get(index - 1).setDone(true);
database.writeAsDone(index);
ui.markedAsDone(index, tasks);
ui.showTotal(tasks);
} else System.out.println(ui.colorRed("There are no tasks to mark as done."));
} catch (NumberFormatException e) {
System.out.println(ui.colorRed("Please type a number for the index."));
} catch (IndexOutOfBoundsException e) {
System.out.println(ui.colorRed("Please type an index number from 1 to "
+ tasks.size() + "."));
}
}
}