📄 currencyconverter.java
字号:
for (int y = 0; y < symbols.length; y++) { if (x != y) { String s = (String)symbols[x] + (String)symbols[y]; Double old = (Double)map.get(s); Double quote = downloadQuote(s); if (quote != null) map.put(s, quote); else map.remove(s); if (old == null && quote != null || old != null && !old.equals(quote)) setChanged(); if (monitor != null && monitor.isCanceled()) return Status.CANCEL_STATUS; } } if (monitor != null) monitor.worked(1); } notifyObservers(); save(); if (monitor != null) monitor.worked(1); if (monitor != null) monitor.done(); return Status.OK_STATUS; } public void setExchangeRatio(Currency from, Currency to, double ratio) { if (from != null && to != null && !from.equals(to)) setExchangeRatio(from.getCurrencyCode(), to.getCurrencyCode(), ratio); } public void setExchangeRatio(String from, String to, double ratio) { Double old = (Double)map.get(from + to); Double quote = new Double(ratio); map.put(from + to, quote); if (old == null && quote != null || old != null && !old.equals(quote)) { setChanged(); notifyObservers(); } } public Double getExchangeRatio(String from, String to) { Double ratio = (Double)map.get(from + to); if (ratio == null) { Double r = (Double)map.get(to + from); if (r != null) ratio = new Double(1 / r.doubleValue()); } return ratio; } public void setExchangeRatio(Date date, String from, String to, double ratio) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); date = calendar.getTime(); List list = (ArrayList)historyMap.get(from + to); if (list == null) list = new ArrayList(); History history = new History(date, new Double(ratio)); int index = list.indexOf(history); if (index != -1) { History old = (History)list.get(index); if (!old.ratio.equals(history.ratio)) { list.set(index, history); setChanged(); } } else { list.add(history); setChanged(); } historyMap.put(from + to, list); notifyObservers(); } public Double getExchangeRatio(Date date, String from, String to) { Double ratio = null; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); date = calendar.getTime(); List list = (ArrayList)historyMap.get(from + to); if (list != null) { for (int i = 0; i < list.size(); i++) { History history = (History)list.get(i); if (history.date.equals(date)) { ratio = history.ratio; break; } } } return ratio; } private void save() { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.getDOMImplementation().createDocument(null, "data", null); Element root = document.getDocumentElement(); for (Iterator iter = currencies.iterator(); iter.hasNext(); ) { Element node = document.createElement("currency"); node.appendChild(document.createTextNode((String)iter.next())); root.appendChild(node); } for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { String symbol = (String)iter.next(); Element node = document.createElement("conversion"); node.setAttribute("symbol", symbol); node.setAttribute("ratio", String.valueOf((Double)map.get(symbol))); saveHistory(node, symbol); root.appendChild(node); } TransformerFactory factory = TransformerFactory.newInstance(); try { factory.setAttribute("indent-number", new Integer(4)); } catch(Exception e) {} Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http\u003a//xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(document); File file = new File(Platform.getLocation().toFile(), "currencies.xml"); BufferedWriter out = new BufferedWriter(new FileWriter(file)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.flush(); out.close(); } catch (Exception e) { logger.error(e, e); } } private void saveHistory(Node root, String symbol) { Document document = root.getOwnerDocument(); List list = (List)historyMap.get(symbol); if (list != null) { java.util.Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { History d1 = (History) o1; History d2 = (History) o2; if (d1.date.after(d2.date) == true) return 1; else if (d1.date.before(d2.date) == true) return -1; return 0; } }); for (Iterator iter = list.iterator(); iter.hasNext(); ) { History history = (History)iter.next(); Element node = document.createElement("history"); node.setAttribute("date", dateFormat.format(history.date)); node.setAttribute("ratio", String.valueOf(history.ratio)); root.appendChild(node); } } } private Double downloadQuote(String symbol) { Double result = null; StringBuffer url = new StringBuffer("http://quote.yahoo.com/download/javasoft.beans?symbols="); url = url.append(symbol + "=X"); url.append("&format=sl1d1t1c1ohgvbap"); String line = ""; try { HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); IPreferenceStore store = CorePlugin.getDefault().getPreferenceStore(); if (store.getBoolean(CorePlugin.PREFS_ENABLE_HTTP_PROXY)) { client.getHostConfiguration().setProxy(store.getString(CorePlugin.PREFS_PROXY_HOST_ADDRESS), store.getInt(CorePlugin.PREFS_PROXY_PORT_ADDRESS)); if (store.getBoolean(CorePlugin.PREFS_ENABLE_PROXY_AUTHENTICATION)) client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(store.getString(CorePlugin.PREFS_PROXY_USER), store.getString(CorePlugin.PREFS_PROXY_PASSWORD))); } HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((line = in.readLine()) != null) { String[] item = line.split(","); if (line.indexOf(";") != -1) item = line.split(";"); // 1 = Last price or N/A if (item[1].equalsIgnoreCase("N/A") == false) result = new Double(numberFormat.parse(item[1]).doubleValue()); // 2 = Date // 3 = Time // 4 = Change // 5 = Open // 6 = Maximum // 7 = Minimum // 8 = Volume // 9 = Bid Price // 10 = Ask Price // 11 = Close Price // 0 = Code } in.close(); } catch (Exception e) { logger.error(e, e); } return result; } public Double downloadQuote(String symbol, Date date) { return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -