-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserManager.cpp
166 lines (130 loc) · 4.57 KB
/
UserManager.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
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
#include "UserManager.h"
#include "HelperFunction.h"
// Constructor
UserManager::UserManager() : last_id(0) {}
// Load data from the database (file)
void UserManager::load_data_base() {
last_id = 0;
user_name_to_UserObject.clear();
HelperFunction helper_function;
std::vector<std::string> lines = helper_function.read_line_from_file("users.txt");
for (const auto& line : lines) {
User user(line);
user_name_to_UserObject[user.get_user_name()] = user;
last_id = std::max(last_id, user.get_user_id());
}
}
// Log in a user
void UserManager::log_in() {
load_data_base();
std::string user_name;
std::string password;
std::cout << "\nUser name: ";
std::cin >> user_name;
std::cout << "\nPassword: ";
std::cin >> password;
current_user.set_user_name(user_name);
current_user.set_password(password);
while (true) {
if (!user_name_to_UserObject.count(current_user.get_user_name())) {
std::cout << "Invalid User name...Try again!.\n";
continue;
}
const User& user_exist = user_name_to_UserObject[current_user.get_user_name()];
if (current_user.get_password() != user_exist.get_password()) {
std::cout << "Invalid Password...Try again!.\n";
continue;
}
current_user = user_exist;
break;
}
}
// Sign up a new user
void UserManager::sign_up() {
std::string user_name;
std::string password;
std::string name;
std::string email;
int allow_aq;
while (true) {
std::cout << "\nYour User name: ";
std::cin >> user_name;
if (user_name_to_UserObject.count(user_name))
std::cout << "This user already exists.";
else
break;
}
std::cout << "\nYour Password: ";
std::cin >> password;
std::cout << "\nYour Name: ";
std::cin >> name;
std::cout << "\nYour Email: ";
std::cin >> email;
std::cout << "\nAre you allow anonymous questions? [1 for yes and 0 for no]: ";
std::cin >> allow_aq;
current_user.set_user_name(user_name);
current_user.set_password(password);
current_user.set_name(name);
current_user.set_email(email);
current_user.set_allow_anonymous_questions(allow_aq);
current_user.set_user_id(++last_id);
user_name_to_UserObject[current_user.get_user_name()] = current_user;
update_data_base(current_user);
}
void UserManager::reset_to_questions(const std::vector<int>& to_questions) {
current_user.reset_to_questions(to_questions);
}
void UserManager::reset_from_questions(const std::vector<std::pair<int, int>>& to_questions) {
current_user.reset_from_questions(to_questions);
}
// List user names and IDs
void UserManager::list_user_names_ids() {
std::cout << "\n------------------------------------\n";
for (const auto& pair : user_name_to_UserObject)
std::cout << "ID: " << pair.second.get_user_id() << "\t\tName: " << pair.second.get_name() << '\n';
std::cout << "\n------------------------------------\n";
}
// Read user ID
std::pair<int, int>& UserManager::read_user_id() {
int user_id;
std::cout << "\nEnter User ID or -1 to cancel: ";
std::cin >> user_id;
if (user_id == -1) {
std::pair<int, int> p = std::make_pair(user_id, user_id);
return p;
}
for (const auto& pair : user_name_to_UserObject) {
if (pair.second.get_user_id() == user_id) {
std::pair<int, int> p = std::make_pair(user_id, pair.second.get_allow_anonymous_questions());
return p;
}
}
std::cout << "Invalid User ID. Try again\n";
return read_user_id();
}
// Update database with user information
void UserManager::update_data_base(User& user) {
HelperFunction helper_function;
std::string line = user.to_string();
std::vector<std::string> lines(1, line);
helper_function.write_line_on_file("users.txt", lines, true);
}
// Getters and setters
std::map<std::string, User> UserManager::get_user_name_to_UserObject() const {
return user_name_to_UserObject;
}
void UserManager::set_user_name_to_UserObject(std::map<std::string, User>& mp) {
user_name_to_UserObject = mp;
}
User& UserManager::get_current_user() {
return current_user;
}
void UserManager::set_current_user(User& user) {
current_user = user;
}
int UserManager::get_last_id() const {
return last_id;
}
void UserManager::set_last_id(int& id) {
last_id = id;
}