-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
222 lines (194 loc) · 8.51 KB
/
main.cpp
File metadata and controls
222 lines (194 loc) · 8.51 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
//Classi che rappresentano attività da svolgere (es. per todo list). Deve essere possibile scrivere e leggere da disco l'elenco delle attività.
#include <iostream>
#include <string>
#include <limits>
#include <filesystem>
namespace fs = std::filesystem;
#include "Task.h"
#include "TodoList.h"
using namespace std;
int main() {
string basePath = "../liste/";
string listName;
cout << "Nome della tua TodoList: ";
getline(cin, listName);
string fileName = basePath + listName + ".txt";
TodoList todoList(listName);
todoList.loadFromFile(fileName);
cout << "Hai aperto la lista: " << todoList.getName() << endl;
string command;
cout << " / / (_) ___ | |_ __ _ __| | ___ (_) ___ ___ _ __ ___ __ _ _ __ __| |(_) \n"
" / / | |/ __|| __|/ _` | / _` | / _ \\| | / __|/ _ \\ | '_ ` _ \\ / _` || '_ \\ / _` || |\n"
"/ /___| |\\__ \\| |_| (_| | | (_| || __/| | | (__| (_) || | | | | || (_| || | | || (_| || | \n"
"\\____/|_||___/ \\__|\\__,_| \\__,_| \\___||_| \\___|\\___/ |_| |_| |_| \\__,_||_| |_| \\__,_||_|\n"
"- add-task: per aggiungere un nuovo task\n"
"- list: per visualizzare la lista completa di task\n"
"- listcompleted: per visualizzare la lista delle task completate\n"
"- listuncompleted: per visualizzare la lista delle task non completate\n"
"- important: per impostare un task come importante\n"
"- notimportant: per impostare un task come non importante\n"
"- completed: per impostare un task come terminato\n"
"- remove: per rimuovere un task\n"
"- search: cerca le task che contengono nel titolo o nella descrizione una certa parola\n"
"- edit: per modificare titolo, descrizione e scadenza di un task\n"
"- renamelist: per modificare il nome della todolist\n"
"- switch: per passare da una todolist ad un'altra\n"
"- quit: per interrompere la sessione"<<endl<<endl;
while (true) {
const auto& tasks = todoList.getAllTasks();
cout << "inserisci il comando che vuoi eseguire" << endl;
cin >> command;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (command == "add-task") {
string title, description, expirationDate;
cout << "Titolo task: ";
getline(cin, title); // Legge intera riga, utile se ci sono spazi
cout << "Descrizione task: ";
getline(cin, description);
cout << "Data di scadenza (formato DD-MM-YYYY, premi INVIO per lasciare vuoto): " << endl;
getline(cin, expirationDate);
char importantChoice;
cout << "Segnare come importante? (s/n): ";
cin >> importantChoice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
bool important = (importantChoice == 's' || importantChoice == 'S');
todoList.addTask(Task(title, description, important, expirationDate));
todoList.saveToFile(fileName);
}
else if (command == "list") {
bool found = false;
for (const auto& task : tasks) {
cout << task.toString() << endl;
found = true;
}
if(found) {
cout << "Task completate: " << todoList.getCompletedCount() << endl;
cout << "Task non completate: " << todoList.getUncompletedCount() << endl;
}
else
cout << "Nessuna task presente. Digita add-task per aggiungerne una nuova" << endl;
}
else if(command == "listcompleted") {
vector<Task> completed = todoList.getCompletedTasks();
if (!completed.empty()) {
cout << "task completate:" << endl;
for (const auto &task: completed) {
cout << "- " << task.toString() << endl;
}
}
else{
cout<<"non ci sono task completate"<<endl;
}
}
else if(command == "listuncompleted"){
vector<Task> uncompleted = todoList.getUncompletedTasks();
if (!uncompleted.empty()) {
cout << "task non completate:" << endl;
for (const auto &task: uncompleted) {
cout << "- " << task.toString() << endl;
}
}
else{
cout<<"non ci sono task non completate"<<endl;
}
}
else if (command == "important") {
string title;
cout << "Titolo task da impostare come importante: " << endl;
getline(cin, title);
todoList.markTaskImportant(title);
todoList.saveToFile(fileName);
}
else if (command == "notimportant") {
string title;
cout << "Titolo task da impostare come non importante: " << endl;
getline(cin, title);
todoList.markTaskNotImportant(title);
todoList.saveToFile(fileName);
}
else if (command == "completed") {
string title;
cout << "Titolo task da completare: " << endl;
getline(cin, title);
todoList.markTaskComplete(title);
todoList.saveToFile(fileName);
}
else if (command == "remove") {
string title;
cout << "Titolo task da rimuovere: " << endl;
getline(cin, title);
todoList.removeTask(title);
todoList.saveToFile(fileName);
}
else if (command == "edit") {
string oldTitle;
cout << "Titolo della task da modificare: ";
getline(cin, oldTitle);
Task* taskToEdit = todoList.getTask(oldTitle);
if (!taskToEdit) {
cout << "Nessuna task trovata con questo titolo." << endl;
} else {
string newTitle, newDescription, newDate;
cout << "Nuovo titolo: ";
getline(cin, newTitle);
cout << "Nuova descrizione: ";
getline(cin, newDescription);
cout << "Nuova data di scadenza (DD-MM-AAAA, o premi INVIO per lasciare vuoto): ";
getline(cin, newDate);
try {
todoList.editTask(oldTitle, newTitle, newDescription, newDate);
todoList.saveToFile(fileName);
cout << "Task modificata con successo!" << endl;
} catch (const invalid_argument& e) {
cout << "Errore: " << e.what() << endl;
}
}
}
else if (command == "search") {
cout << "Inserisci parola chiave: ";
string keyword;
getline(cin, keyword);
vector<Task> risultati = todoList.searchTasks(keyword);
if (risultati.empty()) {
cout << "Nessuna task trovata con la parola chiave: " << keyword << endl;
} else {
cout << "Task trovate:" << endl;
for (const auto &task: risultati) {
cout << "- " << task.getTitle() << " (" << task.getDescription() << ")" << endl;
}
}
}
else if (command == "rename") {
string newName;
cout << "Nuovo nome per la lista: ";
getline(cin, newName);
if (fs::exists(fileName)) {
string newFileName = basePath + newName + ".txt";
fs::rename(fileName, newFileName); // <-- Rinomina il file
cout << "Lista rinominata da \"" << listName << "\" a \"" << newName << "\"" << endl;
listName = newName;
fileName = newFileName;
todoList.setName(newName);
} else {
cout << "Errore: il file originale non esiste, impossibile rinominare." << endl;
}
}
else if (command == "switch") {
string newListName;
cout << "Inserisci il nome della lista a cui passare: ";
getline(cin, newListName);
string newFileName = basePath + newListName + ".txt";
todoList.saveToFile(fileName);
listName = newListName;
fileName = newFileName;
todoList = TodoList(newListName);
todoList.loadFromFile(fileName);
cout << "Sei passato alla lista: " << listName << endl;
}
else if (command == "quit") {
break;
}
cout << "\n";
}
return 0;
}