Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

3.3 Collections of Currencies

A currency book object is a collection of currency objects. In a currency book, each currency is identified by a unique mnemonic. No two currencies with the same mnemonic may exist in the same currency book.

The C++ version of the currency book class is RWCurrencyBook, while the Java version of the class is CurrencyBook.

3.3.1 Available Currencies

You can initialize a currency book class from a file. Money.h++ comes with one such file: currency_book.in, which is located in the examples directory. Appendix B lists the mnemonic and the country for each currency in the provided file.

3.3.2 Initializing a Currency Book

RWCurrencyBook provides two restoreFrom() member functions, and two right shift operators, operator >>(), to restore the currency book from an RWFile or a virtual stream. Similarly, it provides two saveOn() member functions and two left shift operators, operator <<(), to persist a currency book to an RWFile or a virtual stream.

The class also provides an initialize() member function that lets you initialize from an istream. The following code shows how to use initialize() to populate an RWCurrencyBook, myBook from the file currency_book.in:

Each entry in the currency book is a different currency. A currency specification begins with the token BEGIN_CURRENCY, on a line by itself. Each attribute of the currency is specified as a name/value pair, one per line. The name/value separator is the equal sign `='. The end of a currency specification is indicated by the token END_CURRENCY. Comment lines start with a # character and are automatically skipped. For example:

Name matching is case-insensitive and the only required field for each currency in a currency book is the mnemonic.

In Java, a CurrencyBook 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. For example, the following Java also uses currency_book.in to initialize a currency book.

3.3.3 Adding, Deleting, and Searching for Currencies

In the Money.h++ C++ library, you can add individual currencies to a currency book using function RWCurrencyBook::insert(). The insert() function attempts to insert a currency object by seeing whether it already exists in the currency book, and, if it does not, inserting it, then returning TRUE. If a currency object with the same mnemonic as the currency you are trying to insert already exists, the insert() function will simply return a value of FALSE.

You can delete individual currencies from the currency book using function RWCurrencyBook::remove(). The remove() function searches the currency book for a provided mnemonic, then, if it finds it, removes its associated currency and returns TRUE. If remove() does not find the mnemonic, it returns FALSE.

To remove all currencies from the currency book, use function RWCurrencyBook::clear().

Several functions can be used to determine whether a given currency exists in a currency book.

Function contains() simply returns TRUE if the currency book contains the currency with the given mnemonic.

Function find() finds the currency indicated by a given mnemonic, places it in a provided currency object, then returns TRUE. If no such currency exists, the currency object remains unchanged and the function returns FALSE.

The getAllCurrencies() and getAllCurrencyMnemonics classes provide a list of all the currencies or a list of all the mnemonics in a given currency book object.

The Money.h++ Java library provides the functionality described above. For more information, see the CurrencyBook API documentation.



Previous fileTop of DocumentContentsIndexNext file

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