import java.util.*;
/*********************************************
 * Shopper simulates the action of a shopper
 ********************************************/
public class Shopper
{

	/*****************************************
	 * main method will present user with options of 
	 * what produce is in stock.  The user will choose the
	 * quantity to add to the basket, from 0 - 10 items.
	 *
	 * @param args command line arguments (unused)
	 *****************************************/
	public static void main(String args[])
	{
		int 	  		 itemQuantity;	// number of items
		Scanner 		 stdIn;			// standard input
		Item 	       oneItem;		// each item
		MarketBasket myBasket;		// my Shopping Basket
		boolean      shopAgain; 	// shop again
		String		 inLine;    	// incoming line
		
		// instantiate the Scanner and basket and initialize quantity
		stdIn = new Scanner(System.in);
		myBasket = new MarketBasket();


		// Welcome message and explanation
		System.out.println("Welcome to Dukes Cukes");
		System.out.println("\nFrom the list of items presented, you will " +
			"choose the quantity that you want.\nAt the end, I will show your " + 
			"total.\n");		
		
		//*********************************************
		// For each produce item, display description and ask for 
		// quantity
		//*********************************************
		for (Produce thing : Produce.values())
		{
			itemQuantity = -1;
							
			// make sure that we get a good value (not character) 
			while (itemQuantity < 0 || itemQuantity > 10)
			{
				System.out.print(thing); // using toString
				System.out.print(" How many would you like (0 - 10)? ");

				// in case of user entry error	
				try
				{
					itemQuantity = stdIn.nextInt();
					
					// if the values are out of range it is an error
					if (itemQuantity < 0 || itemQuantity > 10)
					{
						System.out.println("The value you entered, " + itemQuantity + 
							" is out of range.  You must enter a number between 0 and 10");
					}

				}
				// display error message and consume the bad entry
				catch (InputMismatchException ime)
				{
					System.out.println("The value you entered, " + stdIn.nextLine()
						+ " is not an integer.  Please try again.");
				}
			} // while
			
			// add items to the market basket if purchased
				// create an item from the produce and quantity.
			if (itemQuantity > 0)
			{
				oneItem = new Item(thing, itemQuantity);
				// and add to basket
				myBasket.addItem(oneItem);
			}					
								
		} // for
		
		System.out.println("\nHere is your receipt");
		System.out.print(myBasket);		
		
		System.out.println("Thank you for shopping at Dukes Cukes.");
		System.out.println("This program has ended normally.");
	}
}	
