Reputation: 1
This code takes a from and to argument, and i need to Exchange from USD to 20 other rates. Is there away i can give it a array of the Strings(convert to) instead of it needs to connect to the website 20 times, takes like 10-15 sec each time it loads the prices.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package priceStrategy;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
*
* @author
*/
public class ExchangeFinder {
/**
* The method takes 2 currency strings and return the exchange rate.
*
* @param fromCurrency String the currency to exchange from
* @param toCurrency String the currency to exchange to
* @return double the exchange rate between fromCurrency and toCurrency. If
* something goes wrong 100.0 will be returned.
*
* USD - DKK USD - JPY USD - GBP USD - AUD USD - EUR USD - ESP USD - GHS USD
* - ILS USD - KES USD - JOD USD - LKR USD - LVL USD - MAD USD - MWK USD -
* NOK USD - PHP USD - NOK USD - PKR USD - RUB USD - SGD
*/
public static double getExchangeRate(String fromCurrency, String toCurrency) {
double result = 100.0;
try {
// Open a connection to bloomberg to get exchange rates
URL bloombergCurrency = new URL("http://www.bloomberg.com/quote/" + fromCurrency + toCurrency + ":CUR");
URLConnection bc = bloombergCurrency.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(bc.getInputStream()));
String inputLine; //Used to read in lines from webpage
boolean found = false; //Flag set true if the exchange rate is found in all the lines
// 1) read in line and if it's not null and the default result has not been changed...
while ((inputLine = in.readLine()) != null && result == 100.0) {
if (found) { //..2) if found == true then we have got the correct exchange rate
result = Double.parseDouble(inputLine);
}
//..3) looking for the exchange rate in the lines. It's right after this string
if (inputLine.trim().equals("<span class=\" price\">")) {
found = true;
}
}
in.close(); //DONE. Closing connection.
if (!found) {
System.out.println("Error: Never found the currency you asked for!");
} //Message if currency not found
} catch (MalformedURLException ex) {
System.out.println("MalformedURLException in getExchangeRate(): Invalid URL.");
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException in getExchangeRate(): Invalid response from server.");
} catch (IOException ex) {
System.out.println("IOException in getExchangeRate(): Cannot connect to server.");
}
return result;
}
}
Upvotes: 0
Views: 1574
Reputation: 12752
I would definitely avoid calling someone else's website repeatedly for every single conversion you need to do. Not only does that generate unwanted traffic for them, it might actually be against their use-policy (I haven't checked in your specific case) and they might randomly block your IP from accessing their service, which will break your application.
The best approach (if you're allowed to use their service this way) would be to build your own "database" (big word for 20 numbers) of exchange rates by just calling bloomberg once per target currency to get the exchange rate and remember it. Then, just do your needed conversions yourself using the data from the database. The only thing that you need to decide then is how often to update your database by calling bloomberg again for each currency (once a day? once a week?). But I would be careful not to overdo it... :)
This approach is not only much faster to run once you have the database built, but it also keeps working if (your link to) bloomberg goes down or they block you or change their website interface, in which case your application will just use the last known exchange rates. You could even fail over to another "provider" and get your exchange rates there instead.
A side comment: I would not return 100.0 if something goes wrong, as that may actually be a valid result that gets returned for one of your conversions if everything went right. Use a negative number or 0 or some other impossible exchange rate.
Upvotes: 1