1234567891011121314151617181920212223242526272829303132333435
  1. import java.util.HashMap;
  2. public class CurrencyConverter {
  3. HashMap<String, Double> rates = new HashMap<>();
  4. public CurrencyConverter() {
  5. rates.put("USD", 1.00);
  6. rates.put("EUR", 0.94);
  7. rates.put("GBP", 0.82);
  8. rates.put("INR", 68.32);
  9. rates.put("AUS", 1.35);
  10. rates.put("CAN", 1.32);
  11. rates.put("SGD", 1.43);
  12. rates.put("CHF", 1.01);
  13. rates.put("MYR", 1.01);
  14. rates.put("YEN", 1.01);
  15. rates.put("CNY", 6.92);
  16. }
  17. public double convertTo(String base, String convert, double value) {
  18. double baseRate = rates.get(base);
  19. double convertRate = rates.get(convert);
  20. double usdRate = rates.get("USD");
  21. double result = 0;
  22. if (baseRate <= usdRate) {
  23. double usd = value / baseRate;
  24. result = usd * convertRate;
  25. } else {
  26. double usd = value * baseRate;
  27. result = usd * convertRate;
  28. }
  29. return result;
  30. }
  31. }