import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Lunch
{

	
	// field declarations
	private static double appleEachPrice;
	private static double breadLoafPrice;
	private static double cheesePoundPrice;
	
	private static String inputSelection;
	private static String inputApples;
	private static String inputBread;
	private static String inputCheese;
	
	private static final double BUDGET = 27.50;
	private static double spndAmt;
	private static double overBudget;
	private static double underBudget;
	
	private static int intSelection;
	private static int amtApples;
	private static int amtBread;
	private static double amtCheese;
	private static int apNum;
	private static int loafNum;
	private static double chsAmt;	
		
	private double totalApples;
	private double totalBread;
	private double totalCheese;
	private double totalCost;
	

	
//constructor method(s)

	
		public Lunch() throws NegBalanceException
	{
		appleEachPrice = 1.29;
		breadLoafPrice = 2.15;
		cheesePoundPrice = 4.23;
		if (spndAmt > BUDGET)
			throw(new NegBalanceException());

	}
	



	
// shopping method with loop

	public static void Shop()throws NumOutOfRangeException
{
	spndAmt = 0;
	
	try
	{


	do
	{
		inputSelection = JOptionPane.showInputDialog(null, "Please enter shopping selection. \nEnter 1 for an Apple. \nEnter 2 for a loaf of bread. \nEnter 3 for a pound of cheese. \nEnter 0 to exit.", "Shopping Dialog", JOptionPane.QUESTION_MESSAGE);
		intSelection = Integer.parseInt(inputSelection);
	
	switch (intSelection)
		{
			case 0:
				System.out.println("Thank you");
				break;
		
			case 1:
				spndAmt += appleEachPrice;
				amtApples += 1;
				break;

			case 2:
				spndAmt += breadLoafPrice;
				amtBread += 1;
				break;
		
			case 3:
				spndAmt += cheesePoundPrice;
				amtCheese += 1;
				break;
			
				
		
			default: 
				JOptionPane.showMessageDialog(null, "Invalid Entry. Please try again.");
		}
		
		
	}while (spndAmt <= BUDGET && intSelection != 0);

		}
	catch(NumberFormatException notInt)
		{
			JOptionPane.showMessageDialog(null, "Invalid Entry. Please try again.");
			Shop();
		}	

}


	




//get/set methods for field declarations	

	public static double getBudget()
	{
		return BUDGET;
	}




	
//calculations method
	public double CalculateCost()
	{
		totalApples = appleEachPrice * amtApples;
		totalBread = breadLoafPrice * amtBread;
		totalCheese = cheesePoundPrice * amtCheese;
		totalCost = totalApples + totalBread + totalCheese;
		return totalCost;
	}	
	

// Display method
	public void Display()
	{
	DecimalFormat df = new DecimalFormat("$0.00");
	df.setMaximumFractionDigits(2);
	df.setMinimumFractionDigits(2);
	totalCost = CalculateCost();
	boolean isBudgetExceeded = (totalCost > BUDGET);
			
 // print itemized
	System.out.println("You have purchased " + amtApples + " apples (" + df.format(totalApples) + 
	"), " + amtBread + " loaves of bread (" + df.format(totalBread) + "), and " + amtCheese + " pounds of cheese (" 
	+ df.format(totalCheese) + ").");
 // print totals and budget comparison
 	if (spndAmt > BUDGET)
	{	overBudget = spndAmt - BUDGET;

		System.out.println("The cost of the lunch is " + df.format(totalCost) + " and you have " + df.format(BUDGET) +
		" dollars to spend. You are " + df.format(overBudget) + " overbudget!");	
	}
	else
	{
		overBudget = BUDGET - spndAmt;
		
		System.out.println("The cost of the lunch is " + df.format(totalCost) + " and you have " + df.format(BUDGET) +
		" dollars to spend. You have " + df.format(overBudget) + " left!");
	}

 // print blank line
 	System.out.println("");
	}

	
}	
