import java.util.*;


/**
 * An abstract SecurityQuotation (extended by StockQuotation, 
 * FutureQuotation, etc...).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 3.0
 */
public abstract class SecurityQuotation
{
    protected GregorianCalendar   date;
    protected double              open, high, low, close;
    protected int                 volume;

//[constructor
    /**
     * Default Constructor.
     *
     * To be used only by other constructors in this class and in
     * derived classes.
     */
    protected SecurityQuotation()
    {
        super();
    }
//]constructor

    /**
     * Explicit Value Constructor.
     *
     * @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;
    }

//[fromString
    /**
     * Set the attributes of this SecurityQuotation based on
     * a String representation.
     *
     * @param s   The String representation
     * @return    The StringTokenizer in case it is needed by a derived class
     */
    protected StringTokenizer fromString(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);
	
        this.date = new GregorianCalendar(yy,mm,dd);
	
        token = tokenizer.nextToken();
        this.open = Double.parseDouble(token);
	
        token = tokenizer.nextToken();
        this.high = Double.parseDouble(token);
	
        token = tokenizer.nextToken();
        this.low = Double.parseDouble(token);
	
        token = tokenizer.nextToken();
        this.close = Double.parseDouble(token);
	
        token = tokenizer.nextToken();
        this.volume = Integer.parseInt(token);

        return tokenizer;        
    }
//]fromString
    
    /**
     * 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;
    }

    /**
     * Return the ticker symbol.
     */
    public abstract String getSymbol();
        

    /**
     * Returns the volume.
     *
     * @return    The volume
     */
    public int getVolume()
    {
       return volume;
    }

    /**
     * Return a String representation.
     *
     * @return   The String representation
     */
    public String toString()
    {
        return getSymbol() + "\t" +
            (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;
    }
}
