package org.jasons.util;

import java.io.*;
import java.net.*;
import java.util.*;

/**
	org.jasons.util.Quote - Fetches stock quotes from the Yahoo!
	Finance site.  Thanks Jerry and Dave for giving us the nifty
	CSV download option that makes this bean possible.

	Not a ton of error checking done here.  This code assumes you have
	Internet connectivity, and that Yahoo! Finance is up and running.
	If either of those are not true, something else goes wrong, you
	accidently blow up your toaster or accidently step on your cat, 
	it's not my fault.

	Copyright 2001 - Jason Costomiris <jcostom@jasons.org>

	Published under the Artistic License.  See:

	http://www.opensource.org/licenses/artistic-license.html
*/

public class Quote extends java.lang.Object implements Serializable {

	/**
		Quote.Quote - Beans require a null constructor method
	*/
	public Quote (){
	}

	// Setup the URL, less the symbol
	private static final String urlFrontEnd = 
				"http://finance.yahoo.com/d/quotes.csv?s=";
	private static final String urlBackEnd  = 
				"&f=l1d1t1c1ohgv";

	// Setup the raw data retrieval
	private String quoteResult = null;

	// the returnable information...
	public String symbol;
	public String last;
	public String date;
	public String time;
	public String change;
	public String open;
	public String high;
	public String low;
	public String volume;

	/**
		Quote.setSymbol - public method used to setup symbol
		This method is completely superfluous.  It's required,
		however, because this is a bean, where one must have
		a buttload of get*() and set*() methods.  If you re-use this
		in a non-bean context, just integrate this into the 
		fetchQuote() method.
	*/
	public void setSymbol(String symbol){
		this.symbol = symbol.toUpperCase();
	}

	/**
		Quote.fetchQuote - public method used to retrieve and parse data
	*/
	public void fetchQuote(){
		// Construct a url and retrieve the data...
		try {
			URL url = new URL(urlFrontEnd + symbol + urlBackEnd);
			BufferedReader reader = 
				new BufferedReader(new InputStreamReader(url.openStream()));
			quoteResult = reader.readLine();
		} catch (Exception e) {
			// implement some sort of error handle here...
		}
		
		// parse return values
		StringTokenizer st = new StringTokenizer(quoteResult, ",");
		this.last = st.nextToken();
		this.date = trimQuoteMarks(st.nextToken());
		this.time = trimQuoteMarks(st.nextToken());
		this.change = st.nextToken();
		this.open = st.nextToken();
		this.high = st.nextToken();
		this.low = st.nextToken();
		this.volume = st.nextToken();
	}

	// private method used to chop off first and last
	// character of a string - i.e. the quotation
	// marks in the CSV text...
	private String trimQuoteMarks(String data){
		int len = data.length();
		return data.substring(1,len - 1);
	}

	// accessor functions
	public String getSymbol(){
		return this.symbol;
	}

	public String getLast(){
		return this.last;
	}

	public String getDate(){
		return this.date;
	}

	public String getTime(){
		return this.time;
	}

	public String getChange(){
		return this.change;
	}
	
	public String getOpen(){
		return this.open;
	}

	public String getHigh(){
		return this.high;
	}
	
	public String getLow(){
		return this.low;
	}

	public String getVolume(){
		return this.volume;
	}
}
