CPP00/ex02/Account.cpp
2025-03-21 14:46:30 +01:00

150 lines
3.9 KiB
C++

/* ************************************************************************** */
/* */
/* :::::::: */
/* Account.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/21 11:04:19 by whaffman #+# #+# */
/* Updated: 2025/03/21 14:45:34 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <iostream>
#include <ctime>
#include <iomanip>
#include "Account.hpp"
int Account::_nbAccounts = 0;
int Account::_totalAmount = 0;
int Account::_totalNbDeposits = 0;
int Account::_totalNbWithdrawals = 0;
Account::Account()
{
}
Account::Account( int initial_deposit )
{
_accountIndex = Account::getNbAccounts();
_amount = initial_deposit;
_nbDeposits = 0;
_nbWithdrawals = 0;
_nbAccounts++;
_totalAmount += _amount;
_displayTimestamp();
std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";created" << std::endl;
}
Account::~Account( void )
{
_displayTimestamp();
std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";closed" << std::endl;
}
int Account::getNbAccounts( void )
{
return (Account::_nbAccounts);
}
int Account::getTotalAmount( void )
{
return (_totalAmount);
}
int Account::getNbDeposits( void )
{
return (_totalNbDeposits);
}
int Account::getNbWithdrawals( void )
{
return (_totalNbWithdrawals);
}
void Account::displayAccountsInfos( void )
{
_displayTimestamp();
std::cout << "accounts:" << Account::getNbAccounts() << ";"
<< "total:" << Account::getTotalAmount() << ";"
<< "deposits:" << Account::getNbDeposits() << ";"
<< "withdrawals:" << Account::getNbWithdrawals()
<< std::endl;
}
void Account::makeDeposit( int deposit )
{
_displayTimestamp();
std::cout << "index:" << _accountIndex << ";"
<< "p_amount:" << _amount << ";";
_amount += deposit;
_totalAmount += deposit;
_nbDeposits++;
_totalNbDeposits++;
std::cout << "deposit:" << deposit << ";"
<< "amount:" << _amount << ";"
<< "nb_deposits:" << _nbDeposits
<< std::endl;
}
bool Account::makeWithdrawal( int withdrawal )
{
_displayTimestamp();
std::cout << "index:" << _accountIndex << ";"
<< "p_amount:" << _amount << ";";
if(withdrawal >= _amount)
{
std::cout << "withdrawal:refused" << std::endl;
return(false);
}
_amount -= withdrawal;
_totalAmount -= withdrawal;
_nbWithdrawals++;
_totalNbWithdrawals++;
std::cout << "withdrawal:" << withdrawal << ";"
<< "amount:" << _amount << ";"
<< "nb_withdrawals:" << _nbWithdrawals
<< std::endl;
return (true);
}
int Account::checkAmount( void ) const
{
return(_amount);
}
void Account::displayStatus( void ) const
{
_displayTimestamp();
std::cout << "index:" << _accountIndex << ";"
<< "amount:" << _amount << ";"
<< "deposits:" << _nbDeposits << ";"
<< "withdrawals:" << _nbWithdrawals
<< std::endl;
}
void Account::_displayTimestamp( void )
{
time_t now = std::time(NULL);
std::tm* localTime = std::localtime(&now);
#include <iomanip> // Add this include at the top of the file for std::setfill and std::setw
std::cout << "["
<< (localTime->tm_year + 1900)
<< std::setfill('0') << std::setw(2) << (localTime->tm_mon + 1)
<< std::setfill('0') << std::setw(2) << localTime->tm_mday << "_"
<< std::setfill('0') << std::setw(2) << localTime->tm_hour
<< std::setfill('0') << std::setw(2) << localTime->tm_min
<< std::setfill('0') << std::setw(2) << localTime->tm_sec
<< "] ";
}