Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Src/Currency_Converter/conv.cpp
Original file line number Diff line number Diff line change
@@ -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<Currency> CurrencyConverter::listCurrencies() const {
vector<Currency> 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);
}
18 changes: 18 additions & 0 deletions Src/Currency_Converter/conv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CURRENCYCONVERTER_H
#define CURRENCYCONVERTER_H
#include <bits/stdc++.h>
#include "cur.h"
using namespace std;
class CurrencyConverter {
map<string, Currency> currencies;
map<string, double> 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<Currency> listCurrencies() const;
};
#endif
16 changes: 16 additions & 0 deletions Src/Currency_Converter/cur.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions Src/Currency_Converter/cur.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CURRENCY_H
#define CURRENCY_H
#include <bits/stdc++.h>
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
83 changes: 83 additions & 0 deletions Src/Currency_Converter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <bits/stdc++.h>
#include "cur.h"
#include "conv.h"
using namespace std;
using nlim = numeric_limits<streamsize>;

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;
}
Loading