Advertisement
alfiesd

ap

Dec 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. public void loadCurrencyRates() {
  2.             // Convert String Array to List to use .contains
  3.             List<String> currencyNamesList = Arrays.asList(currencyNames);
  4.             List<String> symbolsList = Arrays.asList(symbols);
  5.  
  6.             String currentLine;
  7.             BufferedReader in = null;
  8.             try {
  9.                 in = new BufferedReader(new InputStreamReader(new FileInputStream("./" + fileName), "UTF8"));
  10.                 while ((currentLine = in.readLine()) != null) {
  11.                     //currentLine = currentLine.replace("\uFEFF", "");
  12.                     String[] arrOfStr = currentLine.split(", ");//Splits file content by , and sends them to arrays.
  13.                     System.out.println(arrOfStr[0]);//Array for Currency names
  14.                     System.out.println(arrOfStr[1]);//Array for Currency rates
  15.                     System.out.println(arrOfStr[2]);//Array for Currency symbol
  16.                     if (currencyNamesList.contains(arrOfStr[0]) && symbolsList.contains(arrOfStr[2])) {
  17.                         try {
  18.                             double newRate = Double.parseDouble(arrOfStr[1]);//Changes Currency Rates to that in array index [1]
  19.                             System.out.println("valid name and symbol");
  20.                             int indexOfName = currencyNamesList.indexOf(arrOfStr[0]);
  21.                             if (symbols[indexOfName].equals(arrOfStr[2])) { //Checks if currency name and symbol match
  22.                                 System.out.println("currency name and symbol match");
  23.  
  24.                                 switch(indexOfName) {
  25.                                     case 0: eurRate = newRate;
  26.                                     break;
  27.                                     case 1: usdRate = newRate;
  28.                                     break;
  29.                                     case 2: audRate = newRate;
  30.                                     break;
  31.                                     case 3: cadRate = newRate;
  32.                                     break;
  33.                                     case 4: iskRate = newRate;
  34.                                     break;
  35.                                     case 5: aedRate = newRate;
  36.                                     break;
  37.                                     case 6: zarRate = newRate;
  38.                                     break;
  39.                                     case 7: thbRate = newRate;
  40.                                     break;
  41.  
  42.                                 }
  43.  
  44.                                 System.out.println(arrOfStr[0] + " rate changed to: " + arrOfStr[1]);//Prints original currency rate and new currency rate
  45.                                 JOptionPane.showMessageDialog(new JFrame(), "Currency rates loaded from: " + fileName, "Loaded Currency",
  46.                         JOptionPane.INFORMATION_MESSAGE);
  47.                             }
  48.                         } catch(Exception e) {
  49.                             System.out.println("Invalid Line cant parse");//Error Handling
  50.                         }
  51.                     }
  52.                     else {
  53.                         System.out.println("Invalid Line no match");//Error Handling
  54.                     }
  55.                 }
  56.             }
  57.             catch (Exception e) {
  58.                 System.out.println("");//Error Handling
  59.                
  60.             } finally {
  61.                 try {
  62.                 if (in != null)
  63.                 in.close();
  64.             } catch (Exception e) {
  65.                 e.printStackTrace();
  66.             }
  67.             }
  68.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement