Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- public class Stock {
- private String xml;
- private String ticker;
- public Stock(String ticker) throws IOException {
- this.ticker = ticker;
- URL url = new URL("http://www.google.com/ig/api?stock=" + ticker);
- BufferedReader in = new BufferedReader(new InputStreamReader(
- url.openStream()));
- this.xml = in.readLine();
- return;
- }
- public String toString() {
- return this.ticker + " (" + this.getCompany() + ") Last: "
- + this.getPrice() + " (" + this.getTradeTimestamp()
- + ") Change: " + this.getChange() + " ("
- + this.getPercentChange() + "%)\n";
- }
- public static String getDataType(String xml, String DATA_TYPE) {
- // DATA_TYPE options
- // {pretty_symbol,company,exchange,last,high,low,volume,avg_volume,market_cap,open,...
- // y_close,change,perc_change,trade_timestamp,symbol_url,chart_url,isld_last,isld_trade_date_utc,isld_trade_time_utc}
- int r = xml.indexOf("<" + DATA_TYPE);
- int x = xml.indexOf('"', r);
- int y = xml.indexOf('"', x + 1);
- return xml.substring(x + 1, y);
- }
- public String getCompany() {
- return getDataType(this.xml, "company");
- }
- public double getPrice() {
- return Double.parseDouble(getDataType(this.xml, "last"));
- }
- public String getTradeTimestamp() {
- return getDataType(this.xml, "trade_timestamp");
- }
- public String getChange() {
- return getDataType(this.xml, "change");
- }
- public double getPercentChange() {
- return Double.parseDouble(getDataType(this.xml, "perc_change"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement