forked from nadinejerome/atm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM Machine Classes.cpp
271 lines (232 loc) · 5.23 KB
/
ATM Machine Classes.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
/*
TITLE: SIMPLE ATM MACHINE
PURPOSE: Write a program that simulates as an atm machine
TASKS: Create an account that has the ability to
1) Check checking account balance
2) Check savings account balance
3) Make transfers between each account
4) Make deposits into accounts
5) Make withdrawals from accounts
AUTHOR: Nadine E. Jerome
INSTRUCTIONS:
Enter pin number '1234'
Checkings Account Balance = 100
Savings Account Balance = 600
*/
#include <iostream>
#include <string>
// FUNCTIONS
void menu();
void account(int option);
using namespace std;
string AccountType[] = {"", "CHECKINGS", "SAVINGS"};
string response;
int AccountDetails[] = {
1234, // pin number
100, // checking account balance
600, // savings account balance
};
bool validatePin(int pin)
{
if (pin == AccountDetails[0])
{
return true;
}
else
{
return false;
}
}
bool proceed(string response)
{
if (response == "y" || response == "Y")
{
return true;
}
else if (response == "n" || response == "N")
{
string exit;
cout << "\n\n\n\t Thank you for banking with us.";
cin >> exit;
return false;
}
}
class AccountSettings
{
private:
int type; // account type
int balance; // account balance
public:
AccountSettings(int type)
{
// type 1 = checkings
// type 2 = savings
this->type = type;
this->balance = AccountDetails[this->type];
}
int getWithdraw()
{
int withdrawAmount;
cout << "Please enter amount to withdrawn:\n " << endl;
cin >> withdrawAmount;
// get account type
if (withdrawAmount <= this->balance)
{
int AccountBalance = this->balance -= withdrawAmount;
cout << "Dispensing... ";
cout << "$" << withdrawAmount << endl;
// update the account balance
AccountDetails[this->type] = AccountBalance;
getBalance();
}
else
{
cout << "Insufficent funds" << endl;
getBalance();
}
return 0;
}
int getDeposit()
{
int depositAmount;
cout << "Please enter an amount to deposit:\n"
<< endl;
cin >> depositAmount;
int AccountBalance = this->balance += depositAmount;
// update the account balance
AccountDetails[this->type] = AccountBalance;
cout << "\t$" << depositAmount << " was deposited into your account";
getBalance();
return 0;
}
int getTransfer()
{
int AmountTransfer;
int TransferTo = this->type == 1 ? 2 : 1;
cout << "Enter amount to transfer to your " << AccountType[TransferTo] << " account." << endl;
cin >> AmountTransfer;
if (AmountTransfer <= AccountDetails[this->type])
{
// update the current account balance in the selected account
int NewBalance = this->balance -= AmountTransfer;
AccountDetails[this->type] = NewBalance;
// Set the new transfered amount to transfered account
int TransferedAmount = AccountDetails[TransferTo] += AmountTransfer;
AccountDetails[TransferTo] = TransferedAmount;
cout << "$" << AmountTransfer << " has been transfered to your " << AccountType[TransferTo] << " account." << endl;
getBalance();
}
else
{
cout << "Insuffient funds." << endl;
getBalance();
}
return 0;
}
int getBalance()
{
string confirmBalance;
// get the account type, and return balance
cout << "Would you like to check your " << AccountType[this->type] << " account balance? (y/n)\n"
<< endl;
cin >> confirmBalance;
if (confirmBalance == "y" || confirmBalance == "Y")
{
cout << "Your account balance is: $" << this->balance << endl;
}
cout << "\n\nWould you like to continue (y/n)?\n";
cin >> response;
if (proceed(response))
{
account(this->type); // return to account menu
}
return 0;
}
};
void account(int option)
{
// account option = 1 (checkings)
// account option = 2 (savings)
cout << "\n\n"
<< AccountType[option] << "--\n\t1. Check balance"
<< "\n\t2. Withdraw from " << AccountType[option]
<< "\n\t3. Deposit to " << AccountType[option]
<< "\n\t4. Transfer "
<< "\n\t5. --Return to Menu." << endl;
// Pass in account type
AccountSettings Account(option);
int selectMenu;
cin >> selectMenu;
switch (selectMenu)
{
case 1:
cout << Account.getBalance();
break;
case 2:
cout << Account.getWithdraw();
break;
case 3:
cout << Account.getDeposit();
break;
case 4:
cout << Account.getTransfer();
break;
case 5:
menu(); // return to main menu
break;
default:
cout << "Would you like to continue (y/n)\n";
cin >> response;
proceed(response);
if (proceed(response))
{
menu(); // return to main menu
}
break;
}
}
void menu()
{
int option;
cout << "\n\nMain Menu--" << endl;
cout << "\tPlease make a selection. " << endl;
cout << "\t1. Checkings \n\t2. Savings \n\t3. Exit" << endl;
cin >> option;
switch (option)
{
case 1:
account(option); // checkings
break;
case 2:
account(option); // savings
break;
default:
cout << "Would you like to continue (y/n)\n";
cin >> response;
proceed(response);
if (proceed(response))
{
menu();
}
break;
}
}
// Begin MAIN
int main()
{
int pin;
cout << "Welcome to Bank of Programming.\n\tPlease enter your pin number to access your account:" << endl;
do
{
cin >> pin;
if (validatePin(pin))
{
menu(); // continue to main menu
}
else
{
cout << "Invalid pin. Please enter pin number:" << endl;
}
} while (!validatePin(pin));
return 0;
}