/*
	(c) 1998 Burton Rosenberg
*/

class ShoppingCart
{

	final static int MAX_STUFF = 10 ;

	// data structure invariants:
	
	// nStuff points to first available slot in array
	// for all 0<=i<nStuff, theStuff[i] is valid.
	
	// currentThing is the current item, or ==nStuff
	// if there is no current item.
	// 0<=currentThing<=nStuff.

	// nStuff >= MAX_STUFF if array is full.

	Item [] theStuff = new Item[MAX_STUFF] ;
	int []  quantity = new int[MAX_STUFF] ;
	int nStuff = 0 ;
	int currentThing = 0 ;

        void addItem( Item itemToAdd, int quant)
	throws ShoppingCartFullException
        {
		if ( nStuff>=MAX_STUFF )
		{
			throw new ShoppingCartFullException() ;
		}
		if ( itemToAdd==null ) return ;

		quantity[nStuff] = (quant>0) ? quant : 0 ;
		theStuff[nStuff++] = itemToAdd ;
        }

        int getTotalCost()
        {
        	int totalCost = 0 ;
		for ( int i=0; i<nStuff; i++ )
		{
			totalCost += quantity[i]*theStuff[i].getPrice() ;
		}
		return totalCost ;
        }

        Item getItem()
        {
		return (currentThing<nStuff)
			? theStuff[currentThing] : null ;
        }

        int getQuantity( )
        {
		return (currentThing<nStuff) 
			? quantity[currentThing] : 0 ;
        }

        boolean gotoNth(int nth)
        {
                if ( nth<0 ) return false ;
		if ( nth>=nStuff ) return false ;
		currentThing = nth ;
		return true ;
        }

        boolean gotoNext()
        {
		if ( currentThing<nStuff ) currentThing++ ;
		return ( currentThing<nStuff ) ;
        }

        void changeQuantity(int newQuantity)
        {
                if ( currentThing<nStuff ) 
		{
			quantity[currentThing] =
				(newQuantity>0)? newQuantity : 0 ;
		}
	}

}
