From 861eac6e55c3fe70a3c7ff9e44c64c1fac44bf16 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Sat, 5 Feb 2022 00:07:53 +0100 Subject: [PATCH] ex02 ok --- d00/ex02/Account.cpp | 127 ++++++++++++++++++++++++++++++++++--------- d00/ex02/Makefile | 2 +- d00/ex02/tests.cpp | 69 ++++++++++++----------- 3 files changed, 135 insertions(+), 63 deletions(-) diff --git a/d00/ex02/Account.cpp b/d00/ex02/Account.cpp index 4adb3ea..46437e4 100644 --- a/d00/ex02/Account.cpp +++ b/d00/ex02/Account.cpp @@ -1,46 +1,119 @@ #include "Account.hpp" #include +#include +#include + +void Account::displayAccountsInfos( void ) { + + _displayTimestamp(); + std::cout << "accounts:" << _nbAccounts << ";"; + std::cout << "total:" << _totalAmount << ";"; + std::cout << "deposits:" << _totalNbDeposits << ";"; + std::cout << "withdrawals:" << _totalNbWithdrawals << std::endl; +} Account::Account( int initial_deposit ) { - std::cout << "hello" << std::endl; - return 1; + + this->_accountIndex = this->_nbAccounts; + this->_nbAccounts++; + this->_amount = initial_deposit; + this->_totalAmount += initial_deposit; + this->_nbDeposits = 0; + this->_nbWithdrawals = 0; + + this->_displayTimestamp(); + std::cout << "index:" << this->_accountIndex << ";"; + std::cout << "amount:" << this->_amount << ";"; + std::cout << "created" << std::endl; + + return; + } Account::~Account( void ) { + + this->_displayTimestamp(); + std::cout << "index:" << this->_accountIndex << ";"; + std::cout << "amount:" << this->_amount << ";"; + std::cout << "closed" << std::endl; + return; } -/* - typedef Account t; +void Account::makeDeposit( int deposit ) { - static int getNbAccounts( void ); - static int getTotalAmount( void ); - static int getNbDeposits( void ); - static int getNbWithdrawals( void ); - static void displayAccountsInfos( void ); + this->_displayTimestamp(); + std::cout << "index:" << this->_accountIndex << ";"; + std::cout << "p_amount:" << this->_amount << ";"; + std::cout << "deposit:" << deposit << ";"; - Account( int initial_deposit ); - ~Account( void ); + this->_totalNbDeposits++; + this->_nbDeposits++; + this->_amount += deposit; + this->_totalAmount += deposit; - void makeDeposit( int deposit ); - bool makeWithdrawal( int withdrawal ); - int checkAmount( void ) const; - void displayStatus( void ) const; + std::cout << "amount:" << this->_amount << ";"; + std::cout << "nb_deposits:" << this->_nbDeposits << std::endl; +} +bool Account::makeWithdrawal( int withdrawal ) { -private: + this->_displayTimestamp(); + std::cout << "index:" << this->_accountIndex << ";"; + std::cout << "p_amount:" << this->_amount << ";"; - static int _nbAccounts; - static int _totalAmount; - static int _totalNbDeposits; - static int _totalNbWithdrawals; + if (withdrawal > this->_amount) + { + std::cout << "withdrawal:refused" << std::endl; + return false; + } + this->_totalNbWithdrawals++; + this->_nbWithdrawals++; + this->_amount -= withdrawal; + this->_totalAmount -= withdrawal; - static void _displayTimestamp( void ); + std::cout << "withdrawal:" << withdrawal << ";"; + std::cout << "amount:" << this->_amount << ";"; + std::cout << "nb_withdrawals:" << this->_nbWithdrawals << std::endl; - int _accountIndex; - int _amount; - int _nbDeposits; - int _nbWithdrawals; + return true; +} + +void Account::displayStatus( void ) const { + + this->_displayTimestamp(); + std::cout << "index:" << this->_accountIndex << ";"; + std::cout << "amount:" << this->_amount << ";"; + std::cout << "deposits:" << this->_nbDeposits << ";"; + std::cout << "withdrawals:" << this->_nbWithdrawals << std::endl; + +} + +void Account::_displayTimestamp( void ) { + + time_t rawtime = time(NULL); + struct tm *stamp = localtime(&rawtime); + std::cout << "["; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_year + 1900; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_mon; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_mday << "_"; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_hour; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_min; + std::cout << std::setfill('0') << std::setw(2) << stamp->tm_sec; + std::cout << "] "; + +} + +int Account::_totalAmount = 0; +int Account::_nbAccounts = 0; +int Account::_totalNbDeposits = 0; +int Account::_totalNbWithdrawals = 0; + +int Account::checkAmount( void ) const {return 0;} +int Account::getNbAccounts( void ) {return 0;} +int Account::getTotalAmount( void ) {return 0;} +int Account::getNbDeposits( void ) {return 0;} +int Account::getNbWithdrawals( void ) {return 0;} + +Account::Account( void ) {return;} - Account( void ); -*/ diff --git a/d00/ex02/Makefile b/d00/ex02/Makefile index 80da725..a0fb4c5 100644 --- a/d00/ex02/Makefile +++ b/d00/ex02/Makefile @@ -7,7 +7,7 @@ NAME = account CC = clang++ -CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 +CFLAGS = -Wall -Wextra -Wno-unused -Werror $(INCLUDES) -std=c++98 VPATH = $(D_SRCS) diff --git a/d00/ex02/tests.cpp b/d00/ex02/tests.cpp index 40ae348..20fb0e7 100644 --- a/d00/ex02/tests.cpp +++ b/d00/ex02/tests.cpp @@ -12,53 +12,52 @@ #include #include "Account.hpp" - int main( void ) { -// typedef std::vector accounts_t; -// typedef std::vector ints_t; -// typedef std::pair acc_int_t; + typedef std::vector accounts_t; + typedef std::vector ints_t; + typedef std::pair acc_int_t; -// int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 }; -// size_t const amounts_size( sizeof(amounts) / sizeof(int) ); -// accounts_t accounts( amounts, amounts + amounts_size ); -// accounts_t::iterator acc_begin = accounts.begin(); -// accounts_t::iterator acc_end = accounts.end(); + int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 }; + size_t const amounts_size( sizeof(amounts) / sizeof(int) ); + accounts_t accounts( amounts, amounts + amounts_size ); + accounts_t::iterator acc_begin = accounts.begin(); + accounts_t::iterator acc_end = accounts.end(); -// int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 }; -// size_t const d_size( sizeof(d) / sizeof(int) ); -// ints_t deposits( d, d + d_size ); -// ints_t::iterator dep_begin = deposits.begin(); -// ints_t::iterator dep_end = deposits.end(); + int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 }; + size_t const d_size( sizeof(d) / sizeof(int) ); + ints_t deposits( d, d + d_size ); + ints_t::iterator dep_begin = deposits.begin(); + ints_t::iterator dep_end = deposits.end(); -// int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 }; -// size_t const w_size( sizeof(w) / sizeof(int) ); -// ints_t withdrawals( w, w + w_size ); -// ints_t::iterator wit_begin = withdrawals.begin(); -// ints_t::iterator wit_end = withdrawals.end(); + int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 }; + size_t const w_size( sizeof(w) / sizeof(int) ); + ints_t withdrawals( w, w + w_size ); + ints_t::iterator wit_begin = withdrawals.begin(); + ints_t::iterator wit_end = withdrawals.end(); -// Account::displayAccountsInfos(); -// std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); -// for ( acc_int_t it( acc_begin, dep_begin ); -// it.first != acc_end && it.second != dep_end; -// ++(it.first), ++(it.second) ) { + for ( acc_int_t it( acc_begin, dep_begin ); + it.first != acc_end && it.second != dep_end; + ++(it.first), ++(it.second) ) { -// (*(it.first)).makeDeposit( *(it.second) ); -// } + (*(it.first)).makeDeposit( *(it.second) ); + } -// Account::displayAccountsInfos(); -// std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); -// for ( acc_int_t it( acc_begin, wit_begin ); -// it.first != acc_end && it.second != wit_end; -// ++(it.first), ++(it.second) ) { + for ( acc_int_t it( acc_begin, wit_begin ); + it.first != acc_end && it.second != wit_end; + ++(it.first), ++(it.second) ) { -// (*(it.first)).makeWithdrawal( *(it.second) ); -// } + (*(it.first)).makeWithdrawal( *(it.second) ); + } -// Account::displayAccountsInfos(); -// std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); return 0; }