Advertisement
illpastethat

Stock class cats_

Jan 15th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5.  
  6. public class Stock {
  7.     private String xml;
  8.     private String ticker;
  9.  
  10.     public Stock(String ticker) throws IOException {
  11.         this.ticker = ticker;
  12.         URL url = new URL("http://www.google.com/ig/api?stock=" + ticker);
  13.         BufferedReader in = new BufferedReader(new InputStreamReader(
  14.                 url.openStream()));
  15.         this.xml = in.readLine();
  16.         return;
  17.     }
  18.  
  19.     public String toString() {
  20.         return this.ticker + " (" + this.getCompany() + ") Last: "
  21.                 + this.getPrice() + " (" + this.getTradeTimestamp()
  22.                 + ") Change: " + this.getChange() + " ("
  23.                 + this.getPercentChange() + "%)\n";
  24.     }
  25.  
  26.     public static String getDataType(String xml, String DATA_TYPE) {
  27.         // DATA_TYPE options
  28.         // {pretty_symbol,company,exchange,last,high,low,volume,avg_volume,market_cap,open,...
  29.         // y_close,change,perc_change,trade_timestamp,symbol_url,chart_url,isld_last,isld_trade_date_utc,isld_trade_time_utc}
  30.  
  31.         int r = xml.indexOf("<" + DATA_TYPE);
  32.         int x = xml.indexOf('"', r);
  33.         int y = xml.indexOf('"', x + 1);
  34.         return xml.substring(x + 1, y);
  35.     }
  36.  
  37.     public String getCompany() {
  38.         return getDataType(this.xml, "company");
  39.     }
  40.  
  41.     public double getPrice() {
  42.         return Double.parseDouble(getDataType(this.xml, "last"));
  43.     }
  44.  
  45.     public String getTradeTimestamp() {
  46.         return getDataType(this.xml, "trade_timestamp");
  47.     }
  48.  
  49.     public String getChange() {
  50.         return getDataType(this.xml, "change");
  51.     }
  52.  
  53.     public double getPercentChange() {
  54.         return Double.parseDouble(getDataType(this.xml, "perc_change"));
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement