I have four files in c++

  1. BankAccount.h
  2. BankDatabase.h
  3. Main.cpp

//BankAccount.h

#include <string>

class BankAccount
{
    public:
        BankAccount(int accNumber,const std::string& accName);

        void setAccNumber(const int accNumber);
        int getAccNumber() const;

        void setAccName(const std::string& clientName);
        std::string getAccName() const ;

    protected:
        int mAccNumber;
        std::string mAccName;

};

BankAccount::BankAccount(const int accNumber,
        const std::string& accName):mAccNumber(accNumber),mAccName(accName){}


void BankAccount::setAccNumber(const int accNumber)
{
   mAccNumber = accNumber;     
}

void BankAccount::setAccName(const std::string& accName)
{
    mAccName = accName;
}

int BankAccount::getAccNumber() const 
{
    return mAccNumber;
}

std::string BankAccount::getAccName() const {
    return mAccName;
}

// BankDatabase.h

#include <map>
#include <iostream>
#include <stdexcept>
#include "BankAccount.h"


class BankDatabase
{
    public:
        BankDatabase();

        void addAccount(const BankAccount& acc);
        void deleteAccount(int accNumber);

        BankAccount& findAccount(int accNumber) 
            throw (std::out_of_range);

        BankAccount& findAccount(std::string& accName) 
            throw (std::out_of_range);


        void mergeDatabase(BankDatabase& db);

    protected:
        std::map<int,BankAccount> mAccounts;

};


BankDatabase::BankDatabase(){}

void BankDatabase::addAccount(const BankAccount& acc)
{
    std::pair<std::map<int,BankAccount>::iterator,bool> res =  
        mAccounts.insert(std::make_pair(acc.getAccNumber(),acc));

    if(!res.second)
        std::cout << "Account cannot be  added ";
}

void  BankDatabase::deleteAccount(int accNumber) 
{
    if( mAccounts.count(accNumber))
        mAccounts.erase(accNumber);
}

BankAccount& BankDatabase::findAccount(int accNumber)
    throw(std::out_of_range)
{
    auto iter =  mAccounts.find(accNumber);

    if(iter != mAccounts.end())
        return iter->second;
    throw std::out_of_range("No account number with this name ");

}

BankAccount& BankDatabase::findAccount(std::string& accName) 
    throw(std::out_of_range)
{
    for(auto& p : mAccounts) {
        if ( p.second.getAccName() == accName )
            return p.second;
    }
    throw std::out_of_range(" No account with this name ");
}

// Main.cpp

#include <iostream>
#include "BankDatabase.h"


int main()
{
    BankDatabase db;
    db.addAccount(BankAccount(1,"james"));
    db.addAccount(BankAccount(2,"johnson"));
    db.addAccount(BankAccount(3,"kamal"));
    db.addAccount(BankAccount(4,"appu"));

    // find account name based on account number

    std::cout << db.findAccount(1).getAccName();

    // find account number based on account name

    std::cout << db.findAccount("james");

    // delete an account

    db.deleteAccount(db.findAccount("james").getAccNumber());

    // find the account

    std::cout << db.findAccount("james");

    //Merge database

    db.mergeDatabase(db);

    return 0;
}

At this particular statment " std::cout << db.findAccount("james"); " , i got an error

error: invalid conversion from ‘const char*’ to ‘int’

But i defined findAccount(int) and findAccount(string) in BankDatabase.h

Can someone help in this issue.

I think you mean to output the BankAccount& to std::cout but forgot to call BankAccount::getAccName();

std::cout << db.findAccount("james").getAccName() << std::endl;

Is what you need.

You may like some fixes/edits ... as per the following 3 files:

file 1:

// BankAccount.h //

#include <string>

class BankAccount
{
    public:
        BankAccount( const int accNumber, const std::string& accName );

        void setAccNumber( const int accNumber );
        int getAccNumber() const;

        void setAccName( const std::string& clientName );
        std::string getAccName() const ;

    protected:
        int mAccNumber;
        std::string mAccName;

} ;

BankAccount::BankAccount( const int accNumber, const std::string& accName )
    : mAccNumber(accNumber), mAccName(accName) {}


void BankAccount::setAccNumber( const int accNumber )
{
   mAccNumber = accNumber;     
}

void BankAccount::setAccName( const std::string& accName )
{
    mAccName = accName;
}

int BankAccount::getAccNumber() const
{
    return mAccNumber;
}

std::string BankAccount::getAccName() const
{
    return mAccName;
}

file 2:

// BankDatabase.h //

#include <map>
#include <iostream>
#include <stdexcept>

#include "BankAccount.h"


class BankDatabase
{
    public:
        BankDatabase();

        void addAccount( const BankAccount& acc );
        void deleteAccount( const int accNumber );

        BankAccount& findAccount( const int accNumber ) throw ( std::out_of_range );
        BankAccount& findAccount( const std::string& accName ) throw ( std::out_of_range );

        void mergeDatabase( BankDatabase& db );

    protected:
        std::map< int, BankAccount > mAccounts;

    friend std::ostream& operator << ( std::ostream& os, const BankDatabase& db )
    {
        for( auto i : db.mAccounts ) os << i.second.getAccNumber() << ", " << i.second.getAccName() << '\n';
        return os;
    }
} ;


BankDatabase::BankDatabase(){}

void BankDatabase::addAccount( const BankAccount& acc )
{
    if ( ! mAccounts.count( acc.getAccNumber() ) )
       mAccounts.insert( std::pair<int, BankAccount > ( acc.getAccNumber(), acc ) );
    else
        std::cout << "Account '" << acc.getAccNumber() << "' exists already so can NOT be created ...\n";
}

void  BankDatabase::deleteAccount( const int accNumber )
{
    if( mAccounts.count(accNumber) ) mAccounts.erase( accNumber );
    else
        std::cout << "Account " << accNumber << " does not exist ... so can NOT be erased ...\n";
}

BankAccount& BankDatabase::findAccount( const int accNumber ) throw( std::out_of_range )
{
    auto iter = mAccounts.find( accNumber ); // map find //
    if( iter != mAccounts.end() ) return iter->second;
    throw std::out_of_range( "No account number with number '" + std::to_string(accNumber) + "'\n" );
}

BankAccount& BankDatabase::findAccount( const std::string& accName ) throw( std::out_of_range )
{
    for( auto& p : mAccounts )
    {
        if ( p.second.getAccName() == accName ) return p.second;
    }
    throw std::out_of_range( "No account with name '" + accName + "'\n" );
}    

file 3:

// BankMain.cpp //

#include <iostream>

#include "BankDatabase.h"


int main()
{
    BankDatabase db;
    db.addAccount( BankAccount(1, "james") );
    db.addAccount( BankAccount(2, "johnson") );
    db.addAccount( BankAccount(3, "kamal") );
    db.addAccount( BankAccount(4, "appu") );

    std::cout << "All accounts so far ...\n";
    std::cout << db << '\n';

    try
    {
        std::cout << "Find account name based on account number 1 ...\n";
        std::cout << "'" << db.findAccount(1).getAccName() << "'\n";

        std::cout << "Find account number based on account name 'kamal' ...\n";
        std::cout << "'" << db.findAccount("kamal").getAccNumber() << "'\n";

        std::cout << "Delete an account by name 'james' ...\n";
        db.deleteAccount (db.findAccount("james").getAccNumber() );

        std::cout << "Now try to find the account number searching by name 'james' ...\n";
        std::cout << "'" << db.findAccount("james").getAccNumber() << "'\n";
    }
    catch( std::exception& ex )
    {
        std::cerr << ex.what() << '\n';
    }

    std::cout << "All accounts so far ...\n";
    std::cout << db << '\n';

    //std::cout << "merge database ...\n";
    //db.mergeDatabase(db);

    std::cout << "Press 'Enter' to continue/exit ... "  << std::flush;
    std::cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.