diff --git a/Src/Currency_Converter/conv.cpp b/Src/Currency_Converter/conv.cpp new file mode 100644 index 0000000..a716267 --- /dev/null +++ b/Src/Currency_Converter/conv.cpp @@ -0,0 +1,37 @@ +#include "conv.h" + +CurrencyConverter::CurrencyConverter() {} + +void CurrencyConverter::addCurrency(const Currency& currency) { + currencies[currency.getCode()] = currency; +} + +void CurrencyConverter::setExchangeRate(const string& fromCurrency, const string& toCurrency, double rate) { + if (rate <= 0) + throw invalid_argument("Invalid rate"); + exchangeRates[fromCurrency + toCurrency] = rate; +} + +double CurrencyConverter::getExchangeRate(const string& fromCurrency, const string& toCurrency) const { + auto it = exchangeRates.find(fromCurrency + toCurrency); + if (it == exchangeRates.end()) + throw invalid_argument("Exchange rate not found"); + return it->second; +} + +bool CurrencyConverter::hasCurrency(const string& code) const { + return currencies.find(code) != currencies.end(); +} + +vector CurrencyConverter::listCurrencies() const { + vector result; + for (auto& x : currencies) + result.push_back(x.second); + return result; +} + +double CurrencyConverter::convert(double amount, const string& fromCurrency, const string& toCurrency) const { + if (!hasCurrency(fromCurrency) || !hasCurrency(toCurrency)) + throw invalid_argument("Invalid currency code"); + return amount * getExchangeRate(fromCurrency, toCurrency); +} diff --git a/Src/Currency_Converter/conv.h b/Src/Currency_Converter/conv.h new file mode 100644 index 0000000..24e9c7c --- /dev/null +++ b/Src/Currency_Converter/conv.h @@ -0,0 +1,18 @@ +#ifndef CURRENCYCONVERTER_H +#define CURRENCYCONVERTER_H +#include +#include "cur.h" +using namespace std; +class CurrencyConverter { + map currencies; + map exchangeRates; +public: + CurrencyConverter(); + void addCurrency(const Currency& currency); + void setExchangeRate(const string& fromCurrency, const string& toCurrency, double rate); + double convert(double amount, const string& fromCurrency, const string& toCurrency) const; + bool hasCurrency(const string& code) const; + double getExchangeRate(const string& fromCurrency, const string& toCurrency) const; + vector listCurrencies() const; +}; +#endif diff --git a/Src/Currency_Converter/cur.cpp b/Src/Currency_Converter/cur.cpp new file mode 100644 index 0000000..ac6ff92 --- /dev/null +++ b/Src/Currency_Converter/cur.cpp @@ -0,0 +1,16 @@ +#include "cur.h" + +Currency::Currency(const string& code, const string& name, const string& symbol) + : code(code), name(name), symbol(symbol) {} + +string Currency::getCode() const { + return code; +} + +string Currency::getName() const { + return name; +} + +string Currency::getSymbol() const { + return symbol; +} diff --git a/Src/Currency_Converter/cur.h b/Src/Currency_Converter/cur.h new file mode 100644 index 0000000..86aa75a --- /dev/null +++ b/Src/Currency_Converter/cur.h @@ -0,0 +1,13 @@ +#ifndef CURRENCY_H +#define CURRENCY_H +#include +using namespace std; +class Currency { + string code, name, symbol; +public: + Currency(const string& code, const string& name, const string& symbol); + string getCode() const; + string getName() const; + string getSymbol() const; +}; +#endif diff --git a/Src/Currency_Converter/main.cpp b/Src/Currency_Converter/main.cpp new file mode 100644 index 0000000..4723980 --- /dev/null +++ b/Src/Currency_Converter/main.cpp @@ -0,0 +1,83 @@ +#include +#include "cur.h" +#include "conv.h" +using namespace std; +using nlim = numeric_limits; + +void show(const CurrencyConverter& converter) { + for (auto& currency : converter.listCurrencies()) + cout << currency.getCode() << "-" << currency.getName() << "(" << currency.getSymbol() << ")\n"; + cout << endl; +} + +int main() { + CurrencyConverter converter; + converter.addCurrency(Currency("USD", "US Dollar", "$")); + converter.addCurrency(Currency("EUR", "Euro", "€")); + converter.addCurrency(Currency("INR", "Indian Rupee", "₹")); + converter.addCurrency(Currency("GBP", "British Pound", "£")); + converter.setExchangeRate("USD", "INR", 83.0); + converter.setExchangeRate("INR", "USD", 0.012); + converter.setExchangeRate("USD", "EUR", 0.93); + converter.setExchangeRate("EUR", "USD", 1.08); + converter.setExchangeRate("USD", "GBP", 0.79); + converter.setExchangeRate("GBP", "USD", 1.27); + converter.setExchangeRate("EUR", "INR", 89.0); + converter.setExchangeRate("INR", "EUR", 0.011); + int choice; + do { + cout << "\nMenu:\n1.List\n2.Convert\n3.Update\n0.Exit\n> "; + cin >> choice; + cin.ignore(nlim::max(),'\n'); + if (choice == 1) { + show(converter); + } else if (choice == 2) { + string fromCurrency, toCurrency; + double amount; + cout << "From: "; + cin >> fromCurrency; + cout << "To: "; + cin >> toCurrency; + cout << "Amount: "; + cin >> amount; + if (cin.fail() || amount <= 0) { + cout << "Invalid input\n"; + cin.clear(); + cin.ignore(nlim::max(),'\n'); + continue; + } + try { + double result = converter.convert(amount, fromCurrency, toCurrency); + cout << fixed << setprecision(2) << amount << fromCurrency << "=" << result << toCurrency << "\n"; + } catch (const exception& e) { + cout << "Error: " << e.what() << "\n"; + } + } else if (choice == 3) { + string fromCurrency, toCurrency; + double rate; + cout << "From: "; + cin >> fromCurrency; + cout << "To: "; + cin >> toCurrency; + cout << "Rate(1" << fromCurrency << "=" << toCurrency << "): "; + cin >> rate; + if (cin.fail() || rate <= 0) { + cout << "Invalid rate\n"; + cin.clear(); + cin.ignore(nlim::max(),'\n'); + continue; + } + try { + converter.setExchangeRate(fromCurrency, toCurrency, rate); + cout << "Updated\n"; + } catch (const exception& e) { + cout << "Error: " << e.what() << "\n"; + } + } else if (choice == 0) { + cout << "bye\n"; + } else { + cout << "bad\n"; + } + } while (choice != 0); + return 0; +}