Performing Currency Exchanges

The following code shows how exchange rates and tables, exchange objects and groups, and the exchange factory come together to perform a currency exchange. The comments in the code detail each part of the program.

 

#include <rw/currency/money.h>

#include <rw/currency/euroexchggrp.h>

#include <rw/currency/divexchggrp.h>

#include <rw/currency/exchgfact.h>

.

.

.

// Create and initialize an exchange rate table

// to be used for all currency conversions.

RWExchangeRateTable rateTable;

.

.

.

// Create an exchange factory that uses the exchange rate

// table created above. By default the exchange factory contains

// the multiplication exchange group.

RWExchangeFactory<double> exchangeFactory( rateTable );

 

// Add a Euro exchange group

ifstream euroCurrenciesFile( "euro_currencies.txt" );

RWExchangeGroup<double> euroGroup =

RWEuroGroup<double>::create( euroCurrenciesFile );

exchangeFactory.appendExchangeGroup( euroGroup );

 

// Add a division exchange group

RWExchangeGroup<double> divGroup =

RWDivisionGroup<double>::create();

exchangeFactory.appendExchangeGroup( divGroup );

 

// The exchange factory contains three exchange groups:

// RWMultiplicationGroup, RWEuroGroup, and RWDivisionGroup,

// in that order.

 

// Get an exchange object from the factory for converting Belgian

// Francs to Canadian Dollars. Since the factory's exchange rate

// table contains a conversion factor for converting Belgian Francs

// to Canadian Dollars, it creates an exchange object containing

// an RWMultiplicationExchange implementation. Exchanges are

// computed by multiplying the source amount by the conversion

// conversion factor.

RWExchange<double> befToCadExchanger =

exchangeFactory.getExchange( "BEF", "CAD" );

 

// Get an exchange object from the factory for converting Belgian

// Francs to French Francs. The factory's exchange rate table does

// not contain a conversion factor for converting Belgian Francs to

// French Francs. Belgium and France are members of the EMU, so

// instead, the exchange rate table contains conversion factors for

// converting Euros to Belgian Francs and Euros to French Francs.

// This means that the Factory creates an RWExchange object with an

// RWTriangularExchange implementation.

RWExchange<double> befToFrfExchanger =

exchangeFactory.getExchange( "BEF", "FRF" );

 

// Get an exchange object from the factory for converting Belgian

// Francs to Euros. The factory's exchange rate table does not

// contain a conversion factor for converting Belgian Francs to

// Euros and the Euro is not a local Euro currency. There is,

// however, a conversion factor for converting Euros to Belgian

// Francs in the factory's exchange rate table. Thus the last

// exchange group in the list, RWDivisionGroup, creates an exchange

// object with an RWDivisionExchange implementation.

RWExchange<double> befToEURExchanger =

exchangeFactory.getExchange( "BEF", "EUR" );

 

// Do the exchanges, using the exchange function of each exchange object.

RWMoney<double> moneyBEF( 189972.76, "BEF" );

if ( befToCadExchanger.isValid() )

{

RWMoney<double> moneyCAD = befToCadExchanger.exchange(moneyBEF);

}

 

if ( befToFrfExchanger.isValid() )

{

RWMoney<double> moneyFRF = befToFrfExchanger.exchange(moneyBEF);

}

 

if ( befToEURExchanger.isValid() )

{

RWMoney<double> moneyEUR = befToEURExchanger.exchange(moneyBEF);

}