import java.text.*;
import java.util.*;


/**
 * An abstract SecurityQuotation (extended by StockQuotation, 
 * FutureQuotation, etc...)
 *
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 *
 */
public abstract class SecurityQuotation
{
    protected GregorianCalendar   date;
    protected double              open, high, low, close;
    protected int                 volume;
    

    /**
     * Default constructor
     */
    public SecurityQuotation()
    {
	date = null;
    }





    /**
     * Common construction tasks for all derived classes
     *
     * @param date      The date of the entry
     * @param open      The opening price
     * @param high      The high price
     * @param low       The low price
     * @param close     The closing price
     * @param volume    The volume traded
     */
    public SecurityQuotation(GregorianCalendar date, 
			     double open,double high,double low,double close,
                             int volume)
    {
	this.date = date;
	this.open = open;
	this.high = high;
	this.low = low;
	this.close = close;
	this.volume = volume;
    }



    
    /**
     * Return the closing price
     *
     * @return    The closing price
     */
    public double getClose()
    {
        return close;
    }



    /**
     * Return the date
     *
     * @return    The date of this quotation
     */
    public GregorianCalendar getDate()
    {
        return date;
    }



    /**
     * Return the high price
     *
     * @return    The high price
     */
    public double getHigh()
    {
        return high;
    }



    /**
     * Return the low price
     *
     * @return    The low price
     */
    public double getLow()
    {
        return low;
    }



    /**
     * Return the opening price
     *
     * @return    The opening price
     */
    public double getOpen()
    {
        return open;
    }



    /**
     * Returns the symbol for this security
     * (This method must be implemented by derived classes)
     *
     * @return    The symbol
     */
    public abstract TickerSymbol getSymbol();



    /**
     * Returns the volume
     *
     * @return    The volume
     */
    public int getVolumes()
    {
        return volume;
    }



    /**
     * Parse a String representation of a SecurityQuotation into 
     * its components.  This is only useful for derived classes.
     *
     * @param s    The String representation
     * @return     The StringTokenizer used (in case more parsing is needed)
     */
    protected StringTokenizer parseSecurityQuotation(String s) 
                              throws NoSuchElementException,
                                     NumberFormatException
    {

        int              dd, mm, yy;
        String           dds, mms, token, yys;
	StringTokenizer  tokenizer;


	tokenizer = new StringTokenizer(s,",");

	token = tokenizer.nextToken();
	    
	yys = token.substring(0,2);
	yy  = Integer.parseInt(yys);
	
	mms = token.substring(2,4);
	mm  = Integer.parseInt(mms) - 1; // Note that months start with 0
	
	dds = token.substring(4);
	dd  = Integer.parseInt(dds);
	
	date = new GregorianCalendar(yy,mm,dd);
	
	token = tokenizer.nextToken();
	open = Double.parseDouble(token);
	
	token = tokenizer.nextToken();
	high = Double.parseDouble(token);
	
	token = tokenizer.nextToken();
	low = Double.parseDouble(token);
	
	token = tokenizer.nextToken();
	close = Double.parseDouble(token);
	
	token = tokenizer.nextToken();
	volume = Integer.parseInt(token);

	return tokenizer;
    }



    /**
     * Return a String representation of this SecurityQuotation
     *
     * @return   The String representation
     */
    public String toString()
    {
	return ((date.get(Calendar.MONTH)+1)+"/"+
                date.get(Calendar.DAY_OF_MONTH)+"/"+
                date.get(Calendar.YEAR)+
                "\tO: " + open + "\tH: " + high +
		"\tL: " + low + "\tC: " + close + 
                "\tV: " + volume);
    }
}
