package ex6; import java.net.*; import java.io.*; import java.util.*; /** * QuoteServer is a utility class that allows calling code to * retrieve the latest market value of a given stock by ticker symbol. * The QuoteServer obtains the stock value by using the website: * http://quote.fool.com
* Examples of ticker symbols are MSFT and orcl. * (Note that ticker symbols are not case sensitive. That is that "MSFT" * and "msft" are functionally equivalent.)

* * A valid ticker symbol is one that is currently registered with either * the New York Stock Exchange (NYSE) or NASDAQ. */ public class QuoteServer { protected static final String _URL = "http://quote.fool.com/simple.asp?symbols="; protected static final String _TOKEN1 = "mw_simple_last"; protected static final String _TOKEN2 = "NOBR"; protected static final String _DELIMITER = "&<>="; public static void main(String[] args) { try { String str = getLastValue(args[0]); System.out.println(args[0] + " has a stock value of " + str); } catch(Exception e) { System.err.println(e); } } /** * * retrieve the latest market value of a stock * * @requires: tickerSymbol != null * @effects: returns a current * value for tickerSymbol as a dollar amount, with a * period separating dollars and cents (eg, "120.50" for * one hundred and twenty dollars and fifty cents)
* unless tickerSymbol is not a valid NYSE or NASDAQ * symbol, when throws NoSuchTickerException
or * unless an error connecting to the website or some * other error occurs, when throws WebsiteDataException
* The amount returned may contain commas, for example, * "2,243.87"
*/ public static String getLastValue(String tickerSymbol) throws WebsiteDataException, NoSuchTickerException { // the web page queried by the code below can be broken into tokens // with delimiters being <, >, and =. The token corresponding to // the current stock price comes after the two tokens // mw_simple_last and NOBR. String strURLStart = _URL; URL urlWebPage = null; InputStreamReader isr = null; BufferedReader brWebPage = null; // open the web page for reading try { urlWebPage = new URL(strURLStart + tickerSymbol); isr = new InputStreamReader(urlWebPage.openStream()); brWebPage = new BufferedReader(isr); } catch(Exception e) { throw new WebsiteDataException(); } // find the line with the stock quote on it String strLine = null; try { while(true) { strLine = brWebPage.readLine(); if(strLine == null) { throw new WebsiteDataException("Parse failed!"); } if(strLine.indexOf(_TOKEN1) != -1) break; } } catch(IOException e) { throw new WebsiteDataException(); } // find the stock quote in the line StringTokenizer strtok = new StringTokenizer(strLine, _DELIMITER); while(true) { if(strtok.hasMoreTokens() == false) throw new NoSuchTickerException(); if(strtok.nextToken().compareTo(_TOKEN1) == 0) break; } String strNOBR = strtok.nextToken(); if(!strNOBR.equals(_TOKEN2)) throw new NoSuchTickerException(); String strStockValue = strtok.nextToken(); //format check removed // close the web page stream try { brWebPage.close(); isr.close(); } catch(IOException e) { throw new WebsiteDataException(); } return strStockValue; } }