-
Notifications
You must be signed in to change notification settings - Fork 1
/
Account.cpp
176 lines (159 loc) · 5.08 KB
/
Account.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Account.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tblaase <tblaase@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/23 13:50:56 by tblaase #+# #+# */
/* Updated: 2022/03/15 17:29:28 by tblaase ### ########.fr */
/* */
/* ************************************************************************** */
#include "Account.hpp"
#include <iostream>
#include <iomanip>
#include <ctime>
// Init all the start-values
int Account::_nbAccounts = 0;
int Account::_totalAmount = 0;
int Account::_totalNbDeposits = 0;
int Account::_totalNbWithdrawals = 0;
// Empty constuctor, when called without input
Account::Account() {}
// Constructor with init deposit
// after the creation a note about the created account is printed
Account::Account(int initial_deposit)
{
// Init all the start-values of each account
// and keep track of the number of accounts as well as the total ammount of money
this->_nbDeposits = 0;
this->_nbWithdrawals = 0;
this->_amount = initial_deposit;
Account::_totalAmount += this->_amount;
this->_accountIndex = Account::_nbAccounts;
Account::_nbAccounts++;
// Printing note of the creation of the Account with its starting values and the state created
_displayTimestamp();
std::cout <<
"index:" << this->_accountIndex << ";" <<
"amount:" << this->checkAmount() << ";" <<
"created" <<
std::endl;
}
// destructor that prints all the ending values as well as the state closed
Account::~Account(void)
{
_displayTimestamp();
std::cout <<
"index:" << this->_accountIndex << ";" <<
"amount:" << this->checkAmount() << ";" <<
"closed" <<
std::endl;
}
// getter for the total number of accounts
int Account::getNbAccounts( void )
{
return (Account::_nbAccounts);
}
// getter for the total amoount of money of all accounts
int Account::getTotalAmount( void )
{
return (Account::_totalAmount);
}
// getter for the total number of deposits
int Account::getNbDeposits( void )
{
return (Account::_totalNbDeposits);
}
// getter for the total number of withdrawals
int Account::getNbWithdrawals( void )
{
return (Account::_totalNbWithdrawals);
}
// getter to display all the accounts infos
void Account::displayAccountsInfos( void )
{
_displayTimestamp();
std::cout <<
"accounts:" << getNbAccounts() << ";" <<
"total:" << getTotalAmount() << ";" <<
"deposits:" << getNbDeposits() << ";" <<
"withdrawals:" << getNbWithdrawals() <<
std::endl;
}
// makes a deposit
void Account::makeDeposit( int deposit )
{
_displayTimestamp();
std::cout <<
"index:" << this->_accountIndex << ";" <<
"p_amount:" << this->checkAmount() << ";" <<
"deposit:" << deposit << ";";
this->_amount += deposit;
this->_nbDeposits++;
Account::_totalAmount += deposit;
Account::_totalNbDeposits++;
std::cout <<
"amount:" << this->checkAmount() << ";" <<
"nb_deposits:" << this->_nbDeposits <<
std::endl;
}
// makes a withdraw, returns true if successfull, false if not
bool Account::makeWithdrawal( int withdrawal )
{
_displayTimestamp();
std::cout <<
"index:" << this->_accountIndex << ";" <<
"p_amount:" << this->checkAmount() << ";";
if (this->checkAmount() < withdrawal)
{
std::cout <<
"withdrawal:refused" <<
std::endl;
return (false);
}
else
{
this->_amount -= withdrawal;
this->_nbWithdrawals++;
Account::_totalAmount -= withdrawal;
Account::_totalNbWithdrawals++;
std::cout <<
"withdrawal:" << withdrawal << ";" <<
"amount:" << this->checkAmount() << ";" <<
"nb_withdrawals:" << this->_nbWithdrawals <<
std::endl;
return (true);
}
}
// returns the amount of money of the account
int Account::checkAmount( void ) const
{
return (this->_amount);
}
// displays the whole status of an account
void Account::displayStatus( void ) const
{
_displayTimestamp();
std::cout <<
"index:" << this->_accountIndex << ";" <<
"amount:" << this->checkAmount() << ";" <<
"deposits:" << this->_nbDeposits << ";" <<
"withdrawals:" << this->_nbWithdrawals <<
std::endl;
}
// displays the timestamp in the correct format
void Account::_displayTimestamp( void )
{
time_t now = std::time(NULL);
struct tm timenow = *std::localtime(&now);
std::cout << "[" << timenow.tm_year + 1900 <<
std::setfill('0') << std::setw(2) << timenow.tm_mon + 1 <<
std::setfill('0') << std::setw(2) << timenow.tm_mday << "_" <<
std::setfill('0') << std::setw(2) << timenow.tm_hour <<
std::setfill('0') << std::setw(2) << timenow.tm_min <<
std::setfill('0') << std::setw(2) << timenow.tm_sec <<
"] ";
// insert this line below instead of the above and then run 'make re && ./leak_information >my.log && diff my.log 19920104_091532.log'
// std::cout << "[19920104_091532] ";
}