gpt4 book ai didi

java - 获取从 USD 到其他 20 种汇率的货币汇率。

转载 作者:行者123 更新时间:2023-12-01 12:59:06 30 4
gpt4 key购买 nike

此代码需要一个 from 和 to 参数,我需要将美元兑换成 20 个其他汇率。我可以给它一个字符串数组(转换为),而不需要连接到网站 20 次,每次加载价格大约需要 10-15 秒。

/*
* 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;
}

}

最佳答案

我绝对会避免为您需要进行的每一次转化重复调用其他人的网站。这不仅会为他们产生不需要的流量,实际上可能违反他们的使用策略(我没有检查你的具体情况),而且他们可能会随机阻止你的 IP 访问他们的服务,这会破坏你的应用程序。

最好的方法(如果您被允许以这种方式使用他们的服务)是建立您自己的汇率“数据库”(20 个数字的大字),只需为每种目标货币调用一次彭博社即可获取兑换信息评价并记住它。然后,只需使用数据库中的数据自行进行所需的转换即可。然后,您唯一需要决定的是通过针对每种货币再次调用 Bloomberg 来更新数据库的频率(每天一次?每周一次?)。但我会小心不要做得太过分......:)

一旦你建立了数据库,这种方法不仅运行速度更快,而且如果(你的)Bloomberg 出现故障或者他们阻止你或改变他们的网站界面,它也能继续工作,在这种情况下你的应用程序只会使用最后已知的汇率。您甚至可以故障转移到另一个“提供商”并在那里获取汇率。

附注:如果出现问题,我不会返回 100.0,因为如果一切顺利,这实际上可能是为您的一次转换返回的有效结果。使用负数或 0 或其他不可能的汇率。

关于java - 获取从 USD 到其他 20 种汇率的货币汇率。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23660924/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com