From a9ddd2fe4219a3e4dd46a7248e26446d8d6b14f3 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 4 Oct 2024 13:25:15 +0200 Subject: [PATCH] Add ex02 solution --- ex02/Account.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++ ex02/Account.hpp | 69 +++++++++++++++++++++++++ ex02/Makefile | 53 ++++++++++++++++++++ ex02/tests.cpp | 72 +++++++++++++++++++++++++++ 4 files changed, 321 insertions(+) create mode 100644 ex02/Account.cpp create mode 100644 ex02/Account.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/tests.cpp diff --git a/ex02/Account.cpp b/ex02/Account.cpp new file mode 100644 index 0000000..ea45645 --- /dev/null +++ b/ex02/Account.cpp @@ -0,0 +1,127 @@ +#include "Account.hpp" +#include +#include + +int Account::_nbAccounts; +int Account::_totalAmount; +int Account::_totalNbDeposits; +int Account::_totalNbWithdrawals; + +Account::Account(int initial_deposit): + _accountIndex(_nbAccounts), + _amount(initial_deposit), + _nbDeposits(0), + _nbWithdrawals(0) +{ + ++_nbAccounts; + _totalAmount += _amount; + _displayTimestamp(); + std::cout << "index:" << _accountIndex + << ";amount:" << _amount + << ";created\n"; +} + +Account::~Account(void) +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex + << ";amount:" << _amount + << ";closed\n"; +} + +void Account::_displayTimestamp(void) +{ + std::time_t now_raw = std::time(NULL); + std::tm *now = std::localtime(&now_raw); + + std::cout << '[' + << now->tm_year + 1900 + << now->tm_mon + 1 + << now->tm_mday + << '_' + << now->tm_hour + << now->tm_min + << now->tm_sec + << "] "; +} + +void Account::makeDeposit(int deposit) +{ + _displayTimestamp(); + ++_nbDeposits; + ++_totalNbDeposits; + _amount += deposit; + _totalAmount += deposit; + std::cout << "index:" << _accountIndex + << ";p_amount:" << _amount - deposit + << ";deposit:" << deposit + << ";amount:" << _amount + << ";nb_deposits:" << _nbDeposits + << '\n'; +} + +bool Account::makeWithdrawal(int withdrawal) +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex + << ";p_amount:" << _amount; + if (withdrawal > _amount) + { + std::cout << ";withdrawal:refused\n"; + return (0); + } + ++_nbWithdrawals; + ++_totalNbWithdrawals; + _amount -= withdrawal; + _totalAmount -= withdrawal; + std::cout << ";withdrawal:" << withdrawal + << ";amount:" << _amount + << ";nb_withdrawals:" << _nbWithdrawals + << '\n'; + return (1); +} + +int Account::checkAmount(void) const +{ + return (_amount); +} + +void Account::displayStatus(void) const +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex + << ";amount:" << _amount + << ";deposits:" << _nbDeposits + << ";withdrawals:" << _nbWithdrawals + << '\n'; +} + +int Account::getNbAccounts(void) +{ + return (_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:" << _nbAccounts + << ";total:" << _totalAmount + << ";deposits:" << _totalNbDeposits + << ";withdrawals:" << _totalNbWithdrawals + << '\n'; +} diff --git a/ex02/Account.hpp b/ex02/Account.hpp new file mode 100644 index 0000000..cd31e06 --- /dev/null +++ b/ex02/Account.hpp @@ -0,0 +1,69 @@ +// ************************************************************************** // +// // +// Account.hpp for GlobalBanksters United // +// Created on : Thu Nov 20 19:43:15 1989 // +// Last update : Wed Jan 04 14:54:06 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + + +#pragma once +#ifndef __ACCOUNT_H__ +#define __ACCOUNT_H__ + +// ************************************************************************** // +// Account Class // +// ************************************************************************** // + +class Account { + + +public: + + typedef Account t; + + static int getNbAccounts( void ); + static int getTotalAmount( void ); + static int getNbDeposits( void ); + static int getNbWithdrawals( void ); + static void displayAccountsInfos( void ); + + Account( int initial_deposit ); + ~Account( void ); + + void makeDeposit( int deposit ); + bool makeWithdrawal( int withdrawal ); + int checkAmount( void ) const; + void displayStatus( void ) const; + + +private: + + static int _nbAccounts; + static int _totalAmount; + static int _totalNbDeposits; + static int _totalNbWithdrawals; + + static void _displayTimestamp( void ); + + int _accountIndex; + int _amount; + int _nbDeposits; + int _nbWithdrawals; + + Account( void ); + +}; + + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** // + + +#endif /* __ACCOUNT_H__ */ diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..6730364 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,53 @@ +CC := c++ +CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic + +ifneq ("$(wildcard .debug)","") + CFLAGS += -g +endif + +RM := rm -f + +INCDIR := inc +INCDIR += $(addsuffix /inc, $(SUBPROJECTS)); +ifneq ($(INCLUDE),) + INCLUDE := $(addprefix -I, $(INCDIR)) +endif + +SRCDIR := . + +SOURCES := tests.cpp \ + Account.cpp \ + +SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) + +OBJECTS := $(SOURCES:.cpp=.o) + +NAME := tests + +all : $(NAME) + +debug : .debug + $(MAKE) all + +nodebug : + $(RM) .debug + $(MAKE) re + +.% : + $(MAKE) fclean + touch $@ + +$(NAME) : $(OBJECTS) + $(CC) $(CFLAGS) -o $@ $^ $(LINKS) + +%.o : %.cpp + $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) + +clean : + $(RM) $(OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/ex02/tests.cpp b/ex02/tests.cpp new file mode 100644 index 0000000..0999525 --- /dev/null +++ b/ex02/tests.cpp @@ -0,0 +1,72 @@ +// ************************************************************************** // +// // +// tests.cpp for GlobalBanksters United // +// Created on : Thu Nov 20 23:45:02 1989 // +// Last update : Wed Jan 04 09:23:52 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + +#include +#include +#include +#include "Account.hpp" + + +int main( void ) { + + 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 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(); + + 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) ) { + + (*(it.first)).makeDeposit( *(it.second) ); + } + + 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) ) { + + (*(it.first)).makeWithdrawal( *(it.second) ); + } + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + return 0; +} + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** // -- 2.30.2