-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
153 lines (141 loc) · 4.55 KB
/
Copy pathTaskScheduler.java
File metadata and controls
153 lines (141 loc) · 4.55 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
import java.util.Date;
class TaskScheduler {
private class Node {
int taskId;
String taskName;
int priority;
Date dueDate;
Node next;
Node(int taskId, String taskName, int priority, Date dueDate) {
this.taskId = taskId;
this.taskName = taskName;
this.priority = priority;
this.dueDate = dueDate;
this.next = null;
}
}
private Node head = null;
private Node tail = null;
private Node currentTask = null;
public void addTaskAtEnd(int taskId, String taskName, int priority, Date dueDate) {
Node newNode = new Node(taskId, taskName, priority, dueDate);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
public void addTaskAtBeginning(int taskId, String taskName, int priority, Date dueDate) {
Node newNode = new Node(taskId, taskName, priority, dueDate);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
} else {
newNode.next = head;
head = newNode;
tail.next = head;
}
}
public void addTaskAtPosition(int taskId, String taskName, int priority, Date dueDate, int position) {
if (position <= 1) {
addTaskAtBeginning(taskId, taskName, priority, dueDate);
return;
}
Node newNode = new Node(taskId, taskName, priority, dueDate);
Node temp = head;
for (int i = 1; i < position - 1 && temp.next != head; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
if (temp == tail) {
tail = newNode;
}
}
public void removeTaskById(int taskId) {
if (head == null) return;
if (head.taskId == taskId) {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
tail.next = head;
}
return;
}
Node temp = head;
do {
if (temp.next.taskId == taskId) {
temp.next = temp.next.next;
if (temp.next == head) {
tail = temp;
}
return;
}
temp = temp.next;
} while (temp != head);
}
public void viewCurrentTask() {
if (currentTask == null) {
currentTask = head;
}
if (currentTask != null) {
System.out.println("Current Task: " + currentTask.taskName + ", Priority: " + currentTask.priority);
currentTask = currentTask.next;
}
}
public void displayAllTasks() {
if (head == null) {
System.out.println("No tasks available.");
return;
}
Node temp = head;
do {
System.out.println("Task ID: " + temp.taskId + ", Name: " + temp.taskName + ", Priority: " + temp.priority);
temp = temp.next;
} while (temp != head);
}
public void searchByPriority(int priority) {
if (head == null) {
System.out.println("No tasks available.");
return;
}
Node temp = head;
boolean found = false;
do {
if (temp.priority == priority) {
System.out.println("Task Found - ID: " + temp.taskId + ", Name: " + temp.taskName);
found = true;
}
temp = temp.next;
} while (temp != head);
if (!found) {
System.out.println("No task with priority " + priority + " found.");
}
}
public static void main(String[] args) {
TaskScheduler scheduler = new TaskScheduler();
scheduler.addTaskAtEnd(1, "Task A", 3, new Date());
scheduler.addTaskAtEnd(2, "Task B", 2, new Date());
scheduler.addTaskAtBeginning(3, "Task C", 1, new Date());
scheduler.displayAllTasks();
scheduler.viewCurrentTask();
scheduler.searchByPriority(2);
scheduler.removeTaskById(2);
scheduler.displayAllTasks();
}
}
//SampleOutput
//Task ID: 3, Name: Task C, Priority: 1
//Task ID: 1, Name: Task A, Priority: 3
//Task ID: 2, Name: Task B, Priority: 2
//Current Task: Task C, Priority: 1
//Task Found - ID: 2, Name: Task B
//Task ID: 3, Name: Task C, Priority: 1
//Task ID: 1, Name: Task A, Priority: 3