-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c++
326 lines (274 loc) · 9.52 KB
/
main.c++
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <limits>
#include <unordered_map>
class TaskManager {
private:
static int getPriorityRank(const std::string& priority) {
static const std::unordered_map<std::string, int> priorityRank = {
{"High", 3}, {"Medium", 2}, {"Low", 1}
};
auto it = priorityRank.find(priority);
return (it != priorityRank.end()) ? it->second : 1;
}
struct Task {
std::string description;
std::string priority;
bool isCompleted;
int id;
static bool comparePriority(const Task& a, const Task& b) {
return getPriorityRank(a.priority) > getPriorityRank(b.priority);
}
std::string toFileString() const {
return description + "|" + priority + "|" +
(isCompleted ? "1" : "0") + "|" + std::to_string(id);
}
static Task fromFileString(const std::string& line) {
Task task;
std::istringstream ss(line);
std::string completed;
std::getline(ss, task.description, '|');
std::getline(ss, task.priority, '|');
std::getline(ss, completed, '|');
std::string idStr;
std::getline(ss, idStr);
task.isCompleted = (completed == "1");
task.id = std::stoi(idStr);
return task;
}
};
std::vector<Task> tasks;
std::string filename;
int nextTaskId;
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void pauseAndClear() {
std::cout << "\nPress Enter to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
clearScreen();
}
void displayHeader() {
clearScreen();
std::cout << "===== Task Management System =====\n";
std::cout << "==================================\n\n";
}
void saveToFile() {
std::ofstream file(filename);
if (!file) {
std::cerr << "Error: Unable to open file for writing.\n";
return;
}
for (const auto& task : tasks) {
file << task.toFileString() << std::endl;
}
file.close();
}
void loadFromFile() {
std::ifstream file(filename);
if (!file) {
return;
}
tasks.clear();
std::string line;
nextTaskId = 1;
while (std::getline(file, line)) {
Task task = Task::fromFileString(line);
tasks.push_back(task);
nextTaskId = std::max(nextTaskId, task.id + 1);
}
file.close();
}
void displayTasks(bool showCompleted = true) {
if (tasks.empty()) {
std::cout << "No tasks found.\n";
return;
}
auto sortedTasks = tasks;
std::sort(sortedTasks.begin(), sortedTasks.end(), Task::comparePriority);
std::cout << std::left
<< std::setw(5) << "ID"
<< std::setw(40) << "Description"
<< std::setw(15) << "Priority"
<< "Status\n";
std::cout << std::string(70, '-') << "\n";
for (const auto& task : sortedTasks) {
if (!showCompleted && task.isCompleted) continue;
std::cout << std::left
<< std::setw(5) << task.id
<< std::setw(40) << (task.description.length() > 40 ?
task.description.substr(0, 37) + "..." : task.description)
<< std::setw(15) << task.priority
<< (task.isCompleted ? "Completed" : "Pending") << "\n";
}
}
bool validatePriority(const std::string& priority) {
return priority == "High" || priority == "Medium" || priority == "Low";
}
public:
TaskManager(const std::string& file = "tasks.txt") : filename(file), nextTaskId(1) {
loadFromFile();
}
void run() {
while (true) {
displayHeader();
std::cout << "1. Add Task\n";
std::cout << "2. View All Tasks\n";
std::cout << "3. View Pending Tasks\n";
std::cout << "4. Edit Task\n";
std::cout << "5. Mark Task as Completed\n";
std::cout << "6. Delete Task\n";
std::cout << "7. Save and Exit\n";
std::cout << "\nEnter your choice: ";
int choice;
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choice) {
case 1: addTask(); break;
case 2:
displayHeader();
displayTasks();
pauseAndClear();
break;
case 3:
displayHeader();
displayTasks(false);
pauseAndClear();
break;
case 4: editTask(); break;
case 5: markTaskCompleted(); break;
case 6: deleteTask(); break;
case 7:
saveToFile();
std::cout << "Tasks saved. Goodbye!\n";
return;
default:
std::cout << "Invalid choice. Please try again.\n";
pauseAndClear();
}
}
}
void addTask() {
displayHeader();
Task newTask;
std::cout << "Enter task description: ";
std::getline(std::cin, newTask.description);
while (newTask.description.empty()) {
std::cout << "Description cannot be empty. Try again: ";
std::getline(std::cin, newTask.description);
}
while (true) {
std::cout << "Enter task priority (High/Medium/Low): ";
std::getline(std::cin, newTask.priority);
if (validatePriority(newTask.priority)) {
break;
}
std::cout << "Invalid priority. Please choose High, Medium, or Low.\n";
}
newTask.isCompleted = false;
newTask.id = nextTaskId++;
tasks.push_back(newTask);
std::cout << "Task added successfully!\n";
pauseAndClear();
}
void editTask() {
displayHeader();
displayTasks();
if (tasks.empty()) {
pauseAndClear();
return;
}
int taskId;
std::cout << "Enter task ID to edit: ";
std::cin >> taskId;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
auto it = std::find_if(tasks.begin(), tasks.end(),
[taskId](const Task& t) { return t.id == taskId; });
if (it != tasks.end()) {
std::cout << "Current description: " << it->description << "\n";
std::cout << "Enter new description (or press Enter to keep current): ";
std::string newDesc;
std::getline(std::cin, newDesc);
if (!newDesc.empty()) {
it->description = newDesc;
}
while (true) {
std::cout << "Current priority: " << it->priority << "\n";
std::cout << "Enter new priority (High/Medium/Low, or press Enter to keep current): ";
std::string newPriority;
std::getline(std::cin, newPriority);
if (newPriority.empty()) break;
if (validatePriority(newPriority)) {
it->priority = newPriority;
break;
}
std::cout << "Invalid priority. Please choose High, Medium, or Low.\n";
}
std::cout << "Task updated successfully!\n";
} else {
std::cout << "Task not found.\n";
}
pauseAndClear();
}
void markTaskCompleted() {
displayHeader();
displayTasks(false);
if (tasks.empty()) {
pauseAndClear();
return;
}
int taskId;
std::cout << "Enter task ID to mark as completed: ";
std::cin >> taskId;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
auto it = std::find_if(tasks.begin(), tasks.end(),
[taskId](const Task& t) { return t.id == taskId; });
if (it != tasks.end()) {
it->isCompleted = true;
std::cout << "Task marked as completed!\n";
} else {
std::cout << "Task not found.\n";
}
pauseAndClear();
}
void deleteTask() {
displayHeader();
displayTasks();
if (tasks.empty()) {
pauseAndClear();
return;
}
int taskId;
std::cout << "Enter task ID to delete: ";
std::cin >> taskId;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
auto it = std::find_if(tasks.begin(), tasks.end(),
[taskId](const Task& t) { return t.id == taskId; });
if (it != tasks.end()) {
tasks.erase(it);
std::cout << "Task deleted successfully!\n";
} else {
std::cout << "Task not found.\n";
}
pauseAndClear();
}
};
int main() {
try {
TaskManager manager;
manager.run();
} catch (const std::exception& e) {
std::cerr << "An error occurred: " << e.what() << std::endl;
return 1;
}
return 0;
}