-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo-do-list application.cpp
More file actions
443 lines (415 loc) Β· 13.2 KB
/
To-do-list application.cpp
File metadata and controls
443 lines (415 loc) Β· 13.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct Task {
int id;
string name;
string description;
int priority; // 1 (high), 2 (medium), 3 (low)
int dueDate; // in (YYMMDD)
string progress; // "in progress" or "completed"
Task* prev;
Task* next;
Task(int id, string name, string description, int priority, int dueDate) {
this->id = id;
this->name = name;
this->description = description;
this->priority = priority;
this->dueDate = dueDate;
this->progress = "in progress"; // Default value
this->prev = nullptr;
this->next = nullptr;
}
};
struct StackNode {
Task taskData;
StackNode* next;
};
StackNode* stackTop = nullptr;
Task* head = nullptr;
Task* tail = nullptr;
string currentUsername;
void setUsername() {
cout << "Enter your username: ";
getline(cin, currentUsername);
ifstream file(currentUsername + ".txt");
if (!file.is_open()) {
ofstream newFile(currentUsername + ".txt");
newFile.close();
cout << "New file created for user \"" << currentUsername << "\".\n";
}
else {
cout << "Welcome back, \"" << currentUsername << "\".\n";
}
}
void saveToFile() {
if (currentUsername.empty()) return;
ofstream file(currentUsername + ".txt");
if (!file.is_open()) {
cout << "Error: Could not open file to save data.\n";
return;
}
Task* current = head;
while (current != nullptr) {
file << current->id << "," << current->name << "," << current->description << ","
<< current->priority << "," << current->dueDate << "," << current->progress << "\n";
current = current->next;
}
file.close();
}
void sortTask() {
if (head == nullptr || head->next == nullptr)
return;
bool swapped = true;
while (swapped) {
swapped = false;
Task* current = head;
while (current->next) {
if (current->priority > current->next->priority) {
int tempPriority = current->priority;
current->priority = current->next->priority;
current->next->priority = tempPriority;
swapped = true;
}
current = current->next;
}
}
}
bool isIdPresent(int id) {
Task* current = head;
while (current != nullptr) {
if (current->id == id) {
return true;
}
current = current->next;
}
return false;
}
void addTask(int id, string name, string description, int priority, int dueDate) {
if (isIdPresent(id)) {
cout << "Error: Task with ID " << id << " already exists." << endl;
return;
}
Task* newTask = new Task(id, name, description, priority, dueDate);
if (head == nullptr) {
head = tail = newTask;
}
else {
tail->next = newTask;
newTask->prev = tail;
tail = newTask;
}
sortTask();
saveToFile();
}
void search() {
int choice;
cout << "Search by:\n 1. Due Date\n 2. Task Priority\n 3. Task ID" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int dueDate;
cout << "Enter Due Date (YYMMDD): ";
cin >> dueDate;
Task* current = head;
while (current != nullptr) {
if (current->dueDate == dueDate) {
cout << "Task Found:" << endl;
cout << "ID: " << current->id << endl;
cout << "Name: " << current->name << endl;
cout << "Description: " << current->description << endl;
cout << "Priority: " << current->priority << endl;
cout << "Due Date: " << current->dueDate << endl;
cout << "Progress: " << current->progress << endl;
return;
}
current = current->next;
}
cout << "Task with Due Date " << dueDate << " not found." << endl;
break;
}
case 2: {
int priority;
cout << "Enter Priority (Low, Medium, High): ";
string pr;
cin >> pr;
if (pr == "high" || pr == "High")
priority = 1;
else if (pr == "medium" || pr == "Medium")
priority = 2;
else if (pr == "low" || pr == "Low")
priority = 3;
Task* current = head;
while (current != nullptr) {
if (current->priority == priority) {
cout << "Task Found:" << endl;
cout << "ID: " << current->id << endl;
cout << "Name: " << current->name << endl;
cout << "Description: " << current->description << endl;
cout << "Priority: " << current->priority << endl;
cout << "Due Date: " << current->dueDate << endl;
cout << "Progress: " << current->progress << endl;
return;
}
current = current->next;
}
cout << "Task with Priority " << priority << " not found." << endl;
break;
}
case 3: {
int id;
cout << "Enter Task ID: ";
cin >> id;
Task* current = head;
while (current != nullptr) {
if (current->id == id) {
cout << "Task Found:" << endl;
cout << "ID: " << current->id << endl;
cout << "Name: " << current->name << endl;
cout << "Description: " << current->description << endl;
cout << "Priority: " << current->priority << endl;
cout << "Due Date: " << current->dueDate << endl;
cout << "Progress: " << current->progress << endl;
return;
}
current = current->next;
}
cout << "Task with ID " << id << " not found." << endl;
break;
}
default:
cout << "Invalid choice." << endl;
break;
}
}
void pushToStack(Task task) {
StackNode* newNode = new StackNode{ task, stackTop };
stackTop = newNode;
}
Task popFromStack() {
if (!stackTop) {
cout << "Undo stack is empty.\n";
return Task{ -1, "", "", -1, -1 };
}
StackNode* temp = stackTop;
Task taskData = stackTop->taskData;
stackTop = stackTop->next;
delete temp;
return taskData;
}
void editTask(Task* head) {
int id;
cout << "Enter the ID of the task you want to edit: ";
cin >> id;
Task* current = head;
while (current != nullptr) {
if (current->id == id) {
pushToStack(*current);
cout << "Editing Task: " << endl;
cout << "Current Name: " << current->name << endl;
cout << "Enter new Name (or press Enter to keep unchanged): ";
cin.ignore();
string newName;
getline(cin, newName);
if (!newName.empty()) {
current->name = newName;
}
cout << "Current Description: " << current->description << endl;
cout << "Enter new Description (or press Enter to keep unchanged): ";
string newDescription;
getline(cin, newDescription);
if (!newDescription.empty()) {
current->description = newDescription;
}
cout << "Current Priority (High, Medium, Low): " << current->priority << endl;
cout << "Enter new Priority (or press Enter to keep unchanged): ";
string newPriority;
getline(cin, newPriority);
if (!newPriority.empty()) {
current->priority = stoi(newPriority);
}
cout << "Current Due Date (YYMMDD): " << current->dueDate << endl;
cout << "Enter new Due Date (or press Enter to keep unchanged): ";
string newDueDate;
getline(cin, newDueDate);
if (!newDueDate.empty()) {
current->dueDate = stoi(newDueDate);
}
cout << "Current Progress (in progress, completed): " << current->progress << endl;
cout << "Enter new Progress (or press Enter to keep unchanged): ";
string newProgress;
getline(cin, newProgress);
if (!newProgress.empty()) {
current->progress = newProgress;
}
sortTask();
cout << "Task updated successfully!" << endl;
saveToFile();
return;
}
current = current->next;
}
cout << "Task with ID " << id << " not found." << endl;
}
void deleteTask(int id) {
Task* current = head;
while (current) {
if (current->id == id) {
pushToStack(*current);
if (current == head && current == tail) {
head = tail = nullptr;
}
else if (current == head) {
head = head->next;
head->prev = nullptr;
}
else if (current == tail) {
tail = tail->prev;
tail->next = nullptr;
}
else {
current->prev->next = current->next;
current->next->prev = current->prev;
}
delete current;
cout << "Task deleted successfully." << endl;
saveToFile();
return;
}
current = current->next;
}
cout << "Task not found." << endl;
}
void undo() {
Task restoredTask = popFromStack();
if (restoredTask.id != -1) {
addTask(restoredTask.id, restoredTask.name, restoredTask.description, restoredTask.priority, restoredTask.dueDate);
cout << "Task restored successfully." << endl;
}
else {
cout << "No task to restore." << endl;
}
}
void markCompleted(int id) {
Task* current = head;
while (current) {
if (current->id == id) {
current->progress = "completed";
cout << "Task marked as completed.\n";
saveToFile();
return;
}
current = current->next;
}
cout << "Task not found.\n";
}
void display(Task* head) {
cout << "Tasks in the list (sorted by priority):\n";
Task* current = head;
while (current != nullptr) {
cout << "ID: " << current->id << ", Name: " << current->name << ", Description: " << current->description
<< ", Priority: " << current->priority << ", Due Date: " << current->dueDate
<< ", Progress: " << current->progress << endl;
current = current->next;
}
}
void loadFromFile() {
if (currentUsername.empty()) return;
ifstream file(currentUsername + ".txt");
if (!file.is_open()) {
cout << "No existing data for user \"" << currentUsername << "\". A new file will be created.\n";
return;
}
string line;
while (getline(file, line)) {
stringstream ss(line);
int id, priority, dueDate;
string name, description, progress;
ss >> id;
ss.ignore();
getline(ss, name, ',');
getline(ss, description, ',');
ss >> priority;
ss.ignore();
ss >> dueDate;
ss.ignore();
getline(ss, progress);
addTask(id, name, description, priority, dueDate);
Task* lastTask = tail;
if (lastTask) lastTask->progress = progress;
}
file.close();
}
int main() {
setUsername();
loadFromFile();
int choice;
do {
cout << "\nTask Management System\n";
cout << "1. Add Task\n";
cout << "2. Edit Task\n";
cout << "3. Mark Task as Completed\n";
cout << "4. Delete Task\n";
cout << "5. Search Task\n";
cout << "6. Undo Last Operation\n";
cout << "7. Display Tasks\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int id, priority, dueDate;
string name, description;
cout << "Enter Task ID: ";
cin >> id;
cin.ignore();
cout << "Enter Task Name: ";
getline(cin, name);
cout << "Enter Task Description: ";
getline(cin, description);
cout << "Enter Task Priority (1 for High, 2 for Medium, 3 for Low): ";
cin >> priority;
cout << "Enter Due Date (YYMMDD): ";
cin >> dueDate;
addTask(id, name, description, priority, dueDate);
break;
}
case 2:
editTask;
editTask(head);
break;
case 3: {
int id;
cout << "Enter task ID to mark as completed: ";
cin >> id;
markCompleted(id);
break;
}
case 4: {
int id;
cout << "Enter Task ID to delete: ";
cin >> id;
deleteTask(id);
break;
}
case 5:
search();
break;
case 6:
undo();
break;
case 7:
display(head);
break;
case 8:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice, please try again." << endl;
}
} while (choice != 8);
return 0;
}