Calculating a container of monies is straight forward, but can be problematic when done by hand. Instead, Pecunia supplies functions for performing common mathematical operations for both a currency object and a container of money objects. Below is an example of calculating several purchases into a single total to be displayed in various currencies. These functions perform their summations using as few conversions as possible, unlike when done using the standard functions which would create conversions with each different currency.
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <pecunia/Codes.h>
#include <pecunia/FloatingPoint.h>
#include <pecunia/Math.h>
#include <pecunia/Money.h>
#include <pecunia/SetUp.h>
/*
* Conversion function to go from one currency to another. This is hard coded, but in a real world
* usage it would be have a mapping of the latest currency exchange rates.
*/
pecunia::FloatingPoint converter(
const pecunia::currency::Currency& from,
const pecunia::currency::Currency& to
)
{
switch (from)
{
case pecunia::currency::Currency::CAD:
switch (to)
{
case pecunia::currency::Currency::CAD:
return pecunia::FloatingPoint{1.0};
case pecunia::currency::Currency::EUR:
return pecunia::FloatingPoint{0.69};
case pecunia::currency::Currency::USD:
return pecunia::FloatingPoint{0.73};
default:
throw std::runtime_error{"Conversion not present for CAD."};
}
case pecunia::currency::Currency::EUR:
switch (to)
{
case pecunia::currency::Currency::CAD:
return pecunia::FloatingPoint{1.45};
case pecunia::currency::Currency::EUR:
return pecunia::FloatingPoint{1.0};
case pecunia::currency::Currency::USD:
return pecunia::FloatingPoint{1.05};
default:
throw std::runtime_error{"Conversion not present for EUR."};
}
case pecunia::currency::Currency::USD:
switch (to)
{
case pecunia::currency::Currency::CAD:
return pecunia::FloatingPoint{1.37};
case pecunia::currency::Currency::EUR:
return pecunia::FloatingPoint{0.95};
case pecunia::currency::Currency::USD:
return pecunia::FloatingPoint{1.0};
default:
throw std::runtime_error{"Conversion not present for USD."};
}
default:
throw std::runtime_error{"Conversion not present."};
}
}
int main(int argc, char* argv[])
{
// Calling this function sets up the library to use the "converter" function when performing
// any operation between different currencies.
pecunia::currency::setUpCurrency(&converter);
const std::vector<pecunia::currency::Money> purchases{
{
pecunia::currency::Money{pecunia::currency::Currency::USD, 10, 15u},
pecunia::currency::Money{pecunia::currency::Currency::EUR, 20, 94u},
pecunia::currency::Money{pecunia::currency::Currency::USD, 30, 83u},
pecunia::currency::Money{pecunia::currency::Currency::CAD, 40, 72u},
pecunia::currency::Money{pecunia::currency::Currency::EUR, 50, 61u}
}
};
// Calculate the total of purchases in terms of Canadian Dollars.
const auto totalCanada{pecunia::math::sum(pecunia::currency::Currency::CAD, purchases)};
// Calculate the total of purchases in terms of United States Dollars.
const auto totalUsa{pecunia::math::sum(pecunia::currency::Currency::USD, purchases)};
// Calculate the total of purchases in terms of Euros.
const auto totalEu{pecunia::math::sum(pecunia::currency::Currency::EUR, purchases)};
std::cout << "Totals: " << totalCanada << ", " << totalUsa << ", " << totalEu << std::endl;
// Standard Output Buffer contains: Totals: 200.6101CAD, 145.8331USD, 138.5778EUR
return 0;
}