-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccountholder.cpp
More file actions
138 lines (120 loc) · 4.62 KB
/
accountholder.cpp
File metadata and controls
138 lines (120 loc) · 4.62 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
#include "accountholder.h"
using namespace std;
using namespace utils;
using namespace bankerror;
using json = nlohmann::json;
// ------- CONSTRUCTOR -------
AccountHolder::AccountHolder(const string &accountHolderId, const string &password)
{
isUserValid = false;
try {
json accountHolder = readData("account_holder", accountHolderId);
// account holder found and password is valid
if(!accountHolder.empty() && password == accountHolder["password"]){
this->id = accountHolderId;
this->name = accountHolder["name"];
this->mobile = accountHolder["mobile"];
this->address = accountHolder["address"];
// storing bank account numbers related to account holder in vector
for(size_t i=0; i<accountHolder["bank_accounts"].size(); i++){
string bankAccount = accountHolder["bank_accounts"][i];
bankAccounts.push_back(trim(bankAccount, "\""));
}
isUserValid = true;
}
} catch (const exception &error) {
displayCustomErrorMessage( __PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage( __PRETTY_FUNCTION__, __FILE__);
}
}
// ------- STATIC METHODS -------
AccountHolder * AccountHolder::login(const string &id, const string &password){
try {
unique_ptr<AccountHolder> accountHolder(new AccountHolder(id, password));
if(accountHolder->isValid()){ // userid and password is valid
return accountHolder.release();
} else {
throw INVALID_USER;
}
}
catch (const ERROR_USER &error) {
cout << "\n ERROR: " << errmsg::INVALID_USER << endl;
}
catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
return NULL;
}
string AccountHolder::createAccountHolder(const std::string &name,
const std::string &mobile,
const std::string &address,
const std::string &password)
{
try {
json data = readData();
json newAccountHolder = {
{"name", name},
{"mobile", mobile},
{"address", address},
{"password", password},
{"bank_accounts", json::array()}
};
long int accountHolderId = data["last_account_holder_number"];
accountHolderId++;
data["last_account_holder_number"] = accountHolderId;
data["account_holder"].push_back(json::object_t::value_type(to_string(accountHolderId), newAccountHolder));
updateData(data);
return to_string(accountHolderId); // return the id of new account holder
} catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
} catch (...) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__);
}
return ""; // if account is not created
}
// ------- OTHER METHODS -------
bool AccountHolder::isValid(){ return this->isUserValid; }
void AccountHolder::displayDetails(){
cout << "\n ======== DETAILS ========" << endl;
cout << " Customer id : " << this->id << endl
<< " Name : " << this->name << endl
<< " Mobile : " << this->mobile << endl
<< " Address : " << this->address << endl
<< " Bank Accounts : ";
// displaying bank account numbers if any
if(bankAccounts.size() > 0){
cout << "[";
for(std::size_t i=0; i<bankAccounts.size(); i++){
cout << " " << bankAccounts[i] << ",";
}
cout << "\b ]" << endl;
} else {
cout << "NO BANK ACCOUNT" << endl;
}
}
void AccountHolder::displayPanel(){
if(!isUserValid) throw INVALID_USER;
try {
short choice = 1;
system("clear");
while(choice != 0){
cout << "\n ----------- ADMIN PANEL -----------\n" << endl
<< " 1) Display Your Details " << endl
<< " 0) Logout" << endl;
scanNumber(choice, " => Enter Choice: ");
switch (choice) {
case 0: system("clear");
return;
default:
cout << "\n Invalid choice !" << endl;
}
}
} catch (const exception &error) {
displayCustomErrorMessage(__PRETTY_FUNCTION__, __FILE__, error.what());
}
}
// =================================== END ACCOUNT_HOLDER ======================================