-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
525 lines (491 loc) Β· 16 KB
/
Source.cpp
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <windows.h>
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 setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void setUsername() {
setColor(10); // Green
cout << "\nEnter your username: ";
setColor(7); // White
getline(cin, currentUsername);
ifstream file(currentUsername + ".txt");
if (!file.is_open()) {
ofstream newFile(currentUsername + ".txt");
newFile.close();
setColor(14); // Yellow
cout << "New file created for user \"" << currentUsername << "\".\n";
}
else {
setColor(11); // Cyan
cout << "Welcome back, \"" << currentUsername << "\".\n";
}
setColor(7); // Reset to White
}
void saveToFile() {
if (currentUsername.empty()) return;
ofstream file(currentUsername + ".txt");
if (!file.is_open()) {
setColor(12); // Red
cout << "Error: Could not open file to save data.\n";
setColor(7); // Reset
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 if (newTask->priority < head->priority) {
newTask->next = head;
head->prev = newTask;
head = newTask;
}
else {
Task* current = head;
while (current->next != nullptr && current->next->priority <= newTask->priority) {
current = current->next;
}
newTask->next = current->next;
if (current->next != nullptr) {
current->next->prev = newTask;
}
else {
tail = newTask;
}
current->next = newTask;
newTask->prev = current;
}
cout << "Task added successfully!" << endl;
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) {
setColor(13); // Light Purple
cout << "\n================== Task List ==================\n";
setColor(7); // White
Task* current = head;
while (current != nullptr) {
setColor(11); // Cyan
cout << "\n----------------------------------------------\n";
setColor(14); // Yellow
cout << "ID: " << current->id << "\n";
cout << "Name: " << current->name << "\n";
cout << "Description: " << current->description << "\n";
setColor(10); // Green
cout << "Priority: " << (current->priority == 1 ? "High" : (current->priority == 2 ? "Medium" : "Low")) << "\n";
cout << "Due Date: " << current->dueDate << "\n";
cout << "Progress: " << current->progress << "\n";
setColor(11); // Cyan
cout << "----------------------------------------------\n";
current = current->next;
}
setColor(13); // Light Purple
cout << "==============================================\n\n";
setColor(7); // Reset to White
}
void loadFromFile() {
if (currentUsername.empty()) return;
ifstream file(currentUsername + ".txt");
if (!file.is_open()) {
setColor(14); // Yellow
cout << "No existing data for user \"" << currentUsername << "\". A new file will be created.\n";
setColor(7); // Reset
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() {
setColor(13); // Light Purple
cout << "\n\n\n\t\t\t\t---------------------------------\n";
Sleep(350);
setColor(11); // Cyan
cout << "\t\t\t\t\tSmart To-Do List\n";
Sleep(350);
setColor(13); // Light Purple
cout << "\t\t\t\t---------------------------------\n";
setColor(7); // Reset
cout << endl;
Sleep(350);
setUsername();
loadFromFile();
int choice;
do {
setColor(11); // Cyan
cout << "\n\t\t\t\t================== Task Management System ==================\n";
setColor(7); // White
cout << "\n\t\t\t\t\t--------------------------------------------" << endl;
cout << "\t\t\t\t\t\t||1. Add Task || \n";
cout << "\t\t\t\t\t\t||2. Edit Task || \n";
cout << "\t\t\t\t\t\t||3. Mark Task as Completed || \n";
cout << "\t\t\t\t\t\t||4. Delete Task || \n";
cout << "\t\t\t\t\t\t||5. Search Task || \n";
cout << "\t\t\t\t\t\t||6. Undo Last Operation || \n";
cout << "\t\t\t\t\t\t||7. Display Tasks || \n";
cout << "\t\t\t\t\t\t||8. Exit || \n";
cout << "\t\t\t\t\t---------------------------------------------" << endl;
cout << "\nEnter your choice: ";
setColor(14); // Yellow
cin >> choice;
setColor(7); // Reset
switch (choice) {
case 1: {
int id, priority, dueDate;
string name, description;
setColor(10); // Green
cout << "Enter Task ID: ";
setColor(7); // White
cin >> id;
cin.ignore();
setColor(10);
cout << "Enter Task Name: ";
setColor(7);
getline(cin, name);
setColor(10);
cout << "Enter Task Description: ";
setColor(7);
getline(cin, description);
setColor(10);
cout << "Enter Task Priority (1 for High, 2 for Medium, 3 for Low): ";
setColor(7);
cin >> priority;
setColor(10);
cout << "Enter Due Date (YYMMDD): ";
setColor(7);
cin >> dueDate;
addTask(id, name, description, priority, dueDate);
break;
}
case 2:
//editTask;
editTask(head);
break;
case 3: {
int id;
setColor(10);
cout << "Enter task ID to mark as completed: ";
setColor(7);
cin >> id;
markCompleted(id);
break;
}
case 4: {
int id;
setColor(10);
cout << "Enter Task ID to delete: ";
setColor(7);
cin >> id;
deleteTask(id);
break;
}
case 5:
search();
break;
case 6:
undo();
break;
case 7:
display(head);
break;
case 8:
setColor(12); // Red
cout << "Exiting...\n";
setColor(7); // Reset
break;
default:
setColor(12); // Red
cout << "Invalid choice, please try again.\n";
setColor(7); // Reset
}
} while (choice != 8);
}