Application Security Testing: An Integral Part of DevOps
During a recent project, I needed to deal with different currencies. To cope with this requirement, I developed a mechanism for handling different currencies; I would like to share this with you.
The Currency Class
The basic element of the currency handling mechanism is a class called Currency. A currency instance consists of Notes, Coins, and a currency letter. An example is 90.5 $ in which there are 90 Notes, 50 Coins, and the currency letter is $. The Currency class contains these three data members.
The functions of the Currency class include four different constructors and simple functions for setting and obtaining the above-mentioned data values. The values can be provided in the form of notes and coins or in the form of float/double values. Then, there are functions for performing operations with currencies, such as addition, subtraction, multiplication, division, obtaining percentages, and making comparisons. Overloaded operators are used for these operations for ease of use. A Boolean data member and its associated function; it which holds and provides (respectively) information about the last operation—whether that operation was carried out successfully or some error occurred. Another function that provides string form of currency exists.
Then, there are a few static members of this class. They are very important and are used to carry out conversions of currencies.
Other Elements of the Mechanism
Following are other elements of the mechanism that are defined in Currency.h file.
- There is an enumerated data, cname, that defines the names of all the countries whose currencies are being used.
- There is a constant array of cname, called COUNTRIES, that consists of all the countries defined in cname.
- There is a constant array of strings called CURR_LETTERS; it consists of currency letters of currencies of all countries defined in cname.
- There is a constant variable of cname called DEFAULT_COUNTRY. The default country is used for currency conversion rates (as discussed in the following point).
- There is a constant array of float/double, called BASE_RATES. It consists of conversion rates of the currency of the default country to currencies of all other countries.
- There is a constant variable of short integer, called MAX_COUNTIRES, that holds the total number of countries/currencies being used.
How the Mechanism Works
The values of cname, COUNTRIES, CURR_LETTERS, and BASE_RATES are defined in the same sequence. It means that, if the first value in cname is America, the first value of COUNTRIES will also be America, the first value of CURR_LETTERS will be $, and the first value of BASE_RATES will be the conversion rate required to convert the currency of the default country into dollars. This symmetry of sequence allows the mechanism to work easily using respective array indices.
The Currency class has a static data member, called currency_rates; it is a two-dimensional array used to hold the currency conversion rates of all currencies. A static member function called init_currency_rates is used to initialize the values of currency_rates. This function must be called at program startup, so that the values of currency_rates can be initialized at startup and used later on. This function uses the constant array BASE_RATES to determine the conversion rates of currencies of all countries to currencies of all other countries.
The Currency class has two versions of a static function, called convert_currency, that are used to convert an amount of one currency into another currency. Once the currency conversion rates are initialized at startup, any of these functions can be called to convert amounts in different currencies.
Adding More Countries and Currencies
There are currently eight countries/currencies used in the sample project. Adding more countries in the mechanism is fairly simple. You have to amend in following five places, which all exist in the Currency.h file:
- Add the required country name in the definition of cname.
- Add the required country name in the COUNTRIES array in same place as in cname.
- Add the currency letter of the required country in the CURR_LETTERS array in same place as in cname.
- Add the conversion rate of the currency of the default country into the currency of required country in the BASE_RATES array in same place as in cname.
- Increment the constant MAX_COUNTRIES to reflect change in all areas.
Some Conventions Used and Limitations
The following conventions are used in creating the Currency class; they were suitable for my project. The changes can be made easily according to any particular functionality.
- The notes and coins system is used. For notes, the ULONG data type is used; it can only contain values ranges from 0 to 4,294,967,295. If you require holding larger values, you can use __int64 or ULONGLONG. If even larger values are required, you can use double numbers instead of the notes and coins system. In that case, the data members and respective functions will be changed, but the mechanism will remain the same.
- Negative currencies are not allowed. Hence, the ULONG data type is used for notes (instead of LONG).
- Because negative currencies are not allowed, subtraction of a higher currency amount from lower currency amount is not allowed.
- Multiplications, divisions, and percentages are allowed only with numbers other than currencies. This means you can multiply 526.65 $ with the number 5.6, but not with 5.6 $.
- In comparison, the deviation of plus-minus 1 in coins is kept acceptable. It means 1.5 $ is equal to both 1.4 $ and 1.6 $ (also 1.5 $, of course).
- The currency letters are added in urgency and carelessly; for example, E is written for Euro. The same can be resolved by adding the proper currency letters of the currencies.
The Sample Project
To demonstrate the use of the currency mechanism, a simple Currency Calculator is used. You can use this currency calculator to convert amounts in different currencies and to add, subtract, multiply, and divide the currencies. The calculator is fairly simple and was just created to demonstrate some of the functionalities of the subject.