-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shared - Color Improvements + Start Currency Conversion Models
- Loading branch information
Showing
7 changed files
with
238 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef CURRENCYCONVERSION_H | ||
#define CURRENCYCONVERSION_H | ||
|
||
#include <string> | ||
|
||
namespace Nickvision::Money::Shared::Models | ||
{ | ||
/** | ||
* @brief A model of a currency conversion. | ||
*/ | ||
class CurrencyConversion | ||
{ | ||
public: | ||
/** | ||
* @brief Constructs a CurrencyConversion. | ||
* @param sourceCurrency The currency code of the source amount | ||
* @param sourceAmount The source amount to convert | ||
* @param resultCurrency The currency code of the result amount | ||
* @param conversionRate The rate of conversion from the source currency to the result currency | ||
*/ | ||
CurrencyConversion(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency, double conversionRate); | ||
/** | ||
* @brief Gets the currency code of the source amount. | ||
* @return The source currency code | ||
*/ | ||
const std::string& getSourceCurrency() const; | ||
/** | ||
* @brief Gets the source amount to convert. | ||
* @return The source amount | ||
*/ | ||
double getSourceAmount() const; | ||
/** | ||
* @brief Gets the currency code of the result amount. | ||
* @return The result currency code | ||
*/ | ||
const std::string& getResultCurrency() const; | ||
/** | ||
* @brief Gets the rate of conversion from the source currency to the result currency. | ||
* @return The conversion rate | ||
*/ | ||
double getConversionRate() const; | ||
/** | ||
* @brief Gets the result amount using the conversion rate. | ||
* @return The result amount | ||
*/ | ||
double getResultAmount() const; | ||
|
||
private: | ||
std::string m_sourceCurrency; | ||
double m_sourceAmount; | ||
std::string m_resultCurrency; | ||
double m_conversionRate; | ||
}; | ||
} | ||
|
||
#endif //CURRENTCONVERSION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef CURRENCYCONVERSIONSERVICE_H | ||
#define CURRENCYCONVERSIONSERVICE_H | ||
|
||
#include <string> | ||
#include <optional> | ||
#include <unordered_map> | ||
#include "currencyconversion.h" | ||
|
||
namespace Nickvision::Money::Shared::Models::CurrencyConversionService | ||
{ | ||
/** | ||
* @brief Converts a sourceAmount from the sourceCurrency to the resultCurrency. | ||
* @param sourceCurrency The currency code of the source amount | ||
* @param sourceAmount The source amount to convert | ||
* @param resultCurrency The currency code of the result amount | ||
* @return The CurrencyConversion object if successful, else std::nullopt | ||
*/ | ||
std::optional<CurrencyConversion> convert(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency); | ||
/** | ||
* @brief Gets a map of conversion rates from the sourceCurrency to other currencies. | ||
* @brief This method will cache the data for the sourceCurrency on disk. | ||
* @param sourceCurrency The currency code to get converting rates for | ||
* @return The map of conversion rates | ||
*/ | ||
std::unordered_map<std::string, double> getConversionRates(const std::string& sourceCurrency); | ||
} | ||
|
||
#endif //CURRENCYCONVERSIONSERVICE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "models/currencyconversion.h" | ||
|
||
namespace Nickvision::Money::Shared::Models | ||
{ | ||
CurrencyConversion::CurrencyConversion(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency, double conversionRate) | ||
: m_sourceCurrency{ sourceCurrency }, | ||
m_sourceAmount{ sourceAmount }, | ||
m_resultCurrency{ resultCurrency }, | ||
m_conversionRate{ m_conversionRate } | ||
{ | ||
|
||
} | ||
|
||
const std::string& CurrencyConversion::getSourceCurrency() const | ||
{ | ||
return m_sourceCurrency; | ||
} | ||
|
||
double CurrencyConversion::getSourceAmount() const | ||
{ | ||
return m_sourceAmount; | ||
} | ||
|
||
const std::string& CurrencyConversion::getResultCurrency() const | ||
{ | ||
return m_resultCurrency; | ||
} | ||
|
||
double CurrencyConversion::getConversionRate() const | ||
{ | ||
return m_conversionRate; | ||
} | ||
|
||
double CurrencyConversion::getResultAmount() const | ||
{ | ||
return m_sourceAmount * m_conversionRate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "models/currencyconversionservice.h" | ||
|
||
namespace Nickvision::Money::Shared::Models | ||
{ | ||
std::optional<CurrencyConversion> CurrencyConversionService::convert(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency) | ||
{ | ||
if(sourceCurrency == resultCurrency) | ||
{ | ||
return CurrencyConversion{ sourceCurrency, sourceAmount, resultCurrency, 1 }; | ||
} | ||
std::unordered_map<std::string, double> rates{ getConversionRates(sourceCurrency) }; | ||
if(rates.empty() || !rates.contains(resultCurrency)) | ||
{ | ||
return std::nullopt; | ||
} | ||
return CurrencyConversion{ sourceCurrency, sourceAmount, resultCurrency, rates[resultCurrency] }; | ||
} | ||
|
||
std::unordered_map<std::string, double> CurrencyConversionService::getConversionRates(const std::string& sourceCurrency) | ||
{ | ||
return {}; | ||
} | ||
} |