-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskList.java
180 lines (157 loc) · 6.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.util.*;
public class TaskList<E>{
/*
* Eduardo Hernandez
* CS 284 Section B
* I pledge my Honor that I have abided by the Stevens Honor System.
*/
//fields declaration
private ListQueue<E> all;
private ListQueue<E> completed;
private ListQueue<E> active;
private int LOW_PRIORITY = Integer.MAX_VALUE;
private int HIGH_PRIORITY = 1;
// initializes all the ListQueues in the attributes
public TaskList(){
all = new ListQueue<>();
completed = new ListQueue<>();
active = new ListQueue<>();
}
//will add the item into active and all queues with default priority as LOW_PRIORITY.
public boolean createTask(E item) {
if (item == null) {
return false;
}
all.addRear(item);
active.addRear(item);
System.out.println("Successfully entered the task to the to-do list!");
return true;
}
//will add the item into active and all queues. If item is null, it will return false. Otherwise returns true.
public boolean createTask(E item, int priority){
if(item == null){
return false;
}
all.offer(item, priority);
active.offer(item, priority);
System.out.println("Successfully entered the task to the to-do list!");
return true;
}
//getter
public ListQueue<E> getAll(){
return all;
}
//getter
public ListQueue<E> getCompleted(){
return completed;
}
//getter
public ListQueue<E> getActive(){
return active;
}
//will print the top three tasks in the queue. If there are less than 3, it will print them all.
public void printTopThreeTasks(){
System.out.println("Top 3 highest priority tasks:\n" +
"------------------------------\n" +
"Printing Top Three Tasks...");
ListQueue.Node<E> front1 = active.getFront();
int taskNum = 1;
while(front1 != null && taskNum <= 3){
System.out.println(taskNum + ". " + front1.getData());
front1 = front1.getNext();
taskNum++;
}
}
// helper method will use the iterator() to iterate through the queue elements and print them with numbers
private void printTasks(ListQueue<E> queue){
Iterator<E> iterate = queue.iterator();
int taskNum = 1;
while(iterate.hasNext()){
System.out.println(taskNum + ". " + iterate.next());
taskNum++;
}
}
//shows only active tasks
public void showActiveTasks(){
System.out.println("Current TO-DO List:\n"+
"-------------------");
printTasks(active);
}
//shows all tasks in the queue
public void showAllTasks(){
System.out.println("All of the tasks - Both completed and active:\n" +
"---------------------------------------------");
printTasks(all);
}
//shows completed tasks in the queue
public void showCompletedTasks(){
System.out.println("Completed tasks:\n" +
"----------------");
printTasks(completed);
}
//removes the highest priority queue. Meaning the first element in the queue
public boolean crossOffMostUrgent(){
ListQueue.Node<E> front1 = active.getFront();
if(front1 == null){
System.out.println("Unsuccessful operation! Please try again!");
return false;
}
System.out.println("Task is completed and removed from the list: " + front1.getData());
completed.addRear(front1.getData());
active.remove(front1);
System.out.println("Successfully removed the most urgent task/top of the list task!");
return true;
}
/*
* removes a task depending on the userinput
* if the number is not found in the queue, it returns false and does not remove anything
*/
public boolean crossOffTask(int taskNumber){
ListQueue.Node<E> front1 = active.getFront();
int num = 1;
if(taskNumber > active.getSize() || taskNumber <= 0){
System.out.println("Unsuccessful operation! Please try again!");
return false;
}
while(front1 != null){
if(num == taskNumber){
System.out.println("Task number is removed: " + front1.getData());
completed.addRear(front1.getData());
active.remove(front1);
}
front1 = front1.getNext();
num++;
}
System.out.println("Succesfully removed the task number: " + taskNumber);
return true;
}
/*
* Helper for the printMenu method in TestTaskLit class.
* it will print the menu with active tasks if there are tasks in the queue
* if there are no tasks, it will print the menu with a statement saying that there are no elemtns
*/
public void secondMenu(){
if(active.getSize() == 0){
System.out.println("==> Currently there are NO items in the To-Do List");
System.out.println("To add a new task without priority information, press 1.");
System.out.println("To add a new task with a priority information, press 2.");
System.out.println("To cross off the task at the top of the list, press 3.");
System.out.println("To cross off a certain task in the list, press 4.");
System.out.println("To see the top 3 highest priority tasks, press 5.");
System.out.println("To see the completed tasks, press 6.");
System.out.println("To see the all tasks that has been completed or still active, press 7.");
System.out.println("To quit the program, press 8.");
}
else{
showActiveTasks();
System.out.println("To add a new task without priority information, press 1.");
System.out.println("To add a new task with a priority information, press 2.");
System.out.println("To cross off the task at the top of the list, press 3.");
System.out.println("To cross off a certain task in the list, press 4.");
System.out.println("To see the top 3 highest priority tasks, press 5.");
System.out.println("To see the completed tasks, press 6.");
System.out.println("To see the all tasks that has been completed or still active, press 7.");
System.out.println("To quit the program, press 8.");
}
}
}