Advertisement
Guest User

Good good

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