-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
290 lines (243 loc) · 9.11 KB
/
main.cpp
File metadata and controls
290 lines (243 loc) · 9.11 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
#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>
using namespace std;
struct Task {
string description;
bool is_completed;
string category;
time_t created_at;
Task(const string& desc, bool is_compltd, const string& ctg, time_t created = time(nullptr)) {
description = desc;
is_completed = is_compltd;
category = ctg;
created_at = created;
}
};
void writeIntoFile(vector<Task>& tasks) {
ofstream writeFile("tasks.txt"); // write file (.txt)
for (int i = 0; i < tasks.size(); i++) {
writeFile << tasks[i].description << "|" << tasks[i].is_completed << "|" << tasks[i].category << "|" << tasks[i].created_at << endl;
}
}
void loadFromFile(vector<Task>& tasks) {
string task_description;
bool task_is_completed;
string task_category;
time_t task_created_at;
string line;
ifstream readFile("tasks.txt");
while (getline(readFile, line)) {
size_t pos1 = line.find("|"); // position number
size_t pos2 = line.find("|", pos1 + 1); // position number
size_t pos3 = line.find("|", pos2 + 1); // position number
task_description = line.substr(0, pos1);
string str_num1 = line.substr(pos1 + 1, 1);
int task_status = stoi(str_num1);
task_is_completed = task_status;
task_category = line.substr(pos2 + 1, pos3 - pos2 - 1);
string str_num2 = line.substr(pos3 + 1);
task_created_at = stoll(str_num2);
tasks.push_back(Task(task_description, task_is_completed, task_category, task_created_at));
}
}
void showStats(vector<Task>& tasks) {
int count_completed = 0;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].is_completed == true) {
count_completed++;
}
}
cout << "📊 " << "Total: " << tasks.size() << " tasks (✅ "
<< count_completed << " done, " << "⏳ "
<< tasks.size() - count_completed << " pending)\n" << endl;
}
void showHeader() {
cout << "\n╔══════════════════════════════════════╗\n";
cout << "║ 📝 C++ TODO MANAGER ║\n";
cout << "╚══════════════════════════════════════╝\n";
cout << "Commands: list, add, edit, complete, help, clear, exit" << endl;
}
void showTasksList(vector<Task>& tasks) {
cout << "╔══════════════════════════════════════╗\n";
cout << "║ 📋 TASKS LIST ║\n";
cout << "╚══════════════════════════════════════╝\n";
showStats(tasks);
if (!tasks.empty()) {
for (int i = 0; i < tasks.size(); i++) {
cout << "┌─[" << i+1 << "]─ " << tasks[i].description << "\n";
cout << "│ Status: " << (tasks[i].is_completed ? "[\033[32m✅ DONE\033[0m]" : "[\033[31m⏳ PENDING\033[0m]") << "\n";
cout << "│ Category: " << tasks[i].category << "\n";
cout << "│ Created at: " << ctime(&tasks[i].created_at);
cout << "└───────────────────────────────────────\n";
}
return;
}
cout << "Tasks list is empty. Use command 'add' to add a new task." << endl;
}
void showHelp() {
cout << "Available commands:" << endl;
cout << " list - Show all tasks" << endl;
cout << " add - Add a new task" << endl;
cout << " complete [task number] - Complete task" << endl;
cout << " uncomplete [task number] - Uncomplete task" << endl;
cout << " delete [task number] - Delete task" << endl;
cout << " edit [task number] - Edit task" << endl;
cout << " search [text] - Search tasks" << endl;
cout << " help - Show this help" << endl;
cout << " clear - Clear console" << endl;
cout << " exit - Quit program" << endl;
}
void addTask(vector<Task>& tasks) {
string taskDesc;
string taskCtg;
cout << "Enter task description: ";
getline(cin, taskDesc);
cout << "Enter task category: ";
getline(cin, taskCtg);
if (!taskDesc.empty() && !taskCtg.empty()) {
tasks.push_back(Task(taskDesc, false, taskCtg));
writeIntoFile(tasks);
cout << "Task added!" << endl;
return;
}
cout << "Error: Task description or category is empty!" << endl;
}
void completeTask(string& input, vector<Task>& tasks) {
try {
string str_num = input.substr(9);
int task_num = stoi(str_num);
if (task_num < 1 || task_num > tasks.size()) {
cout << "Error: Task " << task_num << " doesn't exist!" << endl;
return;
}
if (tasks[task_num - 1].is_completed == false) {
tasks[task_num - 1].is_completed = true;
writeIntoFile(tasks);
cout << "Task " << task_num << " was marked completed!" << endl;
return;
} else {
cout << "Task is already completed!" << endl;
return;
}
} catch(...) {
cout << "Invalid task number!" << endl;
}
}
void unCompleteTask(string& input, vector<Task>& tasks) {
try {
string str_num = input.substr(11);
int task_num = stoi(str_num);
if (task_num < 1 || task_num > tasks.size()) {
cout << "Error: Task " << task_num << " doesn't exist!" << endl;
return;
}
if (tasks[task_num - 1].is_completed == true) {
tasks[task_num - 1].is_completed = false;
writeIntoFile(tasks);
cout << "Task " << task_num << " was marked uncompleted!" << endl;
return;
} else {
cout << "Task is not completed yet!" << endl;
return;
}
} catch(...) {
cout << "Invalid task number!" << endl;
}
}
void deleteTask(string& input, vector<Task>& tasks) {
try {
string str_num = input.substr(7);
int task_num = stoi(str_num);
if (task_num < 1 || task_num > tasks.size()) {
cout << "Error: Task " << task_num << " doesn't exist!" << endl;
return;
}
tasks.erase(tasks.begin() + (task_num - 1));
writeIntoFile(tasks);
cout << "Task " << task_num << " was successfully deleted!" << endl;
} catch(...) {
cout << "Invalid task number!" << endl;
}
}
void editTask(string& input, vector<Task>& tasks) {
try {
string str_num = input.substr(5);
int task_num = stoi(str_num);
string newTaskDesc;
if (task_num < 1 || task_num > tasks.size()) {
cout << "Error: Task " << task_num << " doesn't exist!" << endl;
return;
}
cout << "Enter new task description: ";
getline(cin, newTaskDesc);
if (!newTaskDesc.empty()) {
tasks[task_num - 1].description = newTaskDesc;
writeIntoFile(tasks);
cout << "Task " << task_num << " was successfully edited!" << endl;
return;
}
cout << "Task description is empty!" << endl;
} catch(...) {
cout << "Invalid task number!" << endl;
}
}
void searchTask(string& input, vector<Task>& tasks) {
bool found = false;
string taskSearchDesc = input.substr(7);
if (taskSearchDesc.empty()) {
cout << "Error: Search query is empty!" << endl;
return;
}
cout << "🔎 Result of searching '" << taskSearchDesc << "':" << endl;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].description.find(taskSearchDesc) != string::npos) {
cout << "[" << i + 1 << "] " << tasks[i].description << " - " << (tasks[i].is_completed ? "✅" : "⏳") << endl;
found = true;
}
}
if (found == false) {
cout << "No tasks found!" << endl;
}
}
int main() {
vector<Task> tasks;
string input;
showHeader();
loadFromFile(tasks);
while (true) {
cout << endl;
cout << "\033[1;36m";
cout << "> ";
getline(cin, input);
cout << "\033[0m";
if (input.empty()) {
continue;
} else if (input == "list") {
showTasksList(tasks);
} else if (input == "add") {
addTask(tasks);
} else if (input == "clear") {
system("clear");
showHeader();
} else if (input.rfind("complete ", 0) == 0) {
completeTask(input, tasks);
} else if (input.rfind("uncomplete ", 0) == 0) {
unCompleteTask(input, tasks);
} else if (input.rfind("delete ", 0) == 0) {
deleteTask(input, tasks);
} else if (input.rfind("edit ", 0) == 0) {
editTask(input, tasks);
} else if (input.rfind("search ", 0) == 0) {
searchTask(input, tasks);
} else if (input == "help") {
showHelp();
} else if (input == "exit") {
break;
} else {
cout << "Invalid command!" << endl;
}
}
return 0;
}