-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.cpp
More file actions
87 lines (69 loc) · 2.53 KB
/
Task.cpp
File metadata and controls
87 lines (69 loc) · 2.53 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
#include "Task.h"
Task::Task(const string& title, const string& description, bool important, const string& expirationDate)
: title(title), description(description), completed(false), important(important), expirationDate(expirationDate) {
if (!expirationDate.empty() && expirationDate != "-" && !isValidDateFormat(expirationDate)) {
throw invalid_argument("Data non valida. Usa il formato DD-MM-AAAA.");
}
this->expirationDate = expirationDate.empty() ? "-" : expirationDate;
}
bool Task::isValidDateFormat(const string& date) {
// Regex per "DD-MM-AAAA", con giorno 01-31, mese 01-12, anno a 4 cifre
regex dateRegex(R"(^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-\d{4}$)");
return regex_match(date, dateRegex);
}
void Task::markComplete() {
completed = true;
}
void Task::markImportant(){
important = true;
}
void Task::markNotImportant(){
important = false;
}
bool Task::isCompleted() const{
return completed;
}
bool Task::isImportant() const{
return important;
}
string Task::getDescription() const{
return description;
}
string Task::getExpirationDate() const {
return expirationDate;
}
string Task::getTitle() const {
return title;
}
void Task::setTitle(const string& newTitle) {
title = newTitle;
}
void Task::setDescription(const string& newDescription) {
description = newDescription;
}
void Task::setExpirationDate(const string& newDate) {
if (!newDate.empty() && newDate != "-" && !isValidDateFormat(newDate)) {
throw invalid_argument("Data non valida. Usa il formato DD-MM-AAAA o lascia vuoto per nessuna scadenza.");
}
expirationDate = newDate.empty() ? "-" : newDate;
}
// Metodo per convertire la task in stringa per la scrittura su file
string Task::toString() const {
string completed_ = completed ? "completata" : "non completata";
string imp = important ? "importante" : "non importante";
return title + "|" + description + "|" + completed_ + "|" + imp + "|" + expirationDate;
}
// Metodo per ricostruire la task da una stringa letta dal file
Task Task::toTask(const string& data) {
stringstream ss(data);
string title, description, expirationDate, completedStr, importantStr;
getline(ss, title, '|');
getline(ss, description, '|');
getline(ss, completedStr, '|');
getline(ss, importantStr, '|');
getline(ss, expirationDate, '|');
bool important = (importantStr == "importante");
Task task(title, description, important, expirationDate);
task.completed = (completedStr == "completata");
return task;
}