-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.java
More file actions
233 lines (203 loc) · 7.62 KB
/
Copy pathTaskManager.java
File metadata and controls
233 lines (203 loc) · 7.62 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class TaskManager {
private ArrayList<Task> tasks;
private int nextId;
//Constructor era
public TaskManager () {
tasks = new ArrayList<>(); //it creates new ArrayList
nextId = 1;
}
// Now here we start the Method Part which help us to do certain tasks :)
//CREATE: Adding new task with it:
public void addTask(String title, String description, String priority) {
Task newTask = new Task(nextId++, title, description, priority);
tasks.add(newTask);
System.out.println("Task added successfully! ( ID: " + newTask.getID() + " )");
}
//READ: with this we can view all Tasks :)
public void viewAllTasks() {
if (tasks.isEmpty()) {
System.out.println("No Tasks Available. -_- ");
}
System.out.println("\n======= ALL TASKS =======");
for (Task task : tasks) { //enhanced for loop
System.out.println(task);
System.out.println("----------------------------");
}
System.out.println("Total tasks: " + tasks.size()); //one of the inbuilt method of ArrayList same as isEmpty
}
//READ: view single task by it's ID
public void viewTask(int id) {
Task task = findTaskById(id);
if (task != null) {
System.out.println("\n" + task);
}
else {
System.out.println("Task with ID " + id + " not found.");
}
}
//Helper method for Find task by Id:
private Task findTaskById(int id) {
for(Task task : tasks) {
if (task.getID() == id) {
return task;
}
}
return null;
}
//UPDATE: here we can Modify our Tasks Details
public void updateTask(int id, String newTitle, String newDescription, String newPriority) {
Task task = findTaskById(id);
if (task != null) {
task.setTitle(newTitle);
task.setDescription(newDescription);
task.setPriority(newPriority);
System.out.println("Task updated Successfuly!!!");
}
else {
System.out.println("Tasl with ID " + id + " not found.");
}
}
//UPDATE: with this we update the status of our task ( completed or not)
public void markComplete(int id) {
Task task = findTaskById(id);
if (task != null) {
task.setCompleted(true);
System.out.println("Task marked as Complete!!!");
}
else {
System.out.println("Task with ID " + id + " not found");
}
}
//UPDATE: here we use this method to UnMark the completed task
public void unmarkComplete(int id) {
Task task = findTaskById(id);
if (task != null) {
//it checks if task is actully completed or not ??
if (!task.isCompleted()) {
System.out.println("Task is arleady Pending ( Not completed yet )");
return;
}
//it will help to Unmark the task ( set back to pending )
task.setCompleted(false);
System.out.println("Task Unmarked ! Status changed back to PENDING.");
}
else {
System.out.println("Task with ID " + id + " not found.");
}
}
//DELETE: here we can Remove the task
public void deleteTask(int id) {
Task task = findTaskById(id);
if (task != null) {
tasks.remove(task);
System.out.println("Task Deleted successfuly ");
}
else {
System.out.println("Task with ID " + id + " not found");
}
}
// SEARCH: Find tasks by Priority
public void searchByPriority(String priority) {
ArrayList<Task> results = new ArrayList<>();
String searchPriority = priority.toUpperCase();
for (Task task : tasks) {
if (task.getPriority().equals(searchPriority)) {
results.add(task);
}
}
if (results.isEmpty()) {
System.out.println("No tasks found with priority");
}
else {
System.out.println("\n======= TASKS WITH PRIORITY: " + priority + " =======");
for (Task task : results) {
System.out.println(task);
System.out.println("------------------------------");
}
System.out.println("Total found: " + results.size());
}
}
// Filter: show only pendingg tasks
public void viewPendingTasks() {
ArrayList<Task> pending = new ArrayList<>();
for (Task task : tasks) {
if (!task.isCompleted()) {
pending.add(task);
}
}
if (pending.isEmpty()) {
System.out.println("No pending tasks here");
}
else {
//sorting our task by priority ( here I googled it hehhehe, nvr thought it could be done via here -_- )
Collections.sort(pending, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
int priority1 = getPriorityValue(t1.getPriority());
int priority2 = getPriorityValue(t2.getPriority());
return Integer.compare(priority2, priority1);
}
});
System.out.println("\n======= PENDING TASKS =======");
for (Task task : pending) {
System.out.println(task);
System.out.println("------------------------------");
}
System.out.println("Total pending: " + pending.size());
}
}
//SORT: sorting task by priority ( HIGH -> MEDIUM -> LOW )
//first of all we have to made a helper method for our sorting method ( we can't ruin our mind again as we F****dup with the findTaskById )
private int getPriorityValue (String priority) {
switch (priority) {
case "HIGH":
return 3;
case "MEDIUM":
return 2;
case "LOW":
return 1;
default:
return 0;
}
}
//Now our sorting method arrived here
public void sortByPriority() {
Collections.sort(tasks, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
int priority1 = getPriorityValue(t1.getPriority());
int priority2 = getPriorityValue(t2.getPriority());
return Integer.compare(priority2, priority1); // using Descending order
}
});
System.out.println("Tasks sorted by priority!");
viewPendingTasks();
}
//STATS: it'll help us to Show Statics of our tasks
public void showStats() {
int completed = 0;
int high = 0, medium = 0, low = 0;
for ( Task task : tasks) {
if (task.isCompleted()) completed++;
switch (task.getPriority()) {
case "HIGH": high++;
break;
case "MEDIUM": medium++;
break;
case "LOW": low++;
break;
}
}
System.out.println("\n========== TASK STATISTICS ==========");
System.out.println("Total Tasks: " + tasks.size());
System.out.println("Completed: " + completed);
System.out.println("Pending: " + (tasks.size() - completed));
System.out.println("\nPriority Breakdown:");
System.out.println(" HIGH: " + high);
System.out.println(" MEDIUM: " + medium);
System.out.println(" LOW: " + low);
}
}