add cpp00 ex02
This commit is contained in:
parent
b4bcf5ba28
commit
3270f3c4c9
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/17 15:55:40 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/21 18:58:19 by apommier ### ########.fr */
|
||||
/* Updated: 2022/07/20 12:19:30 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -62,7 +62,6 @@ int main(int ac, char **av)
|
||||
std::cout << "too much arguments\n";
|
||||
return (0);
|
||||
}
|
||||
//line = "nothing";
|
||||
std::cout << "Enter a command : ADD | SEARCH | EXIT" << std::endl;
|
||||
while (line != "EXIT")
|
||||
{
|
||||
@ -74,6 +73,5 @@ int main(int ac, char **av)
|
||||
else
|
||||
std::cout << "Invalid command" << std::endl;
|
||||
}
|
||||
//std::cout << "exit\n";
|
||||
return (0);
|
||||
}
|
||||
101
cpp00/ex02/Account.cpp
Normal file
101
cpp00/ex02/Account.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Account.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 12:23:50 by apommier #+# #+# */
|
||||
/* Updated: 2022/07/20 12:48:54 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Account.hpp"
|
||||
|
||||
int Account::getNbAccounts( void )
|
||||
{
|
||||
return (this->_nbAccounts);
|
||||
}
|
||||
|
||||
int Account::getTotalAmount( void )
|
||||
{
|
||||
return (this->_totalAmount);
|
||||
}
|
||||
|
||||
int Account::getNbDeposits( void )
|
||||
{
|
||||
return (this->_nbDeposits);
|
||||
}
|
||||
|
||||
int Account::getNbWithdrawals( void )
|
||||
{
|
||||
return (this->_nbWithdrawals);
|
||||
}
|
||||
|
||||
void Account::displayAccountsInfos( void )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Account::Account( int initial_deposit )
|
||||
{
|
||||
this->_nbAccounts = 0;
|
||||
this->_totalAmount = initial_deposit;
|
||||
this->_totalNbDeposits = 0;
|
||||
this->_totalNbWithdrawals = 0;
|
||||
this->_accountIndex = 0;
|
||||
this->_amount = initial_deposit;
|
||||
this->_nbDeposits = 0;
|
||||
this->_nbWithdrawals = 0;
|
||||
}
|
||||
|
||||
Account::Account()
|
||||
{
|
||||
this->_nbAccounts = 0;
|
||||
this->_totalAmount = 0;
|
||||
this->_totalNbDeposits = 0;
|
||||
this->_totalNbWithdrawals = 0;
|
||||
this->_accountIndex = 0;
|
||||
this->_amount = 0;
|
||||
this->_nbDeposits = 0;
|
||||
this->_nbWithdrawals = 0;
|
||||
}
|
||||
|
||||
Account::~Account( void )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Account::makeDeposit( int deposit )
|
||||
{
|
||||
this->_amount += deposit;
|
||||
this->_nbDeposits = 1;
|
||||
this->_totalNbDeposits++;
|
||||
}
|
||||
|
||||
bool Account::makeWithdrawal( int withdrawal )
|
||||
{
|
||||
if (this->_totalAmount >= withdrawal)
|
||||
{
|
||||
this->_nbWithdrawals = 1;
|
||||
this->_totalNbWithdrawals++;
|
||||
this->_amount -= withdrawal;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int Account::checkAmount( void ) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Account::displayStatus( void ) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Account::_displayTimestamp( void )
|
||||
{
|
||||
|
||||
}
|
||||
69
cpp00/ex02/Account.hpp
Normal file
69
cpp00/ex02/Account.hpp
Normal file
@ -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 <bm@gbu.com> //
|
||||
// //
|
||||
// ************************************************************************** //
|
||||
|
||||
|
||||
#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__ */
|
||||
@ -6,12 +6,13 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/04/15 01:47:51 by apommier #+# #+# #
|
||||
# Updated: 2022/07/18 20:52:38 by apommier ### ########.fr #
|
||||
# Updated: 2022/07/20 12:51:45 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = megaphone
|
||||
SRCS = megaphone.cpp
|
||||
NAME = a.out
|
||||
SRCS = Account.cpp\
|
||||
tests.cpp
|
||||
|
||||
OBJS = ${SRCS:.cpp=.o}
|
||||
CC = c++
|
||||
|
||||
72
cpp00/ex02/tests.cpp
Normal file
72
cpp00/ex02/tests.cpp
Normal file
@ -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 <bm@gbu.com> //
|
||||
// //
|
||||
// ************************************************************************** //
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "Account.hpp"
|
||||
|
||||
|
||||
int main( void ) {
|
||||
|
||||
typedef std::vector<Account::t> accounts_t;
|
||||
typedef std::vector<int> ints_t;
|
||||
typedef std::pair<accounts_t::iterator, ints_t::iterator> 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; -*-
|
||||
// ************************************************************************** //
|
||||
Loading…
Reference in New Issue
Block a user