Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void loadCurrencyRates() {
- // Convert String Array to List to use .contains
- List<String> currencyNamesList = Arrays.asList(currencyNames);
- List<String> symbolsList = Arrays.asList(symbols);
- String currentLine;
- BufferedReader in = null;
- try {
- in = new BufferedReader(new InputStreamReader(new FileInputStream("./" + fileName), "UTF8"));
- while ((currentLine = in.readLine()) != null) {
- //currentLine = currentLine.replace("\uFEFF", "");
- String[] arrOfStr = currentLine.split(", ");//Splits file content by , and sends them to arrays.
- System.out.println(arrOfStr[0]);//Array for Currency names
- System.out.println(arrOfStr[1]);//Array for Currency rates
- System.out.println(arrOfStr[2]);//Array for Currency symbol
- if (currencyNamesList.contains(arrOfStr[0]) && symbolsList.contains(arrOfStr[2])) {
- try {
- double newRate = Double.parseDouble(arrOfStr[1]);//Changes Currency Rates to that in array index [1]
- System.out.println("valid name and symbol");
- int indexOfName = currencyNamesList.indexOf(arrOfStr[0]);
- if (symbols[indexOfName].equals(arrOfStr[2])) { //Checks if currency name and symbol match
- System.out.println("currency name and symbol match");
- switch(indexOfName) {
- case 0: eurRate = newRate;
- break;
- case 1: usdRate = newRate;
- break;
- case 2: audRate = newRate;
- break;
- case 3: cadRate = newRate;
- break;
- case 4: iskRate = newRate;
- break;
- case 5: aedRate = newRate;
- break;
- case 6: zarRate = newRate;
- break;
- case 7: thbRate = newRate;
- break;
- }
- System.out.println(arrOfStr[0] + " rate changed to: " + arrOfStr[1]);//Prints original currency rate and new currency rate
- JOptionPane.showMessageDialog(new JFrame(), "Currency rates loaded from: " + fileName, "Loaded Currency",
- JOptionPane.INFORMATION_MESSAGE);
- }
- } catch(Exception e) {
- System.out.println("Invalid Line cant parse");//Error Handling
- }
- }
- else {
- System.out.println("Invalid Line no match");//Error Handling
- }
- }
- }
- catch (Exception e) {
- System.out.println("");//Error Handling
- } finally {
- try {
- if (in != null)
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement