
The following Java 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. This code provides the same functionality as the code in Section 5.6.
import com.roguewave.money.currency.v1_0.*;
.
.
.
// Create and initialize an exchange rate table
// to be used for all currency conversions.
ExchangeRateTable rateTable;
.
.
.
// Create an exchange factory that uses the exchange rate
// table created above. By default the exchange factory contains
// the multiplication exchange group.
ExchangeFactory exchangeFactory = new ExchangeFactory(rateTable);
// Add a Euro exchange group
Reader reader = new FileReader(new File("euro_currencies.in"));
ExchangeGroup euroGroup = new EuroGroup(reader);
exchangeFactory.appendExchangeGroup(euroGroup);
// Add a division exchange group
ExchangeGroup divGroup = new DivisionGroup();
exchangeFactory.appendExchangeGroup(divGroup);
// The exchange factory contains three exchange groups:
// a MultiplicationGroup, a EuroGroup, and a DivisionGroup,
// 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 a MultiplicationExchange object.
// Exchanges will be computed by multiplying the source amount by
// the conversion factor.
Exchange 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 a TriangularExchange object.
Exchange 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, a DivisionGroup, creates a
// DivisionExchange object.
Exchange befToEURExchanger =
exchangeFactory.getExchange("BEF", "EUR");
// Do the exchanges, using the exchange method of each
// exchange object.
Money moneyBEF = new Money(189972.76d, "BEF");
if(befToCadExchanger != null) {
Money moneyCAD = befToCadExchanger.exchange(moneyBEF);
}
if(befToFrfExchanger != null) {
Money moneyFRF = befToFrfExchanger.exchange(moneyBEF);
}
if(befToEURExchanger != null) {
Money moneyEUR = befToEURExchanger.exchange(moneyBEF);
}
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.