-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHethong.cpp
97 lines (81 loc) · 2.64 KB
/
Hethong.cpp
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
#include "Hethong.h"
// Constructor
Hethong::Hethong() : head(nullptr) {}
// Destructor
Hethong::~Hethong() {
while (head != nullptr) {
User* temp = head;
head = head->next;
delete temp;
}
}
void Hethong::napDuLieuSV(const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Không thể mở file:" << filePath;
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(":");
if (fields.size() != 4) continue;
QString maSV = fields[0];
QString password = fields[1];
QString hoten = fields[2];
QString phai = fields[3];
User* newUser = new User(maSV, hoten, phai, password);
newUser->next = head;
head = newUser;
}
file.close();
qDebug() << "Nạp dữ liệu từ file thành công.";
}
void Hethong::ghiDuLieuSV(const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Không thể mở file để ghi:" << filePath;
return;
}
QTextStream out(&file);
User* current = head;
while (current != nullptr) {
out << current->maSV << ":" << current->password << ":" << current->hoten << ":" << current->phai << "\n";
current = current->next;
}
file.close();
qDebug() << "Ghi dữ liệu ra file thành công.";
}
User* Hethong::timKiemSinhVien(const QString& maSV) {
User* current = head;
while (current != nullptr) {
if (current->maSV == maSV) {
return current;
}
current = current->next;
}
return nullptr;
}
QString Hethong::dangNhap(const QString& enteredMaSV, const QString& enteredPassword) {
if (enteredMaSV == "GV" && enteredPassword == "GV") {
qDebug() << "Đăng nhập thành công với vai trò Giáo Viên!";
return "Giáo viên";
}
User* user = timKiemSinhVien(enteredMaSV);
if (user && user->password == enteredPassword) {
qDebug() << "Đăng nhập thành công với vai trò Sinh Viên!";
return "Sinh viên";
}
qDebug() << "Sai mã sinh viên hoặc mật khẩu.";
return "";
}
bool Hethong::quenPassword(User* user, const QString& newPassword) {
if (user) {
user->password = newPassword;
return true;
}
return false;
}
bool Hethong::kiemTraSinhVienTonTai(const QString& maSV) {
return timKiemSinhVien(maSV) != nullptr;
}