-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskList.java
More file actions
84 lines (71 loc) · 2.11 KB
/
Copy pathTaskList.java
File metadata and controls
84 lines (71 loc) · 2.11 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package linqing.tojava;
import java.io.IOException;
import java.util.*;
/**
* TaskList class that is responsible for keeping the in-memory tasks list
*/
public class TaskList {
private static List<Task> tasks = new ArrayList<>();
public TaskList() {
}
/**
* @param description for the task detail
* @throws TaskManagerException if description is empty
*/
public TaskList(List<Task> description) throws TaskManagerException {
if (description.isEmpty()) {
throw new TaskManagerException("Current task list is empty, please try again");
}
tasks = description;
}
private static void showTotalTask() {
System.out.println("Tasks in the list: " + tasks.size());
}
/**
* @param t is to add a new task
*/
public void addTask(Task t) {
tasks.add(t);
showTotalTask();
}
/**
* remove the task base on number of index
*
* @param i is the integer number for each task
*/
public void removeTask(int i) {
tasks.remove(i - 1);
showTotalTask();
}
/**
* @param line for the done description
* @throws TaskManagerException if index<1 or index >tasks.size()
*/
public void markAsDone(String line) throws TaskManagerException {
{
int index = Integer.parseInt(line.substring("done".length()).trim());
if (index < 1 || index > tasks.size()) // if attempting to set done for non-existing task
throw new TaskManagerException("Error: Invalid Task number for DONE");
else {
tasks.get(index - 1).setDone(true);
showTotalTask();
}
}
}
/**
* return print and the number of task
*/
public void printTasks() {
System.out.println("Tasks:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println("[" + (i + 1) + "] " + tasks.get(i));
}
}
public void save() {
try {
Storage.save(tasks);
} catch ( IOException e ) {
e.printStackTrace();
}
}
}