Java 实现币种汇率自动转换

1、 获取实时汇率
思路:从汇率网网页中利用正则表达式提取相应的汇率信息, 填充到当前的汇率表内,

// 从网站:http://www.usd-cny.com/中获取最新的汇率信息

  final static String webSite = "http://www.usd-cny.com/";

//利用hashmap对不同货币之间的利率进行存储
//key: f r o m + from+ from+to, value: $rate

// 获取网页内容

 public static void update() throws Exception {
        URL hp = new URL(webSite);
        URLConnection hpCon = hp.openConnection();
 
        System.out.println("== Content ==");
        InputStream input = (InputStream)hpCon.getInputStream();
        BufferedReader br = new BufferedReader(new
                                               InputStreamReader(input, "gb2312"));
 
        String str = null;
        while (( str = br.readLine() ) != null) {
            System.out.println(str);
        }
        input.close();
    }

//新建汇率类,提取表单内容

class RateInfo {
    String to;
    // [0]: 现汇买入价 [1]: 现钞买入价
    // [2]: 卖出价     [3]: 中间价 [4]: 基准价
    Double price[] = new Double[5];
}

//查看网页源码,发现汇率表由大写的TABLE包括起来, 每一行由TR包围, 每一项由TD包围。 因此, 选择用正则表达式匹配获取内容,代码为:

str.startwith(token), where token = "<TABLE" or "</TABLE" or "<TR" or "</TR"
str.startwith("<TD") && str.indexOf("href) != -1, to recognise the first col
str.startwith("<TD") && str.indexOf("href) == -1, to recognise the other cols

to extract the unit of the money
import java.util.regex.*;
String patt = "<TD.*<a href.*?>\\(.*?\\)</a>"
Pattern r = Pattern.compile(patt);
Matcher m = r.matcher(str);
m.group(1); // is the result, such as "美元 USD"

得到rates,继续编写:

String str = null;
        boolean inTable = false;
        int nRows = 0;
        String matchStr = null;
        while (( str = br.readLine() ) != null) {
            str = str.trim();

            // 判断是否在汇率表的范围
            if (str.startsWith("<TABLE")) {
                inTable = true;
                continue;
            }

            if (str.startsWith("</TABLE")) {
                break;
            }

            if (inTable == false)
                continue;

            if (str.startsWith("<TR")) {
                nRows += 1;
                // 忽略第一行的标题
                if (nRows == 1) continue;

                // 汇率表的读取只到港币
                if (nRows == RateInfo.NKINDS+2) break;

                // 获得第一列的完整代码
                str = br.readLine().trim();
                str = str + br.readLine().trim();

                // 获取币种缩写
                String patt = "<TD.*<a href.*?>(.*)</a>";
                Pattern r = Pattern.compile(patt);
                Matcher m = r.matcher(str);
                // matchStr = m.group(1);
                // 将汉字与缩写进行分离
                // matchStr = (matchStr.split())[1];
                if (m.find()) {
                    matchStr = m.group(1);
                    matchStr = (matchStr.split(" "))[1];
                    System.out.println(matchStr);
                } else {
                    System.out.println("No Match");
                }

                for (int i = 0; i < RateInfo.NELEM; i++) {
                    str = br.readLine();
                    String pattE = "<TD.*>(.*?)&nbsp;</div>";
                    r = Pattern.compile(pattE);
                    m = r.matcher(str);
                    if (m.find())
                        System.out.println(m.group(1));
                    else
                        System.out.println("No Match");
                }
            }

// 设置不同货币之间的利率
// 1 $from * $rate = 1 $to

 public static void setRate(String from, String to, double rate) {
        rateTable.put(from+to, new Double(rate));
    }

    public static Double getRate(String from, String to) {
        return 615.65;
        // return (Double) rateTable.get(from + to);
    }

//将一定量的货币 m , 转 变 成 单 位 为 m, 转变成单位为 m,to的货币量

    // return: 相应的货币值
    public static Money exchangeRate(Money m, String to) {
        if (m.unit.equals(to)) return new Money(m);
        Double rate = getRate(m.unit, to);

        if (rate == null) {
            throw new IllegalArgumentException();
        }

        return new Money(m.amount*rate.doubleValue(), to);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值