/*
	(c) 1998 Burton Rosenberg. All rights reserved.
*/

class ShoppingCartTest
{
	public static void main(String [] args)
	{
		ShoppingCart sc = new ShoppingCart() ;
		String [] topLevelOps = { "List ShoppingCart", "Add Item", "Total Price" } ;
		int rsvp ;

		System.out.println() ;
		System.out.println("+----------------------+") ;
		System.out.println("|  Welcome, shoppers!  |") ;
		System.out.println("+----------------------+") ;

		do {
			rsvp = menuChooser( "\nOperation?", topLevelOps ) ;
			switch ( rsvp )
			{
			case 0:
				listShoppingCart(sc) ;
				break ;
			case 1:
				addItemIntoCart(sc) ;
				break ;
			case 2:
				printTotalPrice(sc) ;
				break ;
			default:
			}
		}
		while ( rsvp>= 0 ) ;
		System.out.println() ;
		System.out.println("+-------------------------------+") ;
		System.out.println("| Thanks for shopping Globalis! |") ;
		System.out.println("+-------------------------------+") ;

	}

	private static void listShoppingCart( ShoppingCart sc )
	{
		if ( !sc.gotoNth(0) ) 
		{
			System.out.println("\nThe Shopping cart is empty") ;	
			return ;
		}
		int li = 1 ;
		do {
			System.out.println("\n=======\nItem "+(li++)) ;
			(sc.getItem()).printInfo() ;
			System.out.println( "Quantity: " + sc.getQuantity() ) ;
		}
		while ( sc.gotoNext() ) ;
		System.out.println("\n=======") ;
	}

	private static void addItemIntoCart( ShoppingCart sc )
	{
		String [] itemNameArray = { 
			(new PizzaItem()).getDescription(),
			(new ShoeItem()).getDescription() } ;
		int rsvp ;
		Item newItem = null ;

		rsvp = menuChooser("\nItem:", itemNameArray) ;
		if ( rsvp<0 ) return ;
		switch(rsvp)
		{
		case 0:
			// Pizza
			newItem = new PizzaItem() ;
			break ;
		case 1:
			// Shoes
			newItem = new ShoeItem() ;
			break ;
		default:
		}

		newItem.customizeInfo() ;

		System.out.println("You ordered:") ;
		newItem.printInfo() ;
		rsvp = corejava.Console.readInt( "How many to order? " ) ;
		if ( rsvp<1 ) return ;
		try {
			sc.addItem( newItem, rsvp ) ;
		}
		catch ( ShoppingCartFullException scf )
		{ 
			System.out.println("\nERROR: Shopping cart is full.") ;
		}
	}

	private static void printTotalPrice( ShoppingCart sc )
	{
		System.out.println( "\nYour total is " + sc.getTotalCost() ) ;
	}

	private static int menuChooser( String prompt, String [] options )
	{
		System.out.println(prompt) ;
		for (int i=0; i<options.length; i++)
		{
			System.out.println("  "+(i+1)
				+": " + options[i] ) ;
		}
		System.out.println("\n  0: exit menu") ;
		int choice = corejava.Console.readInt("?") ;
		while ( choice<0 || choice>options.length )
		{
			choice = corejava.Console.readInt("0-"
				+ options.length + "?") ;
		}
		return choice-1 ;
	}
}
