Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

5.2 Exchange Rates and Exchange Rate Tables

Most currency exchange rates are published in the form:

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:

Here's the same initialization in Java:

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:

For example, the following C++ code initializes exchange rate table rates from the file exchange_rates.in.

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:

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.


Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.