Aikihack

Curency converter 2(advanced)- Java

Jun 15th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. /*** Created by AiKiHack on 13-03-2017 02:21 in project dataTypes11032017 ***/
  5. public class p12ce_CurrencyConverter {
  6.     static Scanner consolein = new Scanner(System.in);
  7.     static Map<String, Double> rates = new HashMap<String, Double>();
  8.     public static void main(String[] args) {
  9.         rates.put("BGN", new Double(1.0));
  10.         rates.put("USD", new Double(1.79549));
  11.         rates.put("EUR", new Double(1.95583));
  12.         rates.put("GBP", new Double(2.53405));
  13.         double inputSum = Double.parseDouble(consolein.nextLine());
  14.         String currIn = consolein.nextLine();
  15.         String currOut = consolein.nextLine();
  16.         if (rates.get(currIn) == null){newCurrency(currIn);} // TODO check HashMap not exists(key)
  17.         if (rates.get(currOut) == null){newCurrency(currOut);} // TODO check HashMap not exists(key)
  18.         double result = inputSum * rates.get(currIn) / rates.get(currOut);
  19.         System.out.printf("%.2f %s", result, currOut);
  20.     }
  21.     public static void newCurrency(String currCode) {
  22.         System.out.printf("Currency rate for %s unavailable! \n To Add new rate type 'YES' \n", currCode);
  23.         String answer = consolein.nextLine();
  24.         double newRate;
  25.         if (answer.equals("YES")){
  26.             System.out.printf("Add rate for %s : \n", currCode);
  27.             newRate = Double.parseDouble(consolein.nextLine());
  28.             rates.put(currCode, new Double(newRate));
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment