-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaff.cpp
495 lines (442 loc) · 17 KB
/
staff.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "staff.h"
using namespace std;
using namespace utils;
using namespace bankerror;
using json = nlohmann::json;
// ------------ CONSTRUCTOR & DESTRUCTOR --------------
Staff::Staff(const string &id, const string &password)
{
isUserValid = false;
try {
json staff = readData("staff", id);
if (! staff.empty() && staff["password"] == password) // valid inputs
{
string name = staff["name"];
string designation = staff["designation"];
string mobile = staff["mobile"];
string address = staff["address"];
string email = staff["email"];
this->id = id;
this->name = trim(name, "\"");
this->designation = trim(designation, "\"");
this->branch_id = staff["branch_id"];
this->mobile = trim(mobile, "\"");
this->email = trim(email, "\"");
this->address = trim(address, "\"");
this->salary = staff["salary"];
isUserValid = true; // staff is valid
}
} catch (const exception &error){
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
// -------------- STATIC METHODS --------------
Staff* Staff::login(const string &userid, const string &password)
{
try {
unique_ptr<Staff> staff(new Staff(userid, password));
if(staff->isValid()){
return staff.release();
} else {
throw INVALID_USER;
}
}
catch (const ERROR_USER &error) {
cerr << "\n ERROR: " << errmsg::INVALID_USER << endl;
}
catch (const exception & error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
return NULL;
}
// -------------- OTHER METHODS --------------
bool Staff::isValid(){ return this->isUserValid; }
void Staff::displayPanel()
{
if(!isUserValid) throw INVALID_USER;
short choice = 1;
system("clear");
while(choice != 0){
cout << "\n ----------- STAFF PANEL -----------\n" << endl
<< " 1) Display Your Details " << endl
<< " 2) Update Your Info" << endl << endl
<< " 3) Create Bank Account" << endl
<< " 4) Remove Bank Account" << endl
<< " 5) Display Bank Account Details" << endl
<< " 6) Withdraw Money" << endl
<< " 7) Deposite Money" << endl << endl
<< " 8) Add New Account Holder" << endl
<< " 9) Display Account Holder Details" << endl << endl
<< " 0) Logout" << endl;
scanNumber(choice, " => Enter Choice: ");
switch (choice) {
case 0: system("clear");
return;
case 1: this->displayStaffDetails();
getchar();
getc(stdin);
system("clear");
break;
case 2: this->updateAccountDetails();
break;
case 3: this->createBankAccount();
break;
case 4: this->removeBankAccount();
break;
case 5: this->displayBankAccountDetails();
break;
case 6: this->withdraw();
break;
case 7: this->deposit();
break;
case 8: this->createAccountHolder();
break;
case 9: this->displayAccountHolderDetails();
break;
default:
cout << "\n Invalid choice !" << endl;
}
}
}
void Staff::displayStaffDetails()
{
if(!isUserValid) throw INVALID_USER;
cout << "\n\n ================ DETAILS ================\n" << endl;
cout << " Id : " << this->id << endl;
cout << " Name : " << this->name << endl;
cout << " Designation : " << this->designation << endl;
cout << " Branch id : " << this->branch_id << endl;
cout << " Mobile : " << this->mobile << endl;
cout << " Email : " << this->email << endl;
cout << " Address : " << this->address << endl;
cout << " Salary : Rs." << this->salary << endl;
}
void Staff::updateAccountDetails()
{
if(!isUserValid) throw INVALID_USER;
this->displayStaffDetails();
while(1){
cout << "\n ===== SELECT WHAT TO UPDATE =====" << endl;
cout << "\n 1) Name" <<endl;
cout << " 2) Mobile" <<endl;
cout << " 3) Email" <<endl;
cout << " 4) Address" <<endl;
cout << " 5) Password" <<endl;
cout << " 0) Cancel" <<endl;
short choice;
try {
scanNumber(choice, " Enter Choice: ");
string newValue, fieldToUpdate;
json data = readData();
if(choice == 0) return;
else if(choice == 1) fieldToUpdate = "name";
else if(choice == 2) fieldToUpdate = "mobile";
else if(choice == 3) fieldToUpdate = "email";
else if(choice == 4) fieldToUpdate = "address";
else if(choice == 5){ // updating password requires password verification
cout << "\n Enter current password: ";
cin >> newValue;
if(newValue != data["staff"][this->id]["password"]){
cout << "\n => Password is incorrect..." << endl;
continue;
}
fieldToUpdate = "password";
}
else {
cout << "\n => Invalid Choice..." << endl;
continue;
}
// getting new value from user
cin.ignore();
cout << " Enter New "<< fieldToUpdate <<": ";
getline(cin, newValue);
trim(newValue);
// validating inputs
if(newValue == "" ||
(fieldToUpdate=="name" && ! isNameValid(newValue)) ||
(fieldToUpdate=="mobile" && ! isMobileValid(newValue)) ||
(fieldToUpdate=="email" && ! isEmailValid(newValue)) ||
(fieldToUpdate=="password" && ! isPasswordValid(newValue)))
{
continue;
}
// updating data into JSON file as well as into current object
updateData(data, data["staff"][this->id][fieldToUpdate], newValue);
switch (choice) { // updating values in the current object
case 1: this->name = newValue; break;
case 2: this->mobile = newValue; break;
case 3: this->email = newValue; break;
case 4: this->address = newValue; break;
}
} catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
} // end of try
} // end of while
}
void Staff::displayBankAccountDetails()
{
if(!isUserValid) throw INVALID_USER;
long int accountNumber;
try {
scanNumber(accountNumber, "\n => Enter bank account number: ");
Account::displayAccountDetails(accountNumber);
}
catch (const ERROR_BANK_ACCOUNT &error) {
if(error == ACC_NOT_FOUND)
cerr << "\n ERROR: " << errmsg::ACC_NOT_FOUND << endl;
}
catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
void Staff::displayAccountHolderDetails()
{
if(!isUserValid) throw INVALID_USER;
string accountHolderId;
cout << "\n ======== ACCOUNT HOLDER DEATILS ========" << endl;
scanNumber(accountHolderId, " Enter Account Holder Id: ");
try {
json accountHolder = readData("account_holder", accountHolderId);
if(accountHolder.empty()){
throw USER_NOT_FOUND;
} else {
AccountHolder(accountHolderId, accountHolder["password"]).displayDetails();
}
} catch (const ERROR_USER &error) {
if(error == USER_NOT_FOUND)
cout << "\n ERROR: " << errmsg::USER_NOT_FOUND<< endl;
} catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
getchar();
getc(stdin);
system("clear");
}
string Staff::createAccountHolder()
{
if(!isUserValid) throw INVALID_USER;
/// @todo test this function
string name, address, mobile, password, newAccountHolderId = "";
system("clear");
cout << "\n ====== ENTER ACCOUNT HOLDER DETAILS ======" << endl
<< "\n [Enter Zero(0) to cancle]" << endl;
try {
cin.ignore();
do { // until the name is not valid
cout << "\n Name: ";
getline(cin, name);
trim(name);
if(name == "0") // operation cancel
throw -1;
} while (!isNameValid(name));
do { // until the mobile is not valid
cout << "\n Mobile Number: ";
getline(cin, mobile);
trim(mobile);
if(mobile == "0") // operation cancel
throw -1;
} while (!isMobileValid(mobile));
do { // until the address is not valid
cout << "\n Address: ";
getline(cin, address);
trim(address);
if(address == "0") // operation cancel
throw -1;
} while (!isAddressValid(address));
do { // until the address is not valid
cout << "\n Password: ";
getline(cin, password);
trim(password);
if(password == "0")
throw -1;
} while (!isPasswordValid(password));
newAccountHolderId = AccountHolder::createAccountHolder(name, mobile, address, password);
cout << "\n => Account Holder added Successfully" << endl;
AccountHolder(newAccountHolderId, password).displayDetails();
} catch (const int &error){ // operation cancel
cerr << "\n => OPERATION CANCELED: Add new account holder" << endl;
} catch (const char* error) {
cerr << error;
} catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
getc(stdin);
system("clear");
return newAccountHolderId;
}
void Staff::createBankAccount()
{
if(!isUserValid) throw INVALID_USER;
unsigned long int balance;
short choice;
string accountType, accountHolderId;
try {
// ---------- Getting Account Holder Id ----------
while(1){ // iterate until valid account holder id is entered
cout << " => Enter account holder id for new bank account: ";
cin >> accountHolderId;
json accountHolder = readData("account_holder", accountHolderId);
// if account holder not found
if(accountHolder.empty()){
while(1){
cout << "\n ============ Account Holder Not Found ============" << endl
<< "\n 1) Add new Account Holder"
<< "\n 2) Re-enter Account Holder Id "
<< "\n 3) Cancel";
scanNumber(choice, "\n Enter choice: ");
if(choice == 1){
accountHolderId = this->createAccountHolder();
if(accountHolderId.empty()){ // if account holder is not created
cout << "\n ERROR: ACCOUNR HOLDER IS NOT CREATED PROPERLY ! TRY AGAIN..." << endl;
getc(stdin);
continue;
}
break;
} else if(choice == 2){
break;
} else if(choice == 3){
return;
} else {
cout << "\n => ERROR: invalid choice entered..." << endl;
}
}
}
else { // valid account holder is found
break;
}
}
// ---------- Getting Account Type ----------
while(1){
cout << "\n => ACCOUNT TYPE <=" << endl
<< "\n 1) Savings"
"\n 2) Current"
"\n 0) Cancle";
scanNumber(choice, "\n Enter choice: ");
if(choice == 0){
cout << "\n => Operation cancelled : add new account holder" << endl;
getc(stdin);
return;
}
else if(choice == 1 || choice == 2){
accountType = (choice == 1? "Saving": "Current");
break;
}
else cout << "\n => ERROR: invalid choice entered..." << endl;
}
// -------- initial bank balance --------
scanNumber(balance, " Enter Initial balance: ");
while(balance < 1){ // if invalid amount is entered
cout << "\n ERROR: negative or zero amount is entered. please enter valid amount..." << endl;
scanNumber(balance, " Enter Initial balance: ");
}
long int bankAccountId = Account::createNewAccount(accountHolderId, balance, accountType);
cout << "\n => New Account is created...";
Account(bankAccountId).displayAccountDetails();
}
catch (const ERROR_BANK_ACCOUNT &error) {
if(error == ACC_NOT_FOUND)
cout << "\n ERROR: BANK ACCOUNT MAY NOT CREATED CORRECTLY" << error << endl;
}
catch (const ERROR_USER &error) {
if(error == USER_NOT_FOUND)
cout << "\n ERROR: " << errmsg::USER_NOT_FOUND << endl;
}
catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
void Staff::removeBankAccount()
{
if(!isUserValid) throw INVALID_USER;
int account_number;
try {
scanNumber(account_number, "\n Enter Bank Account Number to Remove: ");
Account::removeAccount(account_number, this->id);
}
catch (const ERROR_BANK_ACCOUNT &error) {
if(error == ACC_NOT_FOUND)
cerr << "\n ERROR: " << errmsg::ACC_NOT_FOUND << endl;
}
catch (const ERROR_USER &error) {
if(error == INVALID_PASSWORD)
cerr << "\n ERROR: " << errmsg::INVALID_PASSWORD << endl;
}
catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
void Staff::withdraw()
{
if(!isUserValid) throw INVALID_USER;
long int accountNumber, amount;
try {
scanNumber(accountNumber, " => Enter bank account number: ");
Account acc(accountNumber);
scanNumber(amount, " => Enter amount to Withdraw: Rs.");
if(amount < 1) // if negative amount is entered
throw amount;
acc.withdraw(amount ,this->id);
}
catch (const ERROR_BANK_ACCOUNT &error) {
if(error == ACC_NOT_FOUND)
cerr << "\n ERROR: " << errmsg::ACC_NOT_FOUND << endl;
else if(error == INSUFFICIENT_BALANCE)
cerr << "\n ERROR: " << errmsg::INSUFFICIENT_BALANCE << endl;
}
catch (const long int &amount){
cerr << "\n ERROR: NEGATIVE AMOUNT (" << amount << ") IS ENTERED."
<< " PLEASE ENTER VALID AMOUNT..." << endl;
} catch (const string &error){
cerr << "\n ERROR: " << error << endl;
} catch (const exception &error){
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...){
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
void Staff::deposit()
{
if(!isUserValid) throw INVALID_USER;
long int accountNumber, amount;
scanNumber(accountNumber, " => Enter bank account number: ");
try {
// Account constructor will throw an error (string type), if account is not found
Account acc(accountNumber);
scanNumber(amount, " => Enter amount to Deposit: Rs.");
if(amount < 1) // if negative amount is entered
throw amount;
acc.deposit(amount, this->id);
}
catch (const ERROR_BANK_ACCOUNT &error) {
cerr << "\n ERROR: " << errmsg::ACC_NOT_FOUND << endl;
}
catch (const long int &amount){
cerr << "\n ERROR: NEGATIVE OR ZERO AMOUNT (" << amount << ") IS ENTERED" << endl
<< " PLEASE ENTER VALID AMOUNT..." << endl;
} catch (const exception &error){
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
}
// =================================== END STAFF ======================================