-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccount_zhou.cpp
165 lines (131 loc) · 4.44 KB
/
BankAccount_zhou.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
// Name: Lisheng Zhou
// Final Project: Account Management System
// Date: Dec 13, 2015
#include "BankAccount_zhou.h"
using std::string;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
using std::setw;
// constructor
BankAccount::BankAccount()
: Account()
{
}
BankAccount::~BankAccount()
{
}
void BankAccount::viewBalance()
{
// view account balance
// round to 2 decimal places
cout.precision(2);
cout << "You have $" << std::fixed << getBalance() << " in your bank account." << endl;
}
void BankAccount::deposit(double value)
{
// deposit
double newbalance = getBalance() + value;
setBalance(newbalance);
// add event to history
// time
time_t seconds = time(NULL);
struct tm timeinfo;
localtime_s(&timeinfo, &seconds);
// append transaction to bank history
ofstream bankHistoryFile;
bankHistoryFile.open("bank_transaction_history.txt", std::ios::out | std::ios::app);
// event amount date balance
bankHistoryFile << std::fixed << std::setprecision(2);
bankHistoryFile << "\nDesposit\t" << value << "\t\t";
bankHistoryFile << std::setw(2) << std::setfill('0') << (timeinfo.tm_mon + 1) << "/"
<< std::setw(2) << std::setfill('0') << timeinfo.tm_mday << "/"
<< (timeinfo.tm_year + 1900) << "\t";
bankHistoryFile << newbalance;
bankHistoryFile.close(); // close file
}
void BankAccount::withdraw(double value)
{
// withdraw
double newbalance = getBalance() - value;
if (newbalance >= 0){
setBalance(newbalance);
// add event to history
// time
time_t seconds = time(NULL);
struct tm timeinfo;
localtime_s(&timeinfo, &seconds);
// append transaction to bank history
ofstream bankHistoryFile;
bankHistoryFile.open("bank_transaction_history.txt", std::ios::out | std::ios::app);
// event amount date balance
bankHistoryFile << std::fixed << std::setprecision(2);
bankHistoryFile << "\nWithdrawal\t" << value << "\t\t";
bankHistoryFile << std::setw(2) << std::setfill('0') << (timeinfo.tm_mon + 1) << "/"
<< std::setw(2) << std::setfill('0') << timeinfo.tm_mday << "/"
<< (timeinfo.tm_year + 1900) << "\t";
bankHistoryFile << newbalance;
bankHistoryFile.close(); // close file
}
else
cout << "You don't have sufficient amount of cash balance in your bank account. Transaction failed." << endl;
}
void BankAccount::printHistory()
{
// print history
string newevent, amount, date, balance;
ifstream bankHistoryFile("bank_transaction_history.txt", std::ios::in);
if (bankHistoryFile){
// header
cout << std::left << std::setw(16) << std::setfill(' ') << "Event"
<< std::left << std::setw(15) << std::setfill(' ') << "Amount"
<< std::left << std::setw(20) << std::setfill(' ') << " Date"
<< std::left << std::setw(10) << std::setfill(' ') << " Balance" << endl;
while (bankHistoryFile && !bankHistoryFile.eof()){
bankHistoryFile >> newevent >> amount >> date >> balance;
double doubleAmount = std::stod(amount);
double doubleBalance = std::stod(balance);
cout.imbue(std::locale(""));
cout << std::fixed;
cout << std::setprecision(2);
cout << std::left << std::setw(16) << std::setfill(' ') << newevent << "$"
<< std::left << std::setw(15) << std::setfill(' ') << doubleAmount
<< std::left << std::setw(20) << std::setfill(' ') << date << "$"
<< std::left << std::setw(10) << std::setfill(' ') << doubleBalance << endl;
}
}
else{
cout << "\n[Bank Transaction History is empty]" << endl;
}
bankHistoryFile.close();
}
void BankAccount::setBalance( const double &value )
{
// set account balance
Account::setBalance( value );
}
double BankAccount::getBalance() const{
// get balance
return Account::getBalance();
}
/*
void BankAccount::printHistory()
{
// print history entries
// iterator
std::vector< History >::const_iterator it;
cout << std::left<< std::setw(20) << std::setfill(' ') << "Event";
cout << std::left<< std::setw(20) << std::setfill(' ') << "Amount";
cout << std::left<< std::setw(20) << std::setfill(' ') << "Date";
cout << std::left<< std::setw(20) << std::setfill(' ') << "Balance";
//cout << endl;
for ( it = HisV.begin(); it != HisV.end(); it++ ) {
cout << std::left<< std::setw(20) << std::setfill(' ') << (*it).getEvent();
cout << std::left<< std::setw(20) << std::setfill(' ') << (*it).getValue();
cout << std::left<< std::setw(20) << std::setfill(' ') << (*it).getDate();
cout.imbue( std::locale("") );
cout << std::left<< std::setw(20) << std::setfill(' ') << (*it).getBalance();
//cout << endl;
}
}*/