-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
195 lines (158 loc) · 4.55 KB
/
User.cpp
File metadata and controls
195 lines (158 loc) · 4.55 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "User.h"
#include <ctime>
#include <string>
#include <functional>
#include <sstream>
#include <iomanip>
#include <iostream>
#include "HashUtils.h"
using namespace std;
User::User(const string& username, const string& password, bool isAdmin)
: username(username), passwordHash(HashUtils::hashPassword(password)), balance(0.0), isAdmin(isAdmin), isActive(true), failedLoginAttempts(0), isLocked(false) {
}
bool User::login(const string& inputPassword) {
if (!isActive || isLocked) return false;
if (passwordHash == HashUtils::hashPassword(inputPassword)) {
failedLoginAttempts = 0;
return true;
}
else {
failedLoginAttempts++;
if (failedLoginAttempts >= 5) {
isLocked = true;
notifications.push("Your account has been locked due to too many failed login attempts.");
}
return false;
}
}
bool User::changePassword(const string& oldPassword, const string& newPassword) {
if (passwordHash != HashUtils::hashPassword(oldPassword)) {
return false;
}
passwordHash = HashUtils::hashPassword(newPassword);
return true;
}
void User::unlockAccount() {
isLocked = false;
failedLoginAttempts = 0;
}
bool User::getIsLocked() const {
return isLocked;
}
void User::resetFailedLoginAttempts() {
failedLoginAttempts = 0;
}
bool User::sendMoney(double amount) {
if (amount <= 0 || amount > balance) {
return false;
}
balance -= amount;
addTransaction("Sent money", -amount);
return true;
}
void User::receiveMoney(double amount) {
if (amount <= 0) {
return;
}
balance += amount;
addTransaction("Received money", amount);
}
void User::addTransaction(const string& description, double amount) {
transactionHistory.emplace_back(description, amount);
}
string User::getUsername() const {
return username;
}
double User::getBalance() const {
return balance;
}
bool User::getIsAdmin() const {
return isAdmin;
}
bool User::getIsActive() const {
return isActive;
}
const vector<pair<string, double>>& User::getTransactionHistory() const {
return transactionHistory;
}
void User::setBalance(double newBalance) {
if (newBalance >= 0) {
balance = newBalance;
}
}
void User::setActive(bool status) {
isActive = status;
}
void User::addTrustedUser(const string& trusted) {
trustedUsers.insert(trusted);
}
void User::removeTrustedUser(const string& trusted) {
trustedUsers.erase(trusted);
}
bool User::isTrustedUser(const string& user) const {
return trustedUsers.find(user) != trustedUsers.end();
}
void User::addPendingTransaction(const string& desc, double amount) {
pendingTransactions.emplace_back(desc, amount);
}
void User::confirmPendingTransaction() {
if (!pendingTransactions.empty()) {
auto& transaction = pendingTransactions.front();
addTransaction(transaction.first, transaction.second);
balance += transaction.second;
pendingTransactions.pop_front();
}
}
void User::rejectPendingTransaction() {
if (!pendingTransactions.empty()) {
pendingTransactions.pop_front();
}
}
void User::pushRecentAction(const string& action, double amount) {
recentActions.push(make_pair(action, amount));
}
bool User::undoLastAction() {
if (recentActions.empty()) {
return false;
}
auto lastAction = recentActions.top();
recentActions.pop();
// Reverse the last action's effect on balance
balance -= lastAction.second;
addTransaction("Undo: " + lastAction.first, -lastAction.second);
return true;
}
void User::addNotification(const string& message) {
notifications.push(message);
}
vector<string> User::getNotifications() {
vector<string> result;
while (!notifications.empty()) {
result.push_back(notifications.front());
notifications.pop();
}
return result;
}
const list<pair<string, double>>& User::getPendingTransactions() const {
return pendingTransactions;
}
vector<pair<string, double>> User::getRecentActions(int count) const {
vector<pair<string, double>> result;
stack<pair<string, double>> temp = recentActions;
while (!temp.empty() && count > 0) {
result.push_back(temp.top());
temp.pop();
count--;
}
return result;
}
vector<string> User::getTrustedUsers() const {
vector<string> result(trustedUsers.begin(), trustedUsers.end());
return result;
}
void User::setUsername(const string& newUsername) {
username = newUsername;
}
void User::setPasswordHash(const string& hash) {
passwordHash = hash;
}