-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj.cpp
More file actions
337 lines (288 loc) · 12 KB
/
Copy pathproj.cpp
File metadata and controls
337 lines (288 loc) · 12 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
#include <iostream>
#include <string>
#include <stdlib.h>
#include "Person.h"
// run code with g++ proj.cpp Account.cpp Person.cpp
// then ./a.out
//or ./a.out < test.txt
//test colors specified with ANSI color codes
//main menu
void menu(){
std::cout<<"\n-------Menu-------\n"<<std::endl;
std::cout<<"1. Create Account"<<std::endl;
std::cout<<"2. Check Balance"<<std::endl;
std::cout<<"3. Make a deposit"<<std::endl;
std::cout<<"4. Transfer Funds Between Accounts"<<std::endl;
std::cout<<"\nSelection: ";
}
//Generate random Bank Account Number
std::string randomId(){
//Generate random account number
int id = rand() % 100000 + 1;
char randLetter1 = toupper('a' + rand()%26 );
std::string s = std::to_string(id);
char randLetter2 = toupper('a' + rand()%26 );
char randLetter3 = toupper('a' + rand()%26 );
std::string bankId = randLetter1 + s + randLetter2 + randLetter3;
return bankId;
}
//display account balance of all accounts
void checkBalance(Person *me){
std::cout<< "\nAccount Balance is"<<std::endl;
me->printAccounts();
}
//make a deposit
// void deposit(Person *me){
// if(me->getNumAccounts() < 1){
// std::cout<< "You must have at least 1 account to make a deposit!\nBack to MAIN MENU." << std::endl;
// } else{
// bool found = false;
// std::string accNo;
// std::cout<< "\nAccounts"<<std::endl;
// me->printAccounts();
// //loop till account number found
// while(!found){
// std::cout<< "\nType the account number:"<<std::endl;
// std::cin >> accNo ;
// (me->accountExists(accNo) == true )? (found = true) : (std::cout<<"Account Does NOT exist"<<std::endl);
// }
// float amount;
// std::cout<< "\nEnter Amount to deposit: $";
// std::cin >> amount;
// me->deposit(accNo,amount);
// }
// }
void deposit(Person *me){
if(me->getNumAccounts() < 1){
std::cout<< "You must have at least 1 account to make a deposit!\nBack to MAIN MENU." << std::endl;
} else{
bool found = false;
int accNo;
std::cout<< "\nAccounts"<<std::endl;
me->printAccounts();
//loop till account number found
while(!found){
std::cout<< "\nSelect the account [ 1 - 3 ]:"<<std::endl;
std::cin >> accNo ;
//make sure input is of the correct type
while (!std::cin.good())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout<< "\nSelect the account [ 1 - 3 ]:"<<std::endl;
std::cin >> accNo ;
}
//make sure that the input meets the possible requirements
(accNo <= me->getNumAccounts() )? (found = true) : (std::cout<<"Account Does NOT exist"<<std::endl);
}
Account* x = me->getAccount(accNo-1);
float amount;
std::cout<< "\nEnter Amount to deposit: $";
std::cin >> amount;
//deposit the amount in the account
me->deposit(x->getAccountNo(),amount);
}
}
// make transfer between accounts
// void transfer(Person *me){
// //make sure atleast 2 accounts exist
// if(me->getNumAccounts() <= 1){
// std::cout<< "You must have at least two accounts to transfer between!\nBack to MAIN MENU." << std::endl;
// } else{
// std::string from, to;
// bool verified = false;
// checkBalance(me);
// //loop account number entry until gotten right
// while (!verified){
// std::cout<<"Enter account to transfer FROM"<<std::endl;
// std::cin >> from;
// (me->accountExists(from) == true )? verified = true: std::cout<<"Wrong Account Number Entered!"<<std::endl;
// }
// verified = false;
// //loop account number entry until gotten right
// while (!verified){
// std::cout<<"Enter account to transfer TO"<<std::endl;
// std::cin >> to;
// (me->accountExists(to) == true )? verified = true: std::cout<<"Wrong Account Number Entered!"<<std::endl;
// }
// me->transfer(from, to);
// }
// }
// make transfer between accounts
void transfer(Person *me){
//make sure atleast 2 accounts exist
if(me->getNumAccounts() <= 1){
std::cout<< "You must have at least two accounts to transfer between!\nBack to MAIN MENU." << std::endl;
} else{
int from, to;
bool verified = false;
checkBalance(me);
//loop account number entry until gotten right
while (!verified){
std::cout<<"Enter account (1, 2 or 3) to transfer FROM"<<std::endl;
std::cin >> from;
while (!std::cin.good())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout<< "Enter account (1, 2 or 3) to transfer FROM"<<std::endl;
std::cin >> from ;
}
(from <= me->getNumAccounts() && from > 0 )? (verified = true) : (std::cout<<"Account Does NOT exist"<<std::endl);
}
verified = false;
//loop account number entry until gotten right
while (!verified){
std::cout<<"Enter account (1, 2 or 3) to transfer TO"<<std::endl;
std::cin >> to;
while (!std::cin.good())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout<< "Enter account (1, 2 or 3) to transfer TO"<<std::endl;
std::cin >> to ;
}
(to <= me->getNumAccounts() && to > 0 )? (verified = true) : (std::cout<<"Account Does NOT exist"<<std::endl);
}
//subtract 1 from value to get index
Account *b = me->getAccount(from-1);
Account *c = me->getAccount(to-1);
std::string fromNo, toNo;
fromNo = b->getAccountNo();
toNo = c->getAccountNo();
me->transfer(fromNo, toNo);
}
}
//create a new account
void createAccount(Person *me){
int option;
std::string accType;
std::string myName = me->getName();
while(true){
std::cout<<"Enter account type: \n(1) for Chequing \nor \n(2) for Savings"<<std::endl;
std::cin>> option ;
if (option == 1){
accType = "Chequing";
break;
}else if (option == 2){
accType = "Savings";
break;
}
}
me->setAccount(myName,accType,randomId(),0.00);
}
void testInt(int actual, int expected,std::string testName){
std::cout<< testName <<std::endl;
std::cout<<"\tActual Value: "<< actual<< "\n\tExpected Value: "<< expected<<std::endl;
if(actual != expected){
std::cout<< "\e[0;31m" <<"\t --------"<< "\E[0m"<<std::endl;
std::cout<< "\e[0;31m" <<"\t| Failed |"<< "\E[0m"<<std::endl;
std::cout<< "\e[0;31m" <<"\t --------"<< "\E[0m"<<std::endl;
}else{
std::cout<< "\E[22;32m" <<"\t\t ---------"<< "\E[0m" <<std::endl;
std::cout<< "\E[22;32m" <<"\t\t| Passed! |"<< "\E[0m" <<std::endl;
std::cout<< "\E[22;32m" <<"\t\t ---------\n"<< "\E[0m" <<std::endl;
}
}
void testString(std::string actual, std::string expected,std::string testName){
std::cout<< testName <<std::endl;
std::cout<<"\tActual Value: "<< actual<< "\n\tExpected Value: "<< expected<<std::endl;
if(actual != expected){
std::cout<< "\e[0;31m" <<"\t ---------"<< "\E[0m"<<std::endl;
std::cout<< "\e[0;31m" <<"\t| Failed! |"<< "\E[0m"<<std::endl;
std::cout<< "\e[0;31m" <<"\t ---------"<< "\E[0m"<<std::endl;
}else{
std::cout<< "\E[22;32m" <<"\t\t ---------"<< "\E[0m" <<std::endl;
std::cout<<"\E[22;32m" <<"\t\t| Passed! |"<< "\E[0m" <<std::endl;
std::cout<<"\E[22;32m" <<"\t\t ---------\n"<< "\E[0m" <<std::endl;
}
}
void testFunction(){
//Generic testing for dynamic allocation and setting accounts
std::cout << "\e[0;34m" <<" ------"<<std::endl;
std::cout <<"| TEST |"<<std::endl;
std::cout <<" ------"<< "\E[0m" <<std::endl;
Person *testPerson = new Person("Tester");
testString(testPerson->getName(),"Tester","Test 1\nReturn created person name: 'Tester' !");
testInt(testPerson->getNumAccounts(),0,"Test 2\nReturn the number of Accounts Tester has!");
testPerson->setAccount("Tester","Chequing",randomId(),0.00);
std::cout<<" --------------------------------------------"<<std::endl;
std::cout<<"| Just Created a Chequing account for Tester |"<<std::endl;
std::cout<<" --------------------------------------------\n"<<std::endl;
testInt(testPerson->getNumAccounts(),1,"Test 3\nReturn the number of Accounts Tester has!");
testPerson->setAccount("Tester","Savings",randomId(),0.00);
std::cout<<" -------------------------------------------"<<std::endl;
std::cout<<"| Just Created a Savings account for Tester |"<<std::endl;
std::cout<<" -------------------------------------------\n"<<std::endl;
testInt(testPerson->getNumAccounts(),2,"Test 4\nReturn the number of Accounts Tester has!");
checkBalance(testPerson);
Account *a = testPerson->getAccount(0);
testPerson->deposit(a->getAccountNo(),50);
std::cout<<" ----------------------------"<<std::endl;
std::cout<<"| Deposit $50 in account (1) |"<<std::endl;
std::cout<<" ----------------------------\n"<<std::endl;
testInt(a->getAccountBalance(),50,"Test 5\nReturn an account balance of $50!");
checkBalance(testPerson);
a = testPerson->getAccount(1);
std::cout<<" deposit 10 $ to -> "<<a->getAccountNo()<<std::endl;
testPerson->deposit(a->getAccountNo(),10);
std::cout<<" ----------------------------"<<std::endl;
std::cout<<"| Deposit $10 in account (2) |"<<std::endl;
std::cout<<" ----------------------------\n"<<std::endl;
testInt(a->getAccountBalance(),10,"Test 6\nReturn an account balance of $10!");
checkBalance(testPerson);
std::cout<<"End of Test"<<std::endl;
delete a;
delete testPerson;
}
int main(){
int mode;
std::cout << "Welcome to Bank Account Simulator.\nPress (1) to run Test or\nPress (2) to run program manually."<<std::endl;
std::cin>> mode ;
// if (std::stoi(mode) == 1) {testFunction();}
if (mode == 1) {testFunction();}
else{
srand(time(0));
std::string myName;
std::cout << "Please enter your name"<<std::endl;
std::cin>> myName ;
std::system("clear");
Person *p1 = new Person(myName);
int selection;
bool state = true;
std::cout <<"\nHello "<<p1->getName() <<"!!! \nWelcome to Standard Bank.\nThe Greenest Bank in the world.\n"<<std::endl;
while (state == true){
std::cout<<"Select an option by typing the Number then hit ENTER."<<std::endl;
menu();
std::cin>> selection ;
//switch case main menu
switch(selection){
case 1:
if (p1->getNumAccounts() == 3 ){
std::cout<< "\nSorry :( \nYou have reached the MAXIMUM # of accounts"<<std::endl;
}else{
createAccount(p1);
}
break;
case 2:
checkBalance(p1);
break;
case 3:
deposit(p1);
break;
case 4:
transfer(p1);
break;
}
std::string str;
std::cout<<"Would you like to exit program? Y or N."<<std::endl;
std::cin >> str;
std::system("clear");
if (str == "Y" || str == "y" || str == "yes" || str == "Yes"){
std::cout <<"\nGoodbye "<<p1->getName() <<" and Thank you for using Standard Bank.\n"<<std::endl;
state = false;
}
}
}
return 0;
}