Using the Custom Classes
The groupexam example demonstrates how to add the classes described in Class USDollarBaseExchange and Class USDollarBaseGroup to an exchange factory, and how to perform some currency exchanges. Here is an excerpt from the code that creates and populates a currency exchange factory:
 
//set up exchange rate table rateTable
.
.
.
// Construct an exchange factory with the exchange rate table.
 
RWExchangeFactory<Decimal> exchangeFactory( rateTable );
 
// Exchange groups use a handle/body design. The handle class is
// RWExchangeGroup. Instances of this class contain a pointer to
// an implementation class. The static method
// RWEuroGroup<Decimal>::create() creates an RWExchangeGroup
// object with an RWEuroGroup implementation. Function
// appendExchangeGroup appends a Euro exchange group to the
// factory.
 
exchangeFactory.appendExchangeGroup(
RWEuroGroup<Decimal>::create(euroFile) );
 
// Create a USDollarBaseGroup exchange group and append it to the
// exchange factory.
USDollarBaseGroup<Decimal>* grpImpl =
new USDollarBaseGroup<Decimal>;
exchangeFactory.appendExchangeGroup(
RWExchangeGroup<Decimal>(grpImpl) );
// Create a division exchange group and insert into the factory
// in the second position.
exchangeFactory.insertExchangeGroupAt( 1,
RWDivisionGroup<Decimal>::create() );
The currency exchange factory contains four exchange groups, in the following order: RWMultiplicationGroup, RWDivisionGroup, RWEuroGroup, and USDollarBaseGroup. When the exchange factory gets a request for an exchange object, it will ask each exchange group, in order, to create the exchange object. For example, the following line of code requests an exchange object for converting currency ARP to AUD:
 
RWExchange<Decimal> exchanger =
exchangeFactory.getExchange( "ARP", "AUD" );
In this example, the exchange object, exchanger, is used to perform the actual currency conversion. The example defines function doConversion(), which takes a source currency and an exchange object, and returns a RWMoney object target, which contains the conversion. The doConversion() function prints the results as well. Here is its code:
 
RWMoney<Decimal> doConversion( const RWMoney<Decimal>& source,
const RWExchange<Decimal>& exchanger )
{
RWMoney<Decimal> target = round( exchanger.exchange(source),2U );
cout << "\nUsing exchange method: " << exchanger.name() << endl;
cout << source.amount() << " " << source.currency() << " = " <<
target.amount() << " " << target.currency() << endl;
 
return target;
}
Let’s see how all of these pieces are brought together. The following code excerpt creates two RWMoney objects, source and target, requests an exchange object from the factory, and, if the exchange object is valid, uses the doConversion() function to perform a conversion:
 
RWMoney<Decimal> source( Decimal("8976.80"), "ARP" );
RWMoney<Decimal> target;
RWExchange<Decimal> exchanger =
exchangeFactory.getExchange( "ARP", "AUD" );
if ( !exchanger.isValid() )
{
cout << "No exchange object for ARP to AUD" << endl;
}
else
{
target = doConversion( source, exchanger );
}
You can see the complete source code for this example in buildspace\examples\currency\groupexam.cpp.