-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountController.cpp
More file actions
177 lines (161 loc) · 4.25 KB
/
Copy pathAccountController.cpp
File metadata and controls
177 lines (161 loc) · 4.25 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
#include "AccountController.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
AccountController::AccountController() {
fileRead(userData);
}
//Check if the given name exists.
bool AccountController::isId(std::string name) {
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
return true;
}
}
return false;
}
//Check if the given password is matched to the given name.
bool AccountController::isPwMatched(std::string name, std::string pw) {
std::string temp;
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
temp = it->second.password;
}
}
if (pw == temp) return true;
else return false;
}
//Check if the given recovery phase is matched to the given name.
bool AccountController::isRpMatched(std::string name, std::string rp) {
std::string temp;
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
temp = it->second.recoveryPhase;
}
}
if (rp == temp) return true;
else return false;
}
std::string AccountController::isType(std::string name) {
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
return it->second.accountType;
}
}
}
//add ID to id list.
void AccountController::setData(std::string ID, personalData PD) {
userData.insert(make_pair(ID, PD));
}
//getter of id size
int AccountController::getSize() {
return userData.size();
}
//getter of password
std::string AccountController::getPW(std::string name) {
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
return it->second.password;
}
}
}
std::string AccountController::getRP(std::string name) {
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == name) {
return it->second.recoveryPhase;
}
}
}
std::string AccountController::getYourPW(std::string ID, std::string RP) {
if (isId(ID)) {
if (isRpMatched(ID, RP)) {
std::string temp;
for (auto it = userData.begin(); it != userData.end(); it++) {
if (it->first == ID) {
temp = it->second.password;
}
}
return "Your password is [ " + temp + " ]";
}
return "Wrong Recovery Phase!";
}
return "No account found.";
}
void AccountController::fileRead(std::map<std::string, personalData>& v) {
std::ifstream fin;
fin.open("./userData.txt");
if (fin) {
userData.clear();
std::string line;
while (!fin.eof()) {
std::getline(fin, line);
std::string id;
personalData data;
std::istringstream iss(line);
std::getline(iss, id, '|');
std::getline(iss, data.password, '|');
std::getline(iss, data.recoveryPhase, '|');
std::getline(iss, data.accountType, '|');
v.insert(std::make_pair(id, data));
}
fin.close();
}
else {
std::ofstream emptyFile("./userData.txt");
emptyFile.close();
}
}
void AccountController::fileWrite(std::map<std::string, personalData>& m) {
std::ofstream fout;
fout.open("./userData.txt");
if (!fout) {
std::cout << "Error!";
return;
}
for (auto it = ++m.begin(); it != m.end(); it++) {
fout << it->first << '|';
fout << it->second.password << '|';
fout << it->second.recoveryPhase << '|';
fout << it->second.accountType << '|' << '\n';
}
fout.close();
}
void AccountController::printUsers() {
fileRead(userData);
int count = 0;
for (auto it = ++userData.begin(); it != userData.end(); it++) {
std::cout << "[ " << ++count << " ] " << it->first << std::endl;
}
}
void AccountController::saveAll() {
std::ofstream fout;
fout.open("./userData.txt");
if (!fout) {
std::cout << "Data loss error!" << std::endl;
exit(0);
}
for (auto it = userData.begin(); it != userData.end(); it++) {
fout << it->first << '|';
fout << it->second.password << '|';
fout << it->second.recoveryPhase << '|';
fout << it->second.accountType << '|' << '\n';
}
fout.close();
}
void AccountController::deleteAcc(std::string name) {
userData.erase(name);
fileWrite(userData);
system("cls");
for (int i = 0; i < 3; i++) {
std::cout << ".";
Sleep(500);
}
std::cout << std::endl;
std::cout << "Successfully deleted." << std::endl;
std::cout << std::endl;
std::cout << "Thank you for using our program." << std::endl;
Sleep(500);
std::cout << "We hope to see you again." << std::endl;
}