-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrency.cpp
More file actions
65 lines (53 loc) · 1.78 KB
/
Currency.cpp
File metadata and controls
65 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Currency.h"
#include <stdexcept>
#include <random>
#include <cctype>
#include <algorithm>
#include <iostream>
/*
* @brief Currency class
* @param currencyCode - the currency code of the currency
* @param rate - the exchange rate of the currency relative to SEK
*
* This class is used to store currency codes and convert between currencies
* using fixed rates.
*/
//Parameterized constructor
Currency::Currency(const std::string& currencyCode) : currencyCode(currencyCode) {};
// Getter for the currency code
std::string Currency::getCurrencyCode() const
{
return currencyCode;
}
std::string Currency::getCurrencyCodes()
{
// Initialize the static array of currency codes
std::string currencyCodes[5] = { "SEK", "DKK", "EUR", "GBP", "USD" };
std::string codes = "";
for (int i = 0; i < std::size(currencyCodes); i++)
{
codes += currencyCodes[i] + " ";
}
return codes;
}
// Method to convert amount from this currency to another
double Currency::convertAmount(const std::string& fromCurrencyCode, const std::string& toCurrencyCode, double amount)
{
double fromRate = getRate(fromCurrencyCode);
double toRate = getRate(toCurrencyCode);
double newAmount = (amount * fromRate) / toRate;
return newAmount;
}
// Method to get the exchange rate for a given currency code
double Currency::getRate(const std::string& currencyCode)
{
std::string upperCaseCode = currencyCode;
// Convert currency code to uppercase
std::transform(upperCaseCode.begin(), upperCaseCode.end(), upperCaseCode.begin(), ::toupper);
if (upperCaseCode == "SEK") return SEK_RATE;
if (upperCaseCode == "DKK") return DKK_RATE;
if (upperCaseCode == "EUR") return EUR_RATE;
if (upperCaseCode == "GBP") return GBP_RATE;
if (upperCaseCode == "USD") return USD_RATE;
throw std::runtime_error("Invalid currency code");
}