/*
 *2013-09-08
 *HW: Unit 2 Practice Assignment: Ch4 Exercise 5c
 *Bradley Forney
 *CIS 127
 *Professor: Craig Sharp
 * 
 */
 
import java.util.Scanner;
import java.util.ArrayList; //Allows for ArrayList type to be used. Used for dynamically expanding arrays needed to track order here

public class TestSandwich
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		
		int breadType; //result from bread decision stored in this variable
		int fillingType; //result from filling decision stored in this variable
				
		ArrayList<String> sandwiches = new ArrayList<String>(); //Holds names of sandwiches for the order
		ArrayList<Integer> calories = new ArrayList<Integer>();	//Holds calories for each sandwich for the order
	
		//Introduction output
		System.out.println("\n---Welcome to the Sandwich Shop!---\n");
		
		//***Begin sandwich decisions***
		int anotherOrder=1; //Terminator for validation while loop
		int tryAgain=1; //Terminator for validation while loop
		while (anotherOrder==1)
		{ 
			tryAgain=1; //Resets "try again" terminator for validation below
			breadType=breadChoice(); //Inquires choice of bread and returns decision made
			fillingType=fillingChoice(); //Inquires choice of filling and returns decision made
			Sandwich sandwich1 = new Sandwich(breadType,fillingType);//Create object Sandwich 1
			sandwiches.add(sandwich1.getSandwichName()); //Add name of sandwich ordered to array
			calories.add(sandwich1.getSandwichCalories()); //Add amount of calories for sandwich to array
			displaySandwiches(sandwiches, calories); //Displays order and calories up from start to this point
			
			//Repeats if invalid entry is entered - contains loop here instead of asking for entire order again
			while (tryAgain==1)
			{
				System.out.println("\nWould you like to order another Sandwich? 1) Yes, 2) No");
				anotherOrder=keyboard.nextInt();
				
				//Validates entry to ensure value is either 1 or 2
				if (anotherOrder==1 || anotherOrder==2)
				{
					tryAgain=0;
				}
			}	
		}
		
		System.out.println("Total Calories for this Order: "+totalCalories(calories));
	}
	
	//Loop that asks user for type of bread desired. Repeats if incorrect entry is made.
	public static int breadChoice()
	{
		Scanner keyboard = new Scanner(System.in);
		int choice=-1;
		while (choice<0 || choice>3)
		{ 
			System.out.println("Please choose your bread (type 1-3 only):");
			System.out.println("1) Wheat"+"\n"+"2) White"+"\n"+"3) Rye"+"\n");
			choice=keyboard.nextInt();
		}
		return choice;
		
	}
	
	//Loop that asks user for type of filling desired. Repeats if incorrect entry is made.
	public static int fillingChoice()
	{
		Scanner keyboard = new Scanner(System.in);
		int choice=-1;
		while (choice<0 || choice>3)
		{ 
			System.out.println("Please choose your filling (type 1-3 only):");
			System.out.println("1) Cheese"+"\n"+"2) Egg Salad"+"\n"+"3) Salami"+"\n");
			choice=keyboard.nextInt();
		}
		return choice;
	}
	
	//Displays contents of sandwich and calorie arrays in a list so user can see cumulative order as they go.
	public static void displaySandwiches(ArrayList<String> sandwiches, ArrayList<Integer> calories)
	{
		System.out.println("\nYou have "+sandwiches.size()+" items: ");
		for (int x=0;x<sandwiches.size();++x)
		{
			System.out.println("Item "+x+": "+sandwiches.get(x)+" @ Calories: "+calories.get(x));
		}
	}
	
	//Returns total calories for entire order
	public static int totalCalories(ArrayList<Integer> calories)
	{
		int total=0;
		int i=0;
		for (int x=0;x<calories.size();++x)
		{
			total=total+calories.get(x);
		}
		return total;
	}
}