-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankAccount.cpp
277 lines (233 loc) · 6.91 KB
/
BankAccount.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
/*Name: Pranav Shivkumar
Title: BankAccount.cpp (Bank Account Implementation)*/
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<vector>
//using std::cout;
//using std::cin;
//using std::endl;
//using std::fixed;
//using std::setprecision;
//using std::ofstream;
//using std::string;
//using std::setw;
using namespace std;
#include "Account.h"
#include "BankAccount.h"
//constructor definition for Bank Account
BankAccount::BankAccount()
{
setCashBalance(10000); //if not defined, set the balance to $10,000
deposit = withdraw = 0.0;
balance = getCashBalance(); //assigning the balance to be the cash balance when the class object is created
ofstream file;
file.open("Bank_Account_Balance.txt");
file << getCashBalance(); //write the balance to a text file
file.close();
ofstream fout;
fout.open("Bank_Transaction_History.txt", ios::app);
fout << "EVENT\t" << setw(15) << "AMOUNT\t" << setw(15) << "DATE\t" << setw(15) << "BALANCE" << endl;
fout.close();
}
void BankAccount::viewAccountBalance()
{
ifstream f1;
f1.open("Bank_Account_Balance.txt");
if (!f1)
{
//if the file doesn't exist, balance is $10,000 by default
double bal = 10000;
setCashBalance(bal);
getCashBalance();
cout << "Your account currently has $" << fixed << setprecision(2) << bal << endl << endl;
}
else
{
//if the file exists, read the balance from the file and print on screen
double bal = 0.0;
f1 >> bal;
cout << "Your account currently has $" << fixed << setprecision(2) << bal << endl << endl;
ofstream fout;
fout.open("Bank_Account_Balance.txt");
fout << bal << endl;
fout.close();
}
f1.close();
}
void BankAccount::setDeposit(double d)
{
//if the deposit entered is 0 or negative, print invlid amount
if (d <= 0)
{
cout << "Invalid entry. Please enter again: ";
cin.ignore();
}
else
//otherwise, read the deposit
deposit = d;
}
double BankAccount::getDeposit()
{
//return the deposit amount
return deposit;
}
void BankAccount::setWithdrawAmount(double w)
{
//if the amount to be withdrawn is 0 or negative, print invalid
if (w <= 0)
{
cout << "Invalid entry. Please enter again: ";
cin.ignore();
}
else
//otherwise, read the amount to be withdrawn
withdraw = w;
}
double BankAccount::getWithdrawAmount()
{
//return the amount to be withdrawn
return withdraw;
}
void BankAccount::depositMoney() //function to carry out the deposit function
{
cout << "You have chosen to deposit $" << getDeposit() << " into your account." << endl << endl;
viewAccountBalance();
//consult the balance in the text file. Add the deposit amount to the balance.
ifstream in;
in.open("Bank_Account_balance.txt");
in >> total;
total += getDeposit(); //add the deposit amount to the balance, contained in the 'total' member
setCashBalance(total); //update the account cash balance using 'total'
balance = getCashBalance();
//print the transaction results on screen
cout << "Money successfully deposited into your account." << endl << endl;
cout << "Your account currently has : $" << fixed << setprecision(2) << balance << endl << endl;
writeToFile(1, getDeposit(), balance); //write the corresponding results to the text file
ofstream file;
file.open("Bank_Account_Balance.txt"); //store the resulting balance to the file
file << getCashBalance();
file.close();
in.close();
}
void BankAccount::withdrawMoney() //function to carry out the withdraw the amount from the bank account
{
cout << "You have chosen to withdraw $" << fixed << setprecision(2) << getWithdrawAmount() << " from your account." << endl << endl;
double b; // read balance from file
ifstream f1("Bank_Account_Balance.txt");
f1 >> b;
setCashBalance(b);
getCashBalance();
viewAccountBalance();
//if withdraw amount is greater than balance, cancel the transaction with an ERROR MESSAGE
if (getWithdrawAmount() > getCashBalance())
{
cout << "ERROR MESSAGE: Your account has insufficient balance to carry out the transaction." << endl << endl;
return;
}
else
{
//otherwise write to the file
writeToFile(2, getWithdrawAmount(), b);
total -= getWithdrawAmount(); //subtract the withdraw amount from the balance, contained in the 'total' member
setCashBalance(total); //update the account cash balance using 'total' and assign to balance
balance = getCashBalance();
//print the corresponding results to the screen
cout << "The amount was successfully withdrawn from your account." << endl << endl;
cout << "Your account currently has: $" << fixed << setprecision(2) << balance << endl << endl;
ofstream file("Bank_Account_Balance.txt");
file << balance; //update the balance in the text file
file.close();
}
}
void BankAccount::writeToFile(int val, double amt, double bal) //function that writes to text file for transaction history
{
string mode;
ifstream in;
in.open("Bank_Account_Balance.txt");
in >> bal;
in.close();
time_t t = time(NULL); //function for time
tm* timePtr = localtime(&t);
//modes to chooose from: 1. Deposit 2. Withdraw 3. Buy 4. Sell
switch (val)
{
case 1:
mode = "Deposit";
amt = getDeposit();
bal += deposit;
setCashBalance(bal);
getCashBalance();
break;
case 2:
mode = "Withdraw";
amt = getWithdrawAmount();
if (amt > bal)
return;
else
{
amt = getWithdrawAmount();
bal -= withdraw;
setCashBalance(bal);
getCashBalance();
}
break;
case 3:
mode = "Buy";
bal -= amt;
setCashBalance(bal);
getCashBalance();
break;
case 4:
mode = "Sell";
bal += amt;
setCashBalance(bal);
getCashBalance();
break;
default:
break;
}
ofstream f1;
f1.open("Bank_Account_Balance.txt");
f1 << bal;
setCashBalance(bal);
getCashBalance();
f1.close();
ofstream file;
file.open("Bank_Transaction_History.txt", std::ios::app);
file << mode << setw(13) << amt << setw(13); //print the transactions in the text file
file << (timePtr->tm_mday) << "/" << (timePtr->tm_mon) + 1 << "/" << (timePtr->tm_year) + 1900 << setw(13);
file << "$" << bal << endl;
file.close();
}
void BankAccount::printHistory()
{
//read from the transaction file and write to screen
string str;
std::ifstream file("Bank_Transaction_History.txt", std::ios::in);
if (!file.is_open())
{
cout << "ERROR: Unable to open file" << endl;
return;
}
std::vector<string> bank;
while (std::getline(file, str))
{
bank.push_back(str);
}
for (size_t i = 0; i < bank.size(); i++)
cout << bank[i] << endl;
cout << endl << endl;
}
void BankAccount::setCashBalance(double bal)
{
//virtual function to set the account balance
balance = bal;
}
double BankAccount::getCashBalance()
{
//virtual function to get the account balance
return balance;
}