-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManager.cpp
More file actions
385 lines (300 loc) · 8.45 KB
/
StudentManager.cpp
File metadata and controls
385 lines (300 loc) · 8.45 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
#include "StudentManager.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <sstream>
#include "json.hpp"
using json = nlohmann::json;
// ================== UTILS ==================
std::string toLower(std::string data) {
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c) { return std::tolower(c); });
return data;
}
const std::string WHITESPACE = " \n\r\t\f\v";
std::string ltrim(const std::string& s) {
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string& s) {
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string& s) {
return rtrim(ltrim(s));
}
// ================== HISTORY ==================
void StudentManager::saveAction() {
history.push_back(students);
redoStack.clear();
}
void StudentManager::undo() {
if (history.empty()) {
std::cout << "Nothing to undo\n";
return;
}
redoStack.push_back(students);
students = history.back();
history.pop_back();
rebuildIndex();
std::cout << "Undo successful\n";
}
void StudentManager::redo() {
if (redoStack.empty()) {
std::cout << "Nothing to redo\n";
return;
}
history.push_back(students);
students = redoStack.back();
redoStack.pop_back();
rebuildIndex();
std::cout << "Redo successful\n";
}
// ================== CORE ==================
void StudentManager::addStudent(const Student& s) {
if (index.count(s.getId())) {
std::cout << "ID already exists\n";
return;
}
saveAction();
students.push_back(s);
index[s.getId()] = students.size() - 1;
}
void StudentManager::showAll() const {
for (const auto& s : students) {
s.display();
}
}
Student* StudentManager::getStudentById(int id) {
auto it = index.find(id);
if (it == index.end()) return nullptr;
return &students[it->second];
}
// ================== DELETE ==================
void StudentManager::deleteStudentById(int id) {
if (!index.count(id)) {
std::cout << "Student not found\n";
return;
}
saveAction();
int idx = index[id];
int lastIdx = students.size() - 1;
if (idx != lastIdx) {
students[idx] = students[lastIdx];
index[students[idx].getId()] = idx;
}
students.pop_back();
index.erase(id);
std::cout << "Student deleted\n";
}
// ================== SEARCH ==================
void StudentManager::findStudentsByField(Field field, const std::string& value) {
std::string searchVal = toLower(value);
bool found = false;
for (auto& s : students) {
std::string fieldVal =
(field == NAME) ? s.getName() : s.getSurname();
if (toLower(fieldVal).find(trim(searchVal)) != std::string::npos) {
s.display();
found = true;
}
}
if (!found) std::cout << "No students found\n";
}
// ================== FILE ==================
void StudentManager::saveToFile(const std::string& filename) {
std::ofstream file(filename);
if (!file) {
std::cout << "File error\n";
return;
}
json j = json::array();
for (const auto& s : students) {
j.push_back({
{"id", s.getId()},
{"name", s.getName()},
{"surname", s.getSurname()},
{"age", s.getAge()},
{"gpa", s.getGpa()}
});
}
file << j.dump(4);
}
void StudentManager::loadFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
std::cout << "File error\n";
return;
}
if (!students.empty()) saveAction();
json j;
if (file.peek() == std::ifstream::traits_type::eof()) {
return;
}
try {
file >> j;
} catch (...) {
std::cout << "Invalid JSON format\n";
return;
}
students.clear();
index.clear();
for (const auto& item : j) {
Student s;
s.setData(
item.value("id", 0),
item.value("name", ""),
item.value("surname", ""),
item.value("age", 0),
item.value("gpa", 0.0f)
);
students.push_back(s);
}
rebuildIndex();
}
// ================== SORT ==================
void StudentManager::sortStudentsByField(Field field) {
saveAction();
std::sort(students.begin(), students.end(),
[field](const Student& a, const Student& b) {
switch (field) {
case ID: return a.getId() < b.getId();
case GPA: return a.getGpa() > b.getGpa();
case AGE: return a.getAge() > b.getAge();
case NAME: return a.getName() < b.getName();
case SURNAME: return a.getSurname() < b.getSurname();
}
return false;
});
rebuildIndex();
}
// ================== UPDATE ==================
void StudentManager::updateStudent(Student* student) {
std::string input;
int currentIndex = index[student->getId()];
bool changed = false;
bool saved = false;
auto saveOnce = [&]() {
if (!saved) {
saveAction();
saved = true;
}
};
// ID
std::cout << "Enter new id (>0, or skip): ";
std::getline(std::cin, input);
if (!input.empty()) {
try {
int id = std::stoi(input);
if (id <= 0 || (index.count(id) && id != student->getId())) {
std::cout << "Invalid ID\n";
return;
}
if (id != student->getId()) {
saveOnce();
index.erase(student->getId());
student->setId(id);
index[id] = currentIndex;
changed = true;
}
} catch (...) {
std::cout << "Invalid ID\n";
return;
}
}
// NAME
std::cout << "Enter new name: ";
std::getline(std::cin, input);
if (!input.empty()) {
saveOnce();
student->setName(input);
changed = true;
}
// SURNAME
std::cout << "Enter new surname: ";
std::getline(std::cin, input);
if (!input.empty()) {
saveOnce();
student->setSurname(input);
changed = true;
}
// AGE
std::cout << "Enter new age: ";
std::getline(std::cin, input);
if (!input.empty()) {
try {
int age = std::stoi(input);
if (age <= 0) throw;
saveOnce();
student->setAge(age);
changed = true;
} catch (...) {
std::cout << "Invalid age\n";
return;
}
}
// GPA
std::cout << "Enter new gpa: ";
std::getline(std::cin, input);
if (!input.empty()) {
try {
float gpa = std::stof(input);
if (gpa < 0 || gpa > 5) throw;
saveOnce();
student->setGpa(gpa);
changed = true;
} catch (...) {
std::cout << "Invalid GPA\n";
return;
}
}
if (changed)
std::cout << "Updated\n";
else
std::cout << "No changes made\n";
}
// ================== FILTER ==================
void StudentManager::filterByGPA(float min) {
bool found = false;
for (auto& s : students) {
if (s.getGpa() >= min) {
s.display();
found = true;
}
}
if (!found) std::cout << "No students found\n";
}
void StudentManager::filterByAge(int min, int max) {
bool found = false;
for (auto& s : students) {
if (s.getAge() >= min && s.getAge() <= max) {
s.display();
found = true;
}
}
if (!found) std::cout << "No students found\n";
}
// ================== FULL SEARCH ==================
void StudentManager::searchStudent(const std::string& query) {
std::stringstream ss(query);
std::string word;
std::vector<std::string> words;
while (ss >> word) {
words.push_back(toLower(word));
}
if (words.empty()) {
std::cout << "Empty query\n";
return;
}
for (auto& s : students) {
std::string full = toLower(s.getName() + " " + s.getSurname());
bool match = true;
for (auto& w : words) {
if (full.find(w) == std::string::npos) {
match = false;
break;
}
}
if (match) s.display();
}
}