-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
784 lines (726 loc) · 23.7 KB
/
main.cpp
File metadata and controls
784 lines (726 loc) · 23.7 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
* Author: Ethan Xu
* Project Start Date: 11-21-2023
* Version Number: 1.6.3
*/
// Contribution: aqariio
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <termios.h>
#include <unistd.h>
using namespace std;
ofstream outputSaveStream;
ifstream inputReadStream;
string currentVersion = "1.6.3";
string sortBy = "priority";
string filename;
const vector<string> COMMANDS = {"showcommands", "version", "add", "exit", "clear", "display", "remove", "showdescription", "edit", "togglesortby", "search", "erase", "logout", "filter"};
const int WIDTH = 70;
class Task {
public:
string name;
string date;
string description;
string priority;
string tag;
};
vector<Task> tasks;
class Program {
private:
static void printTopRow() {
cout << "\n";
cout << "\033[34m";
for (int i = 0; i < WIDTH; i++) {
cout << "-";
}
cout << "\033[0m";
cout << "\n";
}
static void printBottomRow() {
cout << "\033[34m";
for (int i = 0; i < WIDTH; i++) {
cout << "-";
}
cout << "\033[0m";
cout << "\n\n";
}
static void printf(const string& text) {
int size = (int) text.size();
int width = WIDTH;
width -= 3;
cout << "\033[34m| \033[0m";
cout << text;
for (int i = 0; i < (int) width - size; i++) {
cout << " ";
}
cout << "\033[34m|\033[0m";
cout << "\n";
}
static void printf(const string& text, int size) {
int width = WIDTH;
width -= 3;
cout << "\033[34m| \033[0m";
cout << text;
for (int i = 0; i < (int) width - size; i++) {
cout << " ";
}
cout << "\033[34m|\033[0m";
cout << "\n";
}
void checkDate(string date) {
if (date.size() != 10) {
cout << "\033[31mDate must be in the format of YYYY-MM-DD.\n\033[0m";
printBottomRow();
showMenu();
return;
}
if (date[4] != '-' || date[7] != '-') {
cout << "\033[31mDate must be in the format of YYYY-MM-DD.\n\033[0m";
printBottomRow();
showMenu();
return;
}
string sYear = date.substr(0, 4);
if (!all_of(sYear.begin(), sYear.end(), ::isdigit)) {
cout << "\033[31mYear must be a number.\n\033[0m";
printBottomRow();
showMenu();
return;
}
string sMonth = date.substr(5, 2);
if (!all_of(sMonth.begin(), sMonth.end(), ::isdigit)) {
cout << "\033[31mMonth must be a number.\n\033[0m";
printBottomRow();
showMenu();
return;
}
string sDay = date.substr(8, 2);
if (!all_of(sDay.begin(), sDay.end(), ::isdigit)) {
cout << "\033[31mDay must be a number.\n\033[0m";
printBottomRow();
showMenu();
return;
}
int year = stoi(sYear);
int month = stoi(sMonth);
int day = stoi(sDay);
if (year < 2023) {
cout << "\033[31mYear must be greater than or equal to 2023.\n\033[0m";
printBottomRow();
showMenu();
return;
}
if (month < 1 || month > 12) {
cout << "\033[31mMonth must be between 1 and 12.\n\033[0m";
printBottomRow();
showMenu();
return;
}
if (day < 1 || day > 31) {
cout << "\033[31mDay must be between 1 and 31.\n\033[0m";
printBottomRow();
showMenu();
return;
}
if (month == 2) {
if (year % 4 == 0) {
if (day > 29) {
cout << "\033[31mDay must be between 1 and 29.\n\033[0m";
printBottomRow();
showMenu();
return;
}
} else {
if (day > 28) {
cout << "\033[31mDay must be between 1 and 28.\n\033[0m";
printBottomRow();
showMenu();
return;
}
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
cout << "\033[31mDay must be between 1 and 30.\n\033[0m";
printBottomRow();
showMenu();
return;
}
}
}
int checkValidID(string sid) {
if (!all_of(sid.begin(), sid.end(), ::isdigit)) {
cout << "\033[31mID must be a number.\n\033[0m";
printBottomRow();
showMenu();
return -1;
}
if (sid.empty()) {
cout << "\033[31mID must not be empty.\n\033[0m";
printBottomRow();
showMenu();
return -1;
}
int id = stoi(sid);
if (id < 1) {
cout << "\033[31mID must be greater than or equal to 1.\n\033[0m";
printBottomRow();
showMenu();
return -1;
}
if (tasks.size() < id) {
cout << "\033[31mTask not found.\n\033[0m";
printBottomRow();
showMenu();
return -1;
}
return id;
}
static void printWindow(const string& text) {
printTopRow();
cout << "| ";
int chars = 2;
for (auto c : text) {
cout << c;
chars++;
if (chars == WIDTH - 2) {
cout << " |\n| ";
chars = 2;
}
}
while (chars < WIDTH - 1) {
cout << " ";
chars++;
}
cout << "|\n";
printBottomRow();
}
static void refreshStorage() {
outputSaveStream.close();
outputSaveStream.open(filename, ios::trunc);
outputSaveStream.close();
outputSaveStream.open(filename, ios::app);
for (const Task& task : tasks) {
outputSaveStream << "\"" << task.name << "\"" << "," << "\"" << task.date << "\"" << "," << "\"" << task.description << "\"" << "," << "\"" << task.priority << "\"" << "," << "\"" << task.tag << "\"" << "\n";
}
outputSaveStream.flush();
}
static void sort() {
if (sortBy == "priority") {
std::sort(tasks.begin(), tasks.end(), sortByPriority);
} else {
std::sort(tasks.begin(), tasks.end(), sortByDate);
}
}
static void toLowerCase(string &s) {
for (char &c : s) {
c = (char) tolower(c);
}
}
static bool sortByPriority(const Task& t1, const Task& t2) {
if (t1.priority == "high" && t2.priority == "medium") {
return true;
}
if (t1.priority == "high" && t2.priority == "low") {
return true;
}
if (t1.priority == "medium" && t2.priority == "low") {
return true;
}
return false;
}
static bool sortByDate(const Task& t1, const Task& t2) {
int year1 = stoi(t1.date.substr(0, 4));
int year2 = stoi(t2.date.substr(0, 4));
if (year1 < year2) {
return true;
}
if (year1 > year2) {
return false;
}
int month1 = stoi(t1.date.substr(5, 2));
int month2 = stoi(t2.date.substr(5, 2));
if (month1 < month2) {
return true;
}
if (month1 > month2) {
return false;
}
int day1 = stoi(t1.date.substr(8, 2));
int day2 = stoi(t2.date.substr(8, 2));
if (day1 < day2) {
return true;
}
if (day1 > day2) {
return false;
}
return true;
}
public:
void showMenu() {
cout << "Execute a command here (showCommands to show commands): ";
string command;
getline(cin, command);
toLowerCase(command);
int index = (int) (find(COMMANDS.begin(), COMMANDS.end(), command) - COMMANDS.begin());
if (index < COMMANDS.size()) {
switch (index) {
case 0:
showCommands();
break;
case 1:
version();
break;
case 2:
add();
break;
case 3:
exitProgram();
break;
case 4:
clear();
break;
case 5:
display();
break;
case 6:
remove();
break;
case 7:
showDescription();
break;
case 8:
edit();
break;
case 9:
toggleSortBy();
break;
case 10:
search();
break;
case 11:
erase();
break;
case 12:
tasks.clear();
cout << "\033[32mLogged out.\n\033[0m";
askForLogin();
initialization();
break;
case 13:
default:
filter();
break;
}
} else {
cout << "\033[31mCommand not found.\n\033[0m";
showMenu();
}
}
void showCommands() {
printTopRow();
printf("Here are the list of commands:");
printf("showcommands -> show all commands available (0.1)");
printf("version -> show version of program (0.1)");
printf("add -> add a task (0.2)");
printf("exit -> exit program (0.2)");
printf("clear -> clear all tasks (0.3)");
printf("display -> display all tasks (0.3)");
printf("remove -> remove a task (0.3)");
printf("showdescription -> display the description of a task (0.5)");
printf("edit -> edit a task (0.7)");
printf("togglesortby -> toggle sort mode between priority and date (1.1)");
printf("search -> search for a task (1.2)");
printf("erase -> erase account (1.4)");
printf("logout -> logout (1.4)");
printf("filter -> filter by tag (1.5)");
printBottomRow();
showMenu();
}
void version() {
printTopRow();
printf("\033[31mT\033[32ma\033[33ms\033[34mk \033[31mM\033[32ma\033[33mn\033[34ma\033[35mg\033[36me\033[37mr\033[0m", 12);
printf("Version: " + currentVersion);
printBottomRow();
showMenu();
}
void add() {
printTopRow();
Task task;
cout << "Enter the name of the task: ";
string name;
getline(cin, name);
task.name = name;
cout << "Enter the date of the task (YYYY-MM-DD): ";
string date;
getline(cin, date);
checkDate(date);
task.date = date;
cout << "Give a short description of the task (Optional): ";
string description;
getline(cin, description);
task.description = description;
cout << "Enter the priority of the task (low, medium (default), high): ";
string priority;
getline(cin, priority);
if (priority == "low" || priority == "medium" || priority == "high") {
task.priority = priority;
} else {
task.priority = "medium";
}
cout << "Give it a tag (red, orange, yellow, green, blue, purple, gray) (Optional): ";
string tag;
getline(cin, tag);
if (tag == "red" || tag == "orange" || tag == "yellow" || tag == "green" || tag == "blue" || tag == "purple" || tag == "gray") {
task.tag = tag;
} else {
task.tag = "";
}
tasks.push_back(task);
sort();
outputSaveStream << "\"" << task.name << "\"" << "," << "\"" << task.date << "\"" << "," << "\"" << task.description << "\"" << "," << "\"" << task.priority << "\"" << "," << "\"" << task.tag << "\"" << "\n";
outputSaveStream.flush();
cout << "\033[32mTask added.\n\033[0m";
printBottomRow();
display();
showMenu();
}
static void exitProgram() {
printTopRow();
printf("Exiting...");
outputSaveStream.close();
printBottomRow();
exit(0);
}
void clear() {
printTopRow();
cout << "Are you sure you want to clear all tasks? (y/n): ";
string answer;
getline(cin, answer);
if (answer == "y") {
outputSaveStream.close();
outputSaveStream.open(filename, ios::trunc);
outputSaveStream.close();
outputSaveStream.open(filename, ios::app);
outputSaveStream.flush();
tasks.clear();
cout << "\033[32mCleared.\n\033[0m";
} else {
cout << "\033[31mCancelled\n\033[0m";
}
printBottomRow();
display();
showMenu();
}
void initialization() {
cout << "Initializing...\n";
inputReadStream.open(filename);
outputSaveStream.open(filename, ios::app);
string line;
while (getline(inputReadStream, line)) {
int state = 0;
Task task;
for (char c : line) {
if (c == ',') {
state++;
continue;
}
if (state == 0) {
task.name += c;
} else if (state == 1) {
task.date += c;
} else if (state == 2) {
task.description += c;
} else if (state == 3) {
task.priority += c;
} else if (state == 4) {
task.tag += c;
}
}
task.name.erase(0, 1);
task.name.erase(task.name.size() - 1);
task.date.erase(0, 1);
task.date.erase(task.date.size() - 1);
task.description.erase(0, 1);
task.description.erase(task.description.size() - 1);
task.priority.erase(0, 1);
task.priority.erase(task.priority.size() - 1);
task.tag.erase(0, 1);
task.tag.erase(task.tag.size() - 1);
tasks.push_back(task);
}
sort();
inputReadStream.close();
cout << "\033[32mInitialized.\n\033[0m";
display();
showMenu();
}
void display() {
printTopRow();
if (tasks.empty()) {
printf("\033[31mNo tasks found.\033[0m", 15);
printBottomRow();
showMenu();
return;
}
printf("Here are the list of tasks:");
printf("Sort by: " + sortBy);
printf("ID | Task Name | Due Date | Priority | Tag");
int id = 1;
for (const Task& task : tasks) {
printf(to_string(id) + " | " + task.name + " | " + task.date + " | " + task.priority + " | " + task.tag);
id++;
}
printBottomRow();
showMenu();
}
void remove() {
printTopRow();
string sid;
cout << "Enter the ID of the task you want to remove: ";
getline(cin, sid);
int id = checkValidID(sid);
cout << "Are you sure you want to remove this task? (y/n): ";
string answer;
getline(cin, answer);
if (answer == "y") {
tasks.erase(tasks.begin() + id - 1);
refreshStorage();
cout << "\033[32mRemoved.\n\033[0m";
} else {
cout << "\033[31mCancelled\n\033[0m";
}
printBottomRow();
display();
showMenu();
}
void showDescription() {
printTopRow();
cout << "Enter the ID of the task that you want to view the description of: ";
string sid;
getline(cin, sid);
printBottomRow();
int id = checkValidID(sid);
string description = tasks[id - 1].description;
if (description.empty()) {
printf("\033[31mNo description found.\033[0m");
printBottomRow();
showMenu();
return;
}
printWindow(description);
showMenu();
}
void edit() {
printTopRow();
cout << "Enter the ID of the task that you want to edit: ";
string sid;
getline(cin, sid);
int id = checkValidID(sid);
Task task = tasks[id - 1];
cout << "Enter the new name of the task (Optional): ";
string name;
getline(cin, name);
if (!name.empty()) {
task.name = name;
}
cout << "Enter the new date of the task (Optional): ";
string date;
getline(cin, date);
if (!date.empty()) {
checkDate(date);
task.date = date;
}
cout << "Enter the new description of the task (Optional): ";
string description;
getline(cin, description);
if (!description.empty()) {
task.description = description;
}
cout << "Enter the new priority of the task (Optional): ";
string priority;
getline(cin, priority);
if (!priority.empty()) {
if (priority == "low" || priority == "medium" || priority == "high") {
task.priority = priority;
} else {
task.priority = "medium";
}
}
tasks[id - 1] = task;
sort();
refreshStorage();
cout << "\033[32mEdited.\n\033[0m";
printBottomRow();
display();
showMenu();
}
void toggleSortBy() {
sortBy = sortBy == "priority" ? "date" : "priority";
sort();
display();
showMenu();
}
void search() {
printTopRow();
cout << "Enter the keyword you want to search for: ";
string keyword;
getline(cin, keyword);
printf("ID | Task Name | Due Date | Priority");
int id = 1;
bool found = false;
for (const Task& task : tasks) {
if (task.name.find(keyword) != string::npos) {
printf(to_string(id) + " | " + task.name + " | " + task.date + " | " + task.priority);
found = true;
}
id++;
}
if (!found) {
printf("\033[31mNo tasks found.\033[0m", 15);
}
printBottomRow();
showMenu();
}
void askForLogin() {
printTopRow();
cout << "Enter your username (enter to create new account): ";
string username;
getline(cin, username);
if (username.empty()) {
cout << "Enter your new username: ";
getline(cin, username);
cout << "Enter your new password: ";
string password;
getline(cin, password);
ofstream outputUsernameStream;
outputUsernameStream.open("username.csv", ios::app);
outputUsernameStream << username << "," << password << "\n";
outputUsernameStream.flush();
outputUsernameStream.close();
cout << "\033[32mAccount created.\n\033[0m";
printBottomRow();
askForLogin();
return;
}
ifstream inputUsernameStream;
inputUsernameStream.open("username.csv");
string line;
while (getline(inputUsernameStream, line)) {
if (line.find(username) != string::npos) {
line.erase(0, username.size() + 1);
termios oldt{};
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
cout << "Enter your password: ";
string password;
getline(cin, password);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
if (line.find(password) != string::npos) {
cout << "\033[32mLogin successful.\n\033[0m";
filename = username + ".csv";
inputUsernameStream.close();
printBottomRow();
return;
} else {
cout << "\033[31mIncorrect password.\n\033[0m";
printBottomRow();
askForLogin();
return;
}
}
}
cout << "\033[31mUsername not found.\n\033[0m";
askForLogin();
}
void erase() {
printTopRow();
cout << "Are you sure you want to permanently erase this account? (y/n): ";
string answer;
getline(cin, answer);
if (answer == "y") {
cout << "Enter your username: ";
string username;
getline(cin, username);
cout << "Enter your password: ";
string password;
getline(cin, password);
ifstream inputUsernameStream;
inputUsernameStream.open("username.csv");
string line;
while (getline(inputUsernameStream, line)) {
if (line.find(username) != string::npos) {
line.erase(0, username.size() + 1);
if (line.find(password) != string::npos) {
char charfile[username.size() + 4];
strcpy(charfile, username.c_str());
strcat(charfile, ".csv");
std::remove(charfile);
ifstream inputUsernameStream2;
inputUsernameStream2.open("username.csv");
string line2;
ofstream outputUsernameStream;
outputUsernameStream.open("username2.csv", ios::app);
while (getline(inputUsernameStream2, line2)) {
if (line2.find(username) == string::npos) {
outputUsernameStream << line2 << "\n";
}
}
outputUsernameStream.flush();
outputUsernameStream.close();
inputUsernameStream2.close();
std::remove("username.csv");
std::rename("username2.csv", "username.csv");
cout << "\033[32mAccount erased.\n\033[0m";
printBottomRow();
exitProgram();
return;
} else {
cout << "\033[31mIncorrect password.\n\033[0m";
printBottomRow();
erase();
return;
}
}
}
cout << "\033[31mUsername not found.\n\033[0m";
printBottomRow();
erase();
} else {
cout << "\033[31mCancelled\n\033[0m";
printBottomRow();
showMenu();
}
}
void filter() {
printTopRow();
cout << "Enter the tag you want to filter for: ";
string tag;
getline(cin, tag);
printf("ID | Task Name | Due Date | Priority");
int id = 1;
bool found = false;
for (const Task& task : tasks) {
if (task.tag.find(tag) != string::npos) {
printf(to_string(id) + " | " + task.name + " | " + task.date + " | " + task.priority);
found = true;
}
id++;
}
if (!found) {
printf("\033[31mNo tasks found.\033[0m", 15);
}
printBottomRow();
showMenu();
}
};
int main() {
Program program;
program.askForLogin();
program.initialization();
}