Most currency exchange rates are published in the form:
1 US Dollar = 1.5455 Canadian Dollars
In Money.h++ parlance, 1.5455 is a multiplicative conversion factor for converting US Dollars to Canadian Dollars. This means that you multiply an amount in US Dollars by 1.5455 to convert to Canadian Dollars. C++ class RWExchangeRate, ExchangeRate in Java, encapsulates the information shown above by storing a source currency, a target currency and, by convention, a multiplicative conversion factor. The following C++ example shows how you initialize RWExchangeRate object usToCan with the factor shown above:
#include <rw/money/exchgrate.h> RWExchangeRate usToCan("USD","CAD",1.5455);
Here's the same initialization in Java:
import com.roguewave.money.currency.v1_0.*; ExchangeRate usToCan = new ExchangeRate("USD", "CAD" , 1.5455d);
RWExchangeRate provides members that can return or set an object's source currency mnemonic, target currency mnemonic or rate. It also provides functions and operators that can persist an object to, and restore an object from a virtual stream or a file. In Java, objects can be persisted to a file, string, or stream using the java.io classes.
C++ class RWExchangeRateTable, ExchangeRateTable in Java, stores exchange rates as unique pairs of source and target currencies and an associated rate. No two exchange rates in the table have the same source/target currency pair.
The RWExchangeRateTable class can be initialized from a stream, in particular, from an ifstream (file), using the initialize() member function. The format for the stream is:
BEGIN_EXCHANGE source=### target=### rate=### END_EXCHANGE
For example, the following C++ code initializes exchange rate table rates from the file exchange_rates.in.
#include<rw/money/exchgratetbl.h> ifstream strm("exchange_rates.in"); RWExchangeRateTable rates; rates.initialize(strm);
In Java, an ExchangeRateTable is initialized in a similar fashion with a java.io.reader. The data format and the rules are identical to those for C++. This means that the same data files can be used for both C++ and Java applications. Here is a Java example that uses the same exchange_rates.in file to initialize a table of rates:
import java.io.* import com.roguewave.money.currency.v1_0.*; Reader reader = new FileReader(new File("exchange_rates.in")); ExchangeRateTable rates = ExchangeRateTable.initialize(reader);
An RWExchangeRateTable, when combined with one or more currency exchange groups, provides the basis for an exchange factory that can be used to convert money from one currency to another. Section 5.5 shows how to set up a factory.
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.