-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.hpp
602 lines (524 loc) · 21.9 KB
/
Student.hpp
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#ifndef STUDENT_HPP_
#define STUDENT_HPP_
#include "ConfigManager.hpp"
#include "Logger.hpp"
// Forward declaration
class Student;
// Lớp cơ sở cho Sinh viên
class Student {
public:
// Constructor
Student(std::string id, std::string name, std::string dob, std::string gender,
std::string faculty, std::string course, std::string program, std::string address,
std::string email, std::string phone, std::string status) :
id_(id), name_(name), dob_(dob), gender_(gender), faculty_(faculty),
course_(course), program_(program), address_(address), email_(email),
phone_(phone), status_(status) {}
// Phương thức ảo để hiển thị thông tin sinh viên (cho mục đích kế thừa)
virtual void displayInfo() const {
std::cout << "MSSV: " << id_ << "\n";
std::cout << "Họ tên: " << name_ << "\n";
std::cout << "Ngày sinh: " << dob_ << "\n";
std::cout << "Giới tính: " << gender_ << "\n";
std::cout << "Khoa: " << faculty_ << "\n";
std::cout << "Khóa: " << course_ << "\n";
std::cout << "Chương trình: " << program_ << "\n";
std::cout << "Địa chỉ: " << address_ << "\n";
std::cout << "Email: " << email_ << "\n";
std::cout << "Số điện thoại: " << phone_ << "\n";
std::cout << "Tình trạng: " << status_ << "\n";
}
// Getter methods
std::string getId() const { return id_; }
std::string getName() const { return name_; }
std::string getDob() const { return dob_; }
std::string getAddress() const { return address_; }
std::string getGender() const { return gender_; }
std::string getCourse() const { return course_; }
std::string getProgram() const { return program_; }
std::string getEmail() const { return email_; }
std::string getPhone() const { return phone_; }
std::string getStatus() const { return status_; }
std::string getFaculty() const { return faculty_; }
// Setter methods
void setId(const std::string& id) { id_ = id; }
void setName(const std::string& name) { name_ = name; }
void setDob(const std::string& dob) { dob_ = dob; }
void setGender(const std::string& gender) { gender_ = gender; }
void setFaculty(const std::string& faculty) { faculty_ = faculty; }
void setCourse(const std::string& course) { course_ = course; }
void setProgram(const std::string& program) { program_ = program; }
void setAddress(const std::string& address) { address_ = address; }
void setEmail(const std::string& email) { email_ = email; }
void setPhone(const std::string& phone) { phone_ = phone; }
void setStatus(const std::string& status) { status_ = status; }
// Method to serialize Student object to JSON
json toJson() const {
return {
{"id", id_},
{"name", name_},
{"dob", dob_},
{"gender", gender_},
{"faculty", faculty_},
{"course", course_},
{"program", program_},
{"address", address_},
{"email", email_},
{"phone", phone_},
{"status", status_}
};
}
// Static method to create Student object from JSON
static Student fromJson(const json& j) {
return Student(
j["id"].get<std::string>(),
j["name"].get<std::string>(),
j["dob"].get<std::string>(),
j["gender"].get<std::string>(),
j["faculty"].get<std::string>(),
j["course"].get<std::string>(),
j["program"].get<std::string>(),
j["address"].get<std::string>(),
j["email"].get<std::string>(),
j["phone"].get<std::string>(),
j["status"].get<std::string>()
);
}
private:
std::string id_;
std::string name_;
std::string dob_;
std::string gender_;
std::string faculty_;
std::string course_;
std::string program_;
std::string address_;
std::string email_;
std::string phone_;
std::string status_;
};
// Interface cho việc xác thực dữ liệu sinh viên
class StudentValidator {
public:
virtual bool isValid(const Student& student) = 0;
virtual ~StudentValidator() = default;
};
class StudentRepository {
public:
static StudentRepository& getInstance() {
static StudentRepository instance;
return instance;
}
bool isStudentIdExists(const std::string& id) const {
return std::any_of(students_.begin(), students_.end(),
[&](const Student& student) { return student.getId() == id; });
}
std::string getSafeInput(const std::string& prompt) {
std::string input;
std::cout << prompt;
std::getline(std::cin >> std::ws, input); // Read with leading whitespace skipped
return input;
}
void addStudent(const Student& student) {
// Kiểm tra xem MSSV đã tồn tại hay chưa
for (const auto& existingStudent : students_) {
if (existingStudent.getId() == student.getId()) {
std::cout << "Lỗi: MSSV " << student.getId() << " đã tồn tại!\n";
Logger::getInstance().log("Failed to add student - ID already exists: " + student.getId()); // Log lỗi
return;
}
}
if (validator_ == nullptr) {
std::cerr << "Validator chưa được thiết lập!\n";
return;
}
if (validator_->isValid(student)) {
students_.push_back(student);
saveStudentDataToFile();
std::cout << "Đã thêm sinh viên thành công.\n";
Logger::getInstance().log("Added student with ID: " + student.getId()); // Log thành công
} else {
std::cout << "Không thể thêm sinh viên do thông tin không hợp lệ.\n";
Logger::getInstance().log("Failed to add student due to invalid information."); // Log lỗi
}
}
void removeStudent(const std::string& id) {
auto it = std::remove_if(students_.begin(), students_.end(),
[&](const Student& s) { return s.getId() == id; });
if (it != students_.end()) {
students_.erase(it, students_.end());
saveStudentDataToFile();
std::cout << "Đã xóa sinh viên thành công.\n";
Logger::getInstance().log("Removed student with ID: " + id); // Log the action
}
else {
std::cout << "Không tìm thấy sinh viên với ID này.\n";
Logger::getInstance().log("Attempted to remove student with ID: " + id + " but not found."); // Log the action
}
}
Student* findStudent(const std::string& id) {
for (Student& student : students_) {
if (student.getId() == id) {
return &student;
}
}
return nullptr;
}
std::vector<Student> searchStudents(const std::string& faculty, const std::string& name = "") {
std::vector<Student> results;
for (const Student& student : students_) {
if (student.getFaculty() == faculty) {
if (name.empty() || (student.getName().find(name) != std::string::npos)) {
results.push_back(student);
}
}
}
return results;
}
void setValidator(StudentValidator* validator) {
delete validator_;
validator_ = validator;
}
// Method to get all students as a vector of vectors of strings
std::vector<std::vector<std::string>> getAllStudentsAsStrings() const {
std::vector<std::vector<std::string>> studentStrings;
for (const auto& student : students_) {
std::vector<std::string> studentData;
studentData.push_back(student.getId());
studentData.push_back(student.getName());
studentData.push_back(student.getDob());
studentData.push_back(student.getGender());
studentData.push_back(student.getFaculty());
studentData.push_back(student.getCourse());
studentData.push_back(student.getProgram());
studentData.push_back(student.getAddress());
studentData.push_back(student.getEmail());
studentData.push_back(student.getPhone());
studentData.push_back(student.getStatus());
studentStrings.push_back(studentData);
}
return studentStrings;
}
// Method to import students from a vector of vectors of strings
void importStudentsFromStrings(const std::vector<std::vector<std::string>>& studentStrings) {
bool flag = true;
for (const auto& studentData : studentStrings) {
if (studentData.size() == 11) {
Student newStudent(
studentData[0], // id
studentData[1], // name
studentData[2], // dob
studentData[3], // gender
studentData[4], // faculty
studentData[5], // course
studentData[6], // program
studentData[7], // address
studentData[8], // email
studentData[9], // phone
studentData[10] // status
);
if (validator_->isValid(newStudent)) {
students_.push_back(newStudent);
} else {
flag = false;
std::cout << "Thông tin sinh viên không hợp lệ: " << studentData[0] << std::endl;
}
} else {
std::cout << "Dữ liệu không hợp lệ, bỏ qua sinh viên." << std::endl;
}
}
if (flag == true) std::cout << "Nhập dữ liệu thành công" << std::endl;
saveStudentDataToFile();
}
~StudentRepository() {
delete validator_;
}
void loadStudentDataFromFile() {
std::ifstream file(studentFilename_);
if (file.is_open()) {
json j;
file >> j;
for (auto& item : j) {
students_.push_back(Student::fromJson(item));
}
file.close();
Logger::getInstance().log("Loaded student data from file.");
}
else {
std::cout << "Không thể mở file để đọc dữ liệu. Tạo file mới.\n";
Logger::getInstance().log("Could not open file to load data. Creating new file.");
}
}
void saveStudentDataToFile() {
json j = json::array();
for (const auto& student : students_) {
j.push_back(student.toJson());
}
std::ofstream file(studentFilename_);
file << std::setw(4) << j << std::endl;
file.close();
Logger::getInstance().log("Saved student data to file.");
}
void displayAllStudents() {
std::cout << "\n--- Danh sách sinh viên ---" << std::endl;
if (students_.empty()) {
std::cout << "Danh sách trống.\n";
return;
}
for (const Student& student : students_) {
student.displayInfo();
std::cout << "----------\n";
}
}
//-----------------------------------------------------------------------
// Faculty Management
//-----------------------------------------------------------------------
void addFaculty(const std::string& faculty) {
if (!isValidFaculty(faculty)) {
faculties_.push_back(faculty);
std::cout << "Đã thêm khoa mới: " << faculty << ".\n";
saveDataToFile(facultyFilename_, faculties_);
} else {
std::cout << "Khoa này đã tồn tại.\n";
}
}
void renameFaculty(const std::string& oldFaculty, const std::string& newFaculty) {
if (isValidFaculty(newFaculty)) {
std::cout << "Tên khoa mới đã tồn tại.\n";
return;
}
bool found = false;
for (auto& student : students_) {
if (student.getFaculty() == oldFaculty) {
student.setFaculty(newFaculty);
found = true;
}
}
if (found) {
// Update the faculty list
std::replace(faculties_.begin(), faculties_.end(), oldFaculty, newFaculty);
std::cout << "Đã đổi tên khoa " << oldFaculty << " thành " << newFaculty << ".\n";
saveStudentDataToFile(); // Update student data because faculty has been changed
saveDataToFile(facultyFilename_, faculties_);
} else {
std::cout << "Không tìm thấy khoa " << oldFaculty << ".\n";
}
}
bool isValidFaculty(const std::string& faculty) const {
return std::find(faculties_.begin(), faculties_.end(), faculty) != faculties_.end();
}
void displayFaculties() const {
std::cout << "\n--- Danh sách các Khoa ---" << std::endl;
for (const auto& faculty : faculties_) {
std::cout << faculty << std::endl;
}
}
//-----------------------------------------------------------------------
// Status Management
//-----------------------------------------------------------------------
void addStatus(const std::string& status) {
if (!isValidStatus(status)) {
statuses_.push_back(status);
std::cout << "Đã thêm tình trạng mới: " << status << ".\n";
saveDataToFile(statusFilename_, statuses_);
} else {
std::cout << "Tình trạng này đã tồn tại.\n";
}
}
void renameStatus(const std::string& oldStatus, const std::string& newStatus) {
if (isValidStatus(newStatus)) {
std::cout << "Tên tình trạng mới đã tồn tại.\n";
return;
}
bool found = false;
for (auto& student : students_) {
if (student.getStatus() == oldStatus) {
student.setStatus(newStatus);
found = true;
}
}
if (found) {
// Update the status list
std::replace(statuses_.begin(), statuses_.end(), oldStatus, newStatus);
std::cout << "Đã đổi tên tình trạng " << oldStatus << " thành " << newStatus << ".\n";
saveStudentDataToFile(); // Update student data because status has been changed
saveDataToFile(statusFilename_, statuses_);
} else {
std::cout << "Không tìm thấy tình trạng " << oldStatus << ".\n";
}
}
bool isValidStatus(const std::string& status) const {
return std::find(statuses_.begin(), statuses_.end(), status) != statuses_.end();
}
void displayStatuses() const {
std::cout << "\n--- Danh sách các Tình trạng ---" << std::endl;
for (const auto& status : statuses_) {
std::cout << status << std::endl;
}
}
//-----------------------------------------------------------------------
// Program Management
//-----------------------------------------------------------------------
void addProgram(const std::string& program) {
if (!isValidProgram(program)) {
programs_.push_back(program);
std::cout << "Đã thêm chương trình mới: " << program << ".\n";
saveDataToFile(programFilename_, programs_);
} else {
std::cout << "Chương trình này đã tồn tại.\n";
}
}
void renameProgram(const std::string& oldProgram, const std::string& newProgram) {
if (isValidProgram(newProgram)) {
std::cout << "Tên chương trình mới đã tồn tại.\n";
return;
}
bool found = false;
for (auto& student : students_) {
if (student.getProgram() == oldProgram) {
student.setProgram(newProgram);
found = true;
}
}
if (found) {
// Update the program list
std::replace(programs_.begin(), programs_.end(), oldProgram, newProgram);
std::cout << "Đã đổi tên chương trình " << oldProgram << " thành " << newProgram << ".\n";
saveStudentDataToFile(); // Update student data because program has been changed
saveDataToFile(programFilename_, programs_); } else {
std::cout << "Không tìm thấy chương trình " << oldProgram << ".\n";
}
}
bool isValidProgram(const std::string& program) const {
return std::find(programs_.begin(), programs_.end(), program) != programs_.end();
}
void displayPrograms() const {
std::cout << "\n--- Danh sách các Chương trình ---" << std::endl;
for (const auto& program : programs_) {
std::cout << program << std::endl;
}
}
private:
StudentRepository() : validator_(nullptr) {
loadStudentDataFromFile();
loadDataFromFile(facultyFilename_, faculties_);
loadDataFromFile(statusFilename_, statuses_);
loadDataFromFile(programFilename_, programs_);
}
StudentRepository(const StudentRepository&) = delete;
StudentRepository& operator=(const StudentRepository&) = delete;
void loadDataFromFile(const std::string& filename, std::vector<std::string>& data) {
std::ifstream file(filename);
if (file.is_open()) {
json j;
file >> j;
data = j.get<std::vector<std::string>>();
} else {
std::cout << "Không thể mở file để đọc dữ liệu " << filename << ". Tạo file mới.\n";
}
}
// Helper function to save data to file
void saveDataToFile(const std::string& filename, const std::vector<std::string>& data) {
json j = data;
std::ofstream file(filename);
file << std::setw(4) << j << std::endl;
file.close();
}
std::vector<Student> students_;
StudentValidator* validator_;
const std::string studentFilename_ = "students.json";
// Filenames for Faculty, Status, and Program
const std::string facultyFilename_ = "faculties.json";
const std::string statusFilename_ = "statuses.json";
const std::string programFilename_ = "programs.json";
// Data Members for Faculty, Status, and Program
std::vector<std::string> faculties_ = {"FL", "FBE", "FJPN", "FFR"}; // Initial values
std::vector<std::string> statuses_ = {"Active", "Graduated", "Leave", "Absent"}; // Initial values
std::vector<std::string> programs_ = {"Advanced Program", "Formal Program", "High Quality Program"}; // Initial values
};
class ConcreteStudentValidator : public StudentValidator {
public:
ConcreteStudentValidator(class StudentRepository* repo) : repo_(repo) {}
bool isValid(const Student& student) override {
if (!isValidEmail(student.getEmail())) {
std::cout << "Email không hợp lệ.\n";
return false;
}
if (!isValidPhone(student.getPhone())) {
std::cout << "Số điện thoại không hợp lệ.\n";
return false;
}
if (!repo_->isValidFaculty(student.getFaculty())) {
std::cout << "Khoa không hợp lệ.\n";
return false;
}
if (!repo_->isValidStatus(student.getStatus())) {
std::cout << "Tình trạng sinh viên không hợp lệ.\n";
return false;
}
if (!isValidGender(student.getGender())) {
std::cout << "Giới tính không hợp lệ. (Male, Female)\n";
return false;
}
if (!repo_->isValidProgram(student.getProgram())) {
std::cout << "Chương trình không hợp lệ. (Advanced Program, Formal Program, High Quality Program)\n";
return false;
}
if (!isValidCourse(student.getCourse())) {
std::cout << "Khóa không hợp lệ. (YYYY)\n";
return false;
}
if (!isValidDOB(student.getDob())) {
std::cout << "Ngày sinh không hợp lệ. (DD/MM/YYYY)\n";
return false;
}
return true;
}
private:
// Kiểm tra email: phải kết thúc với đuôi đã cấu hình
bool isValidEmail(const std::string& email) {
std::string suffix = ConfigManager::getInstance().getEmailSuffix();
if (email.size() < suffix.size()) return false;
return email.compare(email.size() - suffix.size(), suffix.size(), suffix) == 0;
}
std::string escapeRegex(const std::string& str) {
std::string escaped;
for (char c : str) {
if (std::string(".^$|()[]{}*+?\\").find(c) != std::string::npos) {
escaped.push_back('\\');
}
escaped.push_back(c);
}
return escaped;
}
bool isValidPhone(const std::string& phone) {
// Lấy cấu hình phoneRegex từ ConfigManager
std::string phonePattern = ConfigManager::getInstance().getPhoneRegex();
std::cout << "Cấu hình phoneRegex: " << phonePattern << "\n";
// Nếu chuỗi không bắt đầu bằng '^', cho rằng đây là chuỗi literal và escape nó
if (phonePattern.empty() || phonePattern.front() != '^') {
phonePattern = "^" + escapeRegex(phonePattern) + ".*$";
}
try {
std::regex pattern(phonePattern);
return std::regex_match(phone, pattern);
} catch (std::regex_error& e) {
std::cerr << "Regex error: " << e.what() << std::endl;
return false;
}
}
// Các hàm xác thực cũ (gender, course, DOB) giữ nguyên...
bool isValidGender(const std::string& gender) {
return (gender == "Male" || gender == "Female");
}
bool isValidCourse(const std::string& course) {
const std::regex pattern(R"(\d{4})");
return std::regex_match(course, pattern);
}
bool isValidDOB(const std::string& dob) {
const std::regex pattern(R"(\d{2}/\d{2}/\d{4})");
return std::regex_match(dob, pattern);
}
StudentRepository* repo_;
};
#endif // STUDENT_HPP_