|
| 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