-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskList.cpp
More file actions
426 lines (378 loc) · 10.5 KB
/
Copy pathtaskList.cpp
File metadata and controls
426 lines (378 loc) · 10.5 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
#include "taskList.h"
#include <iostream>
#include <sstream> // For stringstream
#include <fstream>
#include <stdexcept>
#include <iomanip>
// Constructors
TaskList::TaskList() : size(0), head(nullptr), tail(nullptr) {};
TaskList::TaskList(const TaskList &listToCopy) : size(listToCopy.size)
{
// Initialize the head with null if list is empty
if (listToCopy.head == nullptr)
{
head = nullptr;
tail = nullptr;
return;
}
// Initialize the head if list is not empty
head = new Node;
head->data = listToCopy.head->data;
head->next = nullptr;
tail = head;
Node *currPtr = head;
Node *copyPtr = listToCopy.head->next;
while (copyPtr != nullptr)
{
currPtr->next = new Node;
currPtr = currPtr->next;
currPtr->data = copyPtr->data;
copyPtr = copyPtr->next;
}
tail = currPtr;
};
TaskList &TaskList::operator=(const TaskList &listToCopy)
{
if (this != &listToCopy)
{
// Free old list
while (head)
{
Node *temp = head;
head = head->next;
delete temp;
}
// Copy listToCopy
size = listToCopy.size;
if (listToCopy.head == nullptr)
{
head = nullptr;
tail = nullptr;
return *this;
}
// Copy first node
head = new Node;
head->data = listToCopy.head->data;
head->next = nullptr;
tail = head;
Node *currPtr = head;
Node *copyPtr = listToCopy.head->next;
while (copyPtr != nullptr)
{
currPtr->next = new Node;
currPtr = currPtr->next;
currPtr->data = copyPtr->data;
copyPtr = copyPtr->next;
}
tail = currPtr;
}
return *this;
};
// Destructor
TaskList::~TaskList()
{
Node *curr = head;
while (curr != nullptr)
{
head = head->next;
delete curr;
curr = head;
}
}
// Methods
// Adds a task to the end of the list
// Returns the new size of the list
// If the list was empty, it updates the tail pointer
// Returns 0 on success, 1 on failure
int TaskList::addToList(const Task &task)
{
Node *newTail = new Node;
newTail->data = task;
newTail->next = nullptr;
if (tail == nullptr)
{
head = newTail;
tail = newTail;
}
else
{
tail->next = newTail;
tail = newTail;
}
// If list was empty, update tail
return ++size;
}
// Removes a task from the list
// If position is greater than 0, it removes by position
// If position is 0 or less, it removes by name
// Returns 0 on success, 1 on failure
int TaskList::removeFromList(int position, const std::string &taskName)
{
// Special case: list is empty
if (head == nullptr)
{
return 1;
}
// Remove by position
if (position > 0)
{
Node *curr = head;
Node *prev = nullptr;
int index = 1;
while (curr != nullptr)
{
if (index == position)
{
if (prev == nullptr) // Removing head
{
head = head->next;
if (head == nullptr)
tail = nullptr;
}
else
{
prev->next = curr->next;
if (curr == tail)
tail = prev;
}
delete curr;
size--;
std::cout << "Task #" << position << " removed.\n";
return 0;
}
prev = curr;
curr = curr->next;
index++;
}
std::cout << "Task number out of range.\n";
return 1;
}
// Remove by name
else
{
Node *curr = head;
Node *prev = nullptr;
while (curr != nullptr)
{
if (curr->data.getName() == taskName)
{
if (prev == nullptr) // Removing head
{
head = head->next;
if (head == nullptr)
tail = nullptr;
}
else
{
prev->next = curr->next;
if (curr == tail)
tail = prev;
}
delete curr;
size--;
std::cout << "Task \"" << taskName << "\" removed.\n";
return 0;
}
prev = curr;
curr = curr->next;
}
std::cout << "Task not found.\n";
return 1;
}
}
// Marks a task as finished
// If position is greater than 0, it marks by position
// If position is 0 or less, it marks by name
// Returns 0 on success, 1 on failure
int TaskList::markTaskFinished(int position, const std::string &taskName)
{
if (head == nullptr)
{
return 1;
}
// Find task by position
if (position > 0)
{
Node *curr = head;
int index = 1;
while (curr != nullptr)
{
if (index == position)
{
curr->data.setStatus(Task::FINISHED);
std::cout << "Task #" << position << " marked as finished.\n";
return 0;
}
curr = curr->next;
index++;
}
std::cout << "Task number out of range.\n";
}
// Find task by name
else
{
Node *curr = head;
while (curr != nullptr)
{
if (curr->data.getName() == taskName)
{
curr->data.setStatus(Task::FINISHED);
std::cout << "\nTask \"" << taskName << "\" marked as finished.\n";
return 0;
}
curr = curr->next;
}
std::cout << "Task not found.\n";
}
return 1;
}
// Checks if the list is empty
bool TaskList::isEmpty()
{
return size == 0;
}
// Returns the size of the list
int TaskList::getSize() const
{
return size;
}
// prints the task list
void TaskList::printTaskList() const
{
Node *curr = head;
int i = 1;
int maxNameLength = 0;
int maxDescriptionLength = 0;
if (curr == nullptr)
{
std::cout << "The task list is empty." << std::endl;
return;
}
std::cout << "Task List:" << std::endl;
std::cout << "| * |>------------------------------<| * |>--------------<| * |>---------------------<| * |" << std::endl;
std::cout << "| * |" << std::setw(5) << std::right << "No" << ": "
<< std::setw(25) << std::left << "Task Name"
<< "| * | Priority: " << std::setw(4) << "Prio"
<< " | * | Status : " << std::setw(12) << "Status"
<< " | * | "
<< std::endl;
std::cout << "| * |>------------------------------<| * |>--------------<| * |>---------------------<| * |" << std::endl;
while (curr != nullptr)
{
std::cout << "| * |"
<< std::setw(5) << std::right << i << ": "
<< std::setw(25) << std::left << curr->data.getName()
<< "| * | Priority: " << std::setw(4) << curr->data.getPriority()
<< " | * | Status : " << std::setw(12) << (curr->data.getStatus() == Task::FINISHED ? " Finished " : " Unfinished ")
<< " | * | "
<< std::endl;
curr = curr->next;
i++;
}
std::cout << "| * |>------------------------------<| * |>--------------<| * |>---------------------<| * |" << std::endl;
}
// Loads tasks from a CSV file
bool TaskList::loadFromCSV(const std::string &fileName)
{
std::ifstream inFile(fileName);
if (size > 0)
{
std::cout << "List is not empty, would you like to clear it? (y/n)\n";
std::string input;
std::cin >> input;
if (input == "y" || input == "Y")
{
clear();
}
}
if (!inFile.is_open())
{
std::cerr << "Could not open file: " << fileName << std::endl;
return false;
}
std::string line;
std::getline(inFile, line); // Skip header line
if (line.empty())
{
std::cerr << "Empty file: " << fileName << std::endl;
return false;
}
// Read each line and parse the task details
while (std::getline(inFile, line))
{
std::stringstream ss(line);
std::string name, priorityStr, description, statusStr;
int priority;
Task::Status status = Task::UNFINISHED;
if (std::getline(ss, name, ',') && std::getline(ss, priorityStr, ',') && std::getline(ss, description, ',') && std::getline(ss, statusStr))
{
// Convert priority
try
{
priority = std::stoi(priorityStr);
}
catch (const std::invalid_argument &e)
{
std::cerr << "Invalid priority value: " << priorityStr << std::endl;
return false;
}
// Convert status string to enum
if (statusStr == "FINISHED")
{
status = Task::FINISHED;
}
else if (statusStr == "UNFINISHED")
{
status = Task::UNFINISHED;
}
else
{
std::cerr << "Invalid status in file: " << statusStr << std::endl;
return false; // Skip this line
}
Task task(name, priority, description, status);
addToList(task);
}
else
{
std::cerr << "Error parsing line: " << line << std::endl;
return false; // Skip this line
}
}
inFile.close();
return true;
}
bool TaskList::saveToCSV(const std::string &fileName) const
{
std::ofstream outFile(fileName);
if (!outFile.is_open())
{
std::cerr << "Could not open file: " << fileName << std::endl;
return false;
}
outFile << "Task Name,Priority,Description,Status\n"; // Header
Node *curr = head;
while (curr != nullptr)
{
outFile << curr->data.getName() << ","
<< curr->data.getPriority() << ","
<< curr->data.getDescription() << ","
<< (curr->data.getStatus() == Task::FINISHED ? "FINISHED" : "UNFINISHED") << "\n";
curr = curr->next;
}
outFile.close();
return true;
}
void TaskList::clear()
{
Node *curr = head;
while (curr != nullptr)
{
Node *temp = curr;
curr = curr->next;
delete temp;
}
head = nullptr;
tail = nullptr;
size = 0;
}