Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Currency Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

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 currency book class is RWCurrencyBook.

3.3.1 Available Currencies

You can initialize a currency book class from a file. The Currency Module comes with one such file: currency_book.txt, 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.txt:

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.

3.3.3 Adding, Deleting, and Searching for Currencies

In the Currency Module 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.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.