//************************************                                         
//                                                                             
// Alex Apostol
//
// Java class for vectors, eventually will sort.
// 
// 
//  
//************************************   

import java.applet.*;// used for applets
import java.awt.*;// if you use any kind of graphics  
import java.io.*;// if you use any kind of I/O
import java.awt.event.*;

public class cookieVector
{

    Cookie v[];
    Cookie c; //error trapping cookie, used when I have to return a cookie.
    int n;
    final int MAX=10;

    //***************************************
    //     CONSTRUCTORS
    //***************************************
    /**
     *Constructs an empty cookieVector of size 10.
     */
    cookieVector()
    {
	v=new Cookie [MAX];
	c= new Cookie(0,0,0); //The error trapping cookie.
    }

    //***************************************
    //     ELEMENT
    //returns the cookie at the index i. 
    //***************************************  
    /**
     *  Returns the cookie at array[i].
     *@param i index of cookie.
     */
    public Cookie element (int i)
    {
	if(i>=0 && i<n)
	    return v[i];
	else
	    {
		System.out.println("index out of bounds at call to myVector.element.  i="+i);
		return c;
	    }

    }

    //***************************************
    //     ADDELEMENT
    //***************************************
    /**
     * Adds a cookie to the end of the array.
     */
    public void addElement(Cookie e)
    {
	if(n<MAX)
	    {
	    v[n++]=e;
	    }
	else
	    System.out.println("Array out of bounds.");
    }


    //***************************************
    //     DELETE
    //***************************************
    /**
     * Deletes the last cookie from the vector.
     */
    public void delete()
    {
	if(!(isEmpty()))
	    {
		n--;
	    }
    }

    //***************************************
    //     CLEAR 
    //***************************************
    /*
     * Empties the array, resets all counters.
     */
    public void clear()
    {
	n=0;
    }

    //***************************************  
    //     Sort 
    //***************************************   
    /**
     * Sorts the cookies by size.
     */
    public void sort()
    {
	int i=0;
	int minPos=0;

	System.out.println("Sort Called.");

	for(i=0; i<n; i++)
	    {
		minPos=findMinPos(i);
		Swap(i, minPos);
	    }
    }

    //*****************************************************
    //   findMinPos
    //*****************************************************
    /**
     * Finds the cookies of smallest size.
     * @param i index of first non sorted cookie.
     * @return index of position of lowest flavor cookie.
     */
    public int findMinPos(int i)
    {
	int j=i;
	int pos=i;

	for(j=i;j<n;j++)
	    {
		if (v[j].getSize() < v[pos].getSize()) 
		    pos=j;
	    }

	return pos;
    }

    //*****************************************************
    // SWAP
    //*****************************************************
    /**
     * Swaps 2 cookies.
     * @param i swap this cookie
     *        min with this cookie.
     */
    public void Swap(int i, int min)
    {
	Cookie temp=v[i];
	v[i]=v[min];
	v[min]=temp;
    }

    //**************************************
    //     size 
    //***************************************
    /**
     * @return cookie vector size.
     */   
    public int size()
    {
	return n;
    }

    //**************************************
    //     incElement
    //***************************************
    /**
     * Increases an element
     * @param i index of element to increment.
     */   
    public void incElement(int i)
    {
	if(i<n)
	    v[i].increaseSize();
    }

    //**************************************
    //     changeFlavor
    //***************************************
    /**
     * Changes the flavor
     * @param i index of element to change the flavor of.
     */   
    public void changeFlavor(int i)
    {
	if(i<n)
	    {
		int f=v[i].getFlavor();
		f++;
		v[i].setFlavor(f);
	    }
    }


    //***************************************  
    //     isEmpty
    //***************************************   
    /**
     * @return true is array is empty. false if there are cookies.
     */
    public boolean isEmpty()
    {
	if (n==0)
	    return true;
	else 
	    return false;
    }

    //***************************************  
    //     PRINT 
    //*************************************** 
    /**
     * Prints the array of cookies and their information.
     */
    public void print()
    {
	int i;
	System.out.println("----------------------------------");
        for(i=0;i<n;i++)
	    {
		System.out.print("Cookie "+ i + "=");
		element(i).print();
		System.out.println("");
	    }
    }

    //***************************************       
    //     DRAW    
    //*************************************** 
    /**
     * Draws the array to the canvas.
     */
    public void draw (Graphics g)
    {
 	int y=100; 
	for(int i=0; i<n; i++) 
	    { 
		element(i).draw(g, 40, y); 
		y+= element(i).getHeight() + 3; 

	    }//end of forloop                 
    } 
    
}
