-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.cpp
More file actions
134 lines (116 loc) · 4.76 KB
/
Admin.cpp
File metadata and controls
134 lines (116 loc) · 4.76 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
#include "Admin.h"
bool Admin::checkTrueSize(const str& login, int left, int right) const { if (login.size() >= left and login.size() <= right) return true; else return false; }
bool Admin::checkFirstSymbol(const str& login) const { if (login.find_first_of("0123456789") != -1) return false; else return true; }
bool Admin::checkTrueLogin(str& login) const { if (checkTrueSize(login, 3, 16) == true and checkFirstSymbol(login) == true) return true; else return false; }
bool Admin::getIsAdmin() const { return isAdmin; }
bool Admin::checkLogin(const str& login) {
ifstream checkL("Admin.txt"); string tmp; checkL >> tmp; checkL.close();
if (login == uncodeStr(tmp)) return true; return false;
}
bool Admin::checkPassword(const str& password) {
ifstream checkP("Admin.txt"); string tmp; checkP >> tmp >> tmp; checkP.close();
if (password == uncodeStr(tmp)) return true; return false;
}
void Admin::registerAdmin() {
cout << " login: "; str login; cin >> login;
while (checkTrueLogin(login) == false) {
while (checkTrueSize(login, 3, 16) == false) { system("cls"); cerr << " incorect login: size, re-enter correct: "; cin >> login; }
while (checkFirstSymbol(login) == false) { system("cls"); cerr << " incorect login: first must be letter, re-enter correct: "; cin >> login; }
} this->loginAdm = login;
cout << " password: "; str password; cin >> password;
while (checkTrueSize(password, 8, 32) == false) { system("cls"); cerr << " incorrect password: size, re-enter correct: "; cin >> password; } this->passwordAdm = password;
system("cls");
cout << " login: " << loginAdm << "\n";
cout << " password: " << passwordAdm << "\n *save it somewhere\n\n";
isAdmin = true;
saveLPToFile(codeStr(loginAdm), codeStr(passwordAdm));
}
str Admin::codeStr(str& word) {
for (int i = 0; i < word.size(); i++) {
if (word[i] >= char(65) and word[i] <= char(90)) word[i] = char(word[i] + 2);
else if (word[i] >= char(97) and word[i] <= char(111)) word[i] = char(word[i] - 64);
else if (word[i] >= char(112) and word[i] <= char(122)) word[i] = char(word[i] + 2);
} return word;
}
str Admin::uncodeStr(str& word) {
for (int i = 0; i < word.size(); i++) {
if (word[i] >= char(67) and word[i] <= char(92)) word[i] = char(word[i] - 2);
else if (word[i] >= char(33) and word[i] <= char(47)) word[i] = char(word[i] + 64);
else if (word[i] >= char(114) and word[i] <= char(124)) word[i] = char(word[i] - 2);
} return word;
}
void Admin::loginAdmin() {
cout << " login: "; str login; cin >> login;
while (checkLogin(login) == false) { system("cls"); cerr << " incorrect login: not found, re-enter: "; cin >> login; }
system("cls");
cout << " password: "; str password; cin >> password;
for (size_t i = 0; i < 3; i++) {
if (checkPassword(password) == false) { system("cls"); cerr << " incorrect password: not match, re-enter (" << 3 - i << " tries left): "; cin >> password; }
else { adminTools(); break; }
}
}
void Admin::saveLPToFile(const str& login, const str& password) const { ofstream fileIt("Admin.txt"); fileIt << loginAdm << "\n"; fileIt << passwordAdm; fileIt.close(); }
void Admin::addGuest() {
Guest newGuest; newGuest.registerGuest();
guests.push_back(newGuest);
newGuest.load_user_data();
}
void Admin::addGuest(class Guest a) {
guests.push_back(a);
a.load_user_data();
}
void Admin::delGuest() {
if (guests.size() == 0) { system("cls"); cout << " >>> No guests founded <<<\n"; return; }
system("cls");
cout << " >>> Guests <<<\n";
for (int i = 0; i < guests.size(); i++) { cout << " " << i + 1 << "# guest: " << guests[i].getLogin() << "\n"; }
cout << " ?: "; int guestPos; cin >> guestPos;
guests.erase(guests.begin() + guestPos - 1);
}
void Admin::editUsers() {
system("cls");
cout << " >>> Edit users <<<\n";
cout << " 1. Add guest\n 2. Del guest\n 3. Modify guest\n 4. Return\n";
cout << " ?: "; int editChoise; cin >> editChoise;
switch (editChoise) {
case 1:
addGuest();
break;
case 2:
delGuest();
break;
default:
system("cls");
cout << " >>> Edit users <<<\n";
cout << " 1. Add guest\n 2. Del guest\n 3. Modify guest\n 4. Return\n";
cerr << " error: wrong option, re-enter opt: "; cin >> editChoise;
break;
}
}
void Admin::adminTools() {
system("cls");
cout << " >>> Welcome to Admin tools <<<\n";
cout << " 1. Edit users\n 2. Show statistic\n 3. Edit tests\n 4. Log Out\n";
cout << " ?: "; int adminChoise; cin >> adminChoise;
switch (adminChoise) {
case 1:
editUsers();
break;
case 2:
//showStat();
break;
case 3:
//EditQuestion();
break;
case 4:
system("cls");
cout << " >>> Succesfully logged out <<<\n\n";
return;
default:
system("cls");
cout << " >>> Welcome to Admin tools <<<\n";
cout << " 1. Edit users\n 2. Show statistic\n 3. Edit tests\n 4. Log Out\n";
cerr << " error: wrong option, re-enter opt: "; cin >> adminChoise;
break;
}
}