Skip to content

Commit 82d8412

Browse files
committed
feat: Implement C++ Currency Converter (#4)
1 parent fcd5a2a commit 82d8412

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

Src/Currency_Converter/conv.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "conv.h"
2+
3+
CurrencyConverter::CurrencyConverter() {}
4+
5+
void CurrencyConverter::addCurrency(const Currency& currency) {
6+
currencies[currency.getCode()] = currency;
7+
}
8+
9+
void CurrencyConverter::setExchangeRate(const string& fromCurrency, const string& toCurrency, double rate) {
10+
if (rate <= 0)
11+
throw invalid_argument("Invalid rate");
12+
exchangeRates[fromCurrency + toCurrency] = rate;
13+
}
14+
15+
double CurrencyConverter::getExchangeRate(const string& fromCurrency, const string& toCurrency) const {
16+
auto it = exchangeRates.find(fromCurrency + toCurrency);
17+
if (it == exchangeRates.end())
18+
throw invalid_argument("Exchange rate not found");
19+
return it->second;
20+
}
21+
22+
bool CurrencyConverter::hasCurrency(const string& code) const {
23+
return currencies.find(code) != currencies.end();
24+
}
25+
26+
vector<Currency> CurrencyConverter::listCurrencies() const {
27+
vector<Currency> result;
28+
for (auto& x : currencies)
29+
result.push_back(x.second);
30+
return result;
31+
}
32+
33+
double CurrencyConverter::convert(double amount, const string& fromCurrency, const string& toCurrency) const {
34+
if (!hasCurrency(fromCurrency) || !hasCurrency(toCurrency))
35+
throw invalid_argument("Invalid currency code");
36+
return amount * getExchangeRate(fromCurrency, toCurrency);
37+
}

Src/Currency_Converter/conv.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CURRENCYCONVERTER_H
2+
#define CURRENCYCONVERTER_H
3+
#include <bits/stdc++.h>
4+
#include "cur.h"
5+
using namespace std;
6+
class CurrencyConverter {
7+
map<string, Currency> currencies;
8+
map<string, double> exchangeRates;
9+
public:
10+
CurrencyConverter();
11+
void addCurrency(const Currency& currency);
12+
void setExchangeRate(const string& fromCurrency, const string& toCurrency, double rate);
13+
double convert(double amount, const string& fromCurrency, const string& toCurrency) const;
14+
bool hasCurrency(const string& code) const;
15+
double getExchangeRate(const string& fromCurrency, const string& toCurrency) const;
16+
vector<Currency> listCurrencies() const;
17+
};
18+
#endif

Src/Currency_Converter/cur.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "cur.h"
2+
3+
Currency::Currency(const string& code, const string& name, const string& symbol)
4+
: code(code), name(name), symbol(symbol) {}
5+
6+
string Currency::getCode() const {
7+
return code;
8+
}
9+
10+
string Currency::getName() const {
11+
return name;
12+
}
13+
14+
string Currency::getSymbol() const {
15+
return symbol;
16+
}

Src/Currency_Converter/cur.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CURRENCY_H
2+
#define CURRENCY_H
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
class Currency {
6+
string code, name, symbol;
7+
public:
8+
Currency(const string& code, const string& name, const string& symbol);
9+
string getCode() const;
10+
string getName() const;
11+
string getSymbol() const;
12+
};
13+
#endif

Src/Currency_Converter/main.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <bits/stdc++.h>
2+
#include "cur.h"
3+
#include "conv.h"
4+
using namespace std;
5+
using nlim = numeric_limits<streamsize>;
6+
7+
void show(const CurrencyConverter& converter) {
8+
for (auto& currency : converter.listCurrencies())
9+
cout << currency.getCode() << "-" << currency.getName() << "(" << currency.getSymbol() << ")\n";
10+
cout << endl;
11+
}
12+
13+
int main() {
14+
CurrencyConverter converter;
15+
converter.addCurrency(Currency("USD", "US Dollar", "$"));
16+
converter.addCurrency(Currency("EUR", "Euro", ""));
17+
converter.addCurrency(Currency("INR", "Indian Rupee", ""));
18+
converter.addCurrency(Currency("GBP", "British Pound", "£"));
19+
converter.setExchangeRate("USD", "INR", 83.0);
20+
converter.setExchangeRate("INR", "USD", 0.012);
21+
converter.setExchangeRate("USD", "EUR", 0.93);
22+
converter.setExchangeRate("EUR", "USD", 1.08);
23+
converter.setExchangeRate("USD", "GBP", 0.79);
24+
converter.setExchangeRate("GBP", "USD", 1.27);
25+
converter.setExchangeRate("EUR", "INR", 89.0);
26+
converter.setExchangeRate("INR", "EUR", 0.011);
27+
int choice;
28+
do {
29+
cout << "\nMenu:\n1.List\n2.Convert\n3.Update\n0.Exit\n> ";
30+
cin >> choice;
31+
cin.ignore(nlim::max(),'\n');
32+
if (choice == 1) {
33+
show(converter);
34+
} else if (choice == 2) {
35+
string fromCurrency, toCurrency;
36+
double amount;
37+
cout << "From: ";
38+
cin >> fromCurrency;
39+
cout << "To: ";
40+
cin >> toCurrency;
41+
cout << "Amount: ";
42+
cin >> amount;
43+
if (cin.fail() || amount <= 0) {
44+
cout << "Invalid input\n";
45+
cin.clear();
46+
cin.ignore(nlim::max(),'\n');
47+
continue;
48+
}
49+
try {
50+
double result = converter.convert(amount, fromCurrency, toCurrency);
51+
cout << fixed << setprecision(2) << amount << fromCurrency << "=" << result << toCurrency << "\n";
52+
} catch (const exception& e) {
53+
cout << "Error: " << e.what() << "\n";
54+
}
55+
} else if (choice == 3) {
56+
string fromCurrency, toCurrency;
57+
double rate;
58+
cout << "From: ";
59+
cin >> fromCurrency;
60+
cout << "To: ";
61+
cin >> toCurrency;
62+
cout << "Rate(1" << fromCurrency << "=" << toCurrency << "): ";
63+
cin >> rate;
64+
if (cin.fail() || rate <= 0) {
65+
cout << "Invalid rate\n";
66+
cin.clear();
67+
cin.ignore(nlim::max(),'\n');
68+
continue;
69+
}
70+
try {
71+
converter.setExchangeRate(fromCurrency, toCurrency, rate);
72+
cout << "Updated\n";
73+
} catch (const exception& e) {
74+
cout << "Error: " << e.what() << "\n";
75+
}
76+
} else if (choice == 0) {
77+
cout << "bye\n";
78+
} else {
79+
cout << "bad\n";
80+
}
81+
} while (choice != 0);
82+
return 0;
83+
}

0 commit comments

Comments
 (0)