Repository navigation
Expand file tree
/
Copy pathuser.cpp
More file actions
418 lines (337 loc) · 13.9 KB
/
Copy pathuser.cpp
File metadata and controls
418 lines (337 loc) · 13.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "user.h"
#include "transactions.h"
#include<string>
#include<sstream>
int User::id_count = 1000;
std::filesystem::path User::dir_path = "Users";
User::User()
{//Default constructor for when we do a new User(), used for PlaceHolder user IDs
id_count = User::numOfUsers() + 1001;
this->userid = id_count;
this->first_name = "NULL";
this->last_name = "NULL";
this->balance = 0;
}
//Can be used by managers to create a new account by entering all information right away, will still need to run a few setters
//NOT IMPLEMENTED
User::User(std::string& first_name, std::string& last_name, double balance){
id_count++;
this->userid = id_count;
this->first_name = first_name;
this->last_name = last_name;
this->balance = balance;
}
//NOT IMPLEMENTED
User::User(const User& otherUser){//Copy function, will need to prompt to get a username, validate it, and check if its taken
id_count++;
this->userid = id_count;
this->first_name = otherUser.first_name;
this->last_name = otherUser.last_name;
this->balance = otherUser.balance;
}
// Getters
std::string User::getFirstName() const{
return this->first_name;
}
std::string User::getLastName() const{
return this->last_name;
}
std::string User::getPassword() const{
return this->password;
}
int User::getUserID() const{
return this->userid;
}
double User::getBalance() const {
return this->balance;
}
std::string User::getUsername() const{
return this->username;
}
std::string User::getAccountType() const{
return this->account_type;
}
std::string User::getTransactions() const{
return this->transactions;
}
// Setters
void User::setFirstName(const std::string& first_name){
this->first_name = first_name;
}
void User::setLastName(const std::string& last_name){
this->last_name = last_name;
}
void User::setBalance(double balance){
this->balance = balance;
}
void User::setPassword(const std::string& password){
this->password = password;
}
void User::setUsername(const std::string& username){
this->username = username;
}
void User::setAccountType(const std::string& account_type){
this->account_type = account_type;
}
void User::setTransactions(const std::string& transactions){
this->transactions = transactions;
}
bool User::DeleteUser()
{
// Build the path to this user's file: Users/<username>.txt
std::filesystem::path filepath = dir_path / (username + ".txt");
try
{
if (!std::filesystem::exists(filepath))//Check if file exists in the directory
{
std::cout << "Error: No account file found for user '"
<< username << "'." << std::endl;
return false;
}
// Attempt to delete the file
if (!std::filesystem::remove(filepath))
{
std::cout << "Error: Failed to delete account file for user '"
<< username << "'." << std::endl;
return false;
}
std::cout << "Account for user deleted successfully." << std::endl;
return true;
}
catch (const std::filesystem::filesystem_error& e)
{
std::cerr << "Filesystem error while deleting user: "
<< e.what() << std::endl;
return false;
}
}
int User::numOfUsers(){
int count = 0;
if (!std::filesystem::exists(dir_path))
return 0;
for (const auto& file : std::filesystem::directory_iterator(dir_path)) {
if (file.is_regular_file()) {//Counts how many txt files are inside the 'Users' directory to keep a 'real-time' update of UserIDs
count++;
}
}
return count;
}
User* User::GetUser(std::string username)
{
return User::loadUserFromFile(User::dir_path / (username + ".txt"));
}
//Used by Managers to view all Bank Accounts
/* Example of 'listUsers()'
UserID Username Name Balance Account Type
------------------------------------------------------------------------
1011 chrie30 Ethan Christman $85.38 Saving
1012 desidj27 David Desing $52.32 Checking
1013 londj Jack London $112.74 Saving
*/
void User::listUsers()
{
if (!std::filesystem::exists(dir_path))
{
std::cout << "No users found." << std::endl;
return;
}
std::cout << std::left
<< std::setw(10) << "UserID"
<< std::setw(15) << "Username"
<< std::setw(20) << "Name"
<< std::setw(12) << "Balance"
<< std::setw(15) << "Account Type"
<< std::endl;
std::cout << std::string(72, '-') << std::endl;
for (const auto& file : std::filesystem::directory_iterator(dir_path))
{
if (file.is_regular_file())
{
User* user = loadUserFromFile(file.path());
if (user)
{
std::string fullName = user->getFirstName() + " " + user->getLastName();
// format "$123.45" as a single string so setw applies to the whole thing
std::ostringstream balStream;
balStream << std::fixed << std::setprecision(2) << "$" << user->getBalance();
std::string balanceStr = balStream.str();
std::cout << std::left
<< std::setw(10) << user->getUserID()
<< std::setw(15) << user->getUsername()
<< std::setw(20) << fullName
<< std::setw(12) << balanceStr
<< std::setw(15) << user->getAccountType()
<< std::endl;
delete user;
}
}
}
}
//Once user logs in, load their userdata from the file
User* User::loadUserFromFile(const std::filesystem::path& filepath) {
std::ifstream file(filepath);
User* u = new User();
std::string line;
while (std::getline(file, line)) {//Only loads information needed by the BankAccount
if (line.rfind("Account#: ", 0) == 0) {
std::string numberStr = line.substr(10);
try {
u->userid = std::stoi(numberStr);//convert to int
}
catch (const std::exception& e) {
std::cerr << "Error: invalid account number format: " << numberStr << std::endl;
}
}
else if (line.rfind("Username: ", 0) == 0) {
u->setUsername(line.substr(10));
} else if (line.rfind("Password: ", 0) == 0) {
u->setPassword(line.substr(10));
} else if (line.rfind("First: ", 0) == 0) {
u->setFirstName(line.substr(7));
} else if (line.rfind("Last: ", 0) == 0) {
u->setLastName(line.substr(6));
} else if (line.rfind("Balance: ", 0) == 0) {
try {
u->setBalance(std::stod(line.substr(9)));
} catch (...) {
u->setBalance(0);
}
} else if (line.rfind("Account Type: ", 0) == 0) {
u->setAccountType(line.substr(14));
} else if (line.rfind("Transaction History: ", 0) == 0) {
const std::string key = "Transaction History: ";
u->setTransactions(line.substr(key.length()));
}
}
return u;
}
void User::createUserDirectory(){
try {
// Create the directory 'Users' if it doesn't exist
if (std::filesystem::create_directories(dir_path)) {
std::cout << "Users directory created successfully: " << dir_path << std::endl;
}
//Creates the Managers directory, then creates the managers.txt
if(std::filesystem::create_directories("Managers")){
std::ofstream outputFile("Managers/managers.txt");
outputFile.close();
std::cout << "Managers directory and txt created successfully: " << dir_path << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << std::endl;
}
//Initializes the total amount of accounts created
id_count += User::numOfUsers();
}
bool User::isUsernameTaken(std::string& username){
std::filesystem::path target = dir_path / (username + ".txt");//creating a path to the inputted username. EX: "Users\\user_001.txt"
return std::filesystem::exists(target);
}
bool User::isValidUsername(std::string& username){
bool validUsername = false;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');//clears input buffer, aka next input line is actually what information we want
do{
std::cout << "What would you like your Username to be?" << std::endl;
getline(std::cin, username);
if(username.find(' ') != std::string::npos){
std::cout << "Error! Username cannot have a space in it!" << std::endl;
}else if(username.empty()){
std::cout << "Error! Username cannot be empty!" << std::endl;
}else{
validUsername = true;
}
}while(!validUsername);
return true;
}
void User::checkAccountType(std::string& account_type){
while (true) {
if (account_type == "-1") {
return;
}
if (account_type == "checking" || account_type == "saving") {
account_type[0] = std::toupper(account_type[0]);//Changed the first character of the string to an uppercase
return;
}
std::cout << "Error! Account type is invalid! Must be either 'Checking' or 'Saving', or enter -1 to cancel account creation." << std::endl;
std::cout << "Enter account type: ";
std::cin >> account_type;
transform(account_type.begin(), account_type.end(), account_type.begin(), ::tolower);//coverts inputted string to all lowercase
}
}
void User::createAccount(User* newUser){//User account creation. "Returns" the pointer of the created account.
std::string first_name, last_name, username, account_type, password;
double balance;
std::cout << "Enter your First and Last name. <first_name last_name>" << std::endl;
std::cin >> first_name >> last_name;
User::isValidUsername(username);//Checks to see if the username is valid. Aka no spaces
transform(username.begin(), username.end(), username.begin(), ::tolower);//converts the username std::string to all lowercase
//check if username is already in use. If so, tell the user to choose a different username.
while(isUsernameTaken(username)){
std::cout << "Error! Username is already taken. Please enter a new one." << std::endl;
std::cin >> username;
transform(username.begin(), username.end(), username.begin(), ::tolower);
}
std::cout << "Please create a password:" << std::endl;//There are no password requirements as of right now.
std::cin >> password;
std::cout << "What type of account are you creating? <Checking / Saving>" << std::endl;
std::cin >> account_type;
transform(account_type.begin(), account_type.end(), account_type.begin(), ::tolower);
checkAccountType(account_type);//Account type MUST be either 'Checking' or 'Saving'. It is not case sensitive (can be ChECkIng or Checking, or checking, etc.)
if(account_type == "-1"){
std::cout << "Account Creation Cancelled" << std::endl;
return;
}
std::cout << "How much is the initial deposit? <X.XX>" << std::endl;
std::cin >> balance;
//Most banks require a minimum deposite. The one I use has a min of $20
while (std::cin.fail() || balance < 20) {//Check to see if a number was inputted, if not it's a fail()
if (std::cin.fail()) {
std::cin.clear(); //Clears the fail
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');//ignores the next line
std::cout << "Error! Deposit must be a number." << std::endl;
}
else if (balance < 20) {
std::cout << "Sorry, you must deposit at least $20 to open an account!" << std::endl;
}
std::cout << "Enter amount or -1 to cancel: ";
std::cin >> balance;
// If user types -1, cancel account creation
if (!std::cin.fail() && balance == -1) {
std::cout << "Account Creation Cancelled!" << std::endl << std::endl;
return;
}
}
//Saves the user to private data members
newUser->setFirstName(first_name);
newUser->setLastName(last_name);
newUser->setPassword(password);
newUser->setUsername(username);
newUser->setBalance(balance);
newUser->setAccountType(account_type);
// Log the initial deposit into transaction history
Transactions::addInitialDeposit(newUser, balance);
// Write everything (including transaction history) to file
newUser->saveToFile();
std::cout << std::endl << "-- SUCCESS! -- "
<< username << "'s " + account_type
<< " account has been successfully created."
<< std::endl << std::endl;
}
void User::saveToFile() const {
std::filesystem::path filepath = dir_path / (username + ".txt");
std::ofstream out(filepath);
// YOUR ERROR CHECK (RESTORED)
if (!out.is_open()) {
std::cout << "Unable to open the file for writing." << std::endl;
return;
}
out << "Account#: " << userid << std::endl;
out << "Username: " << username << std::endl;
out << "Password: " << password << std::endl;
out << "First: " << first_name << std::endl;
out << "Last: " << last_name << std::endl;
out << "Balance: " << balance << std::endl;
out << "Account Type: " << account_type << std::endl;
out << "Transaction History: " << transactions << std::endl;
out.close();
}