I'm having some trouble with my code. I want to calculate the final balances of some movie tickets based on their day or night showing (and depending on if they are children or adults). So at the end of the code, I've been trying to get the solution to a variable that I had in the second while loop. It seems like I have to have the variables !time.equalsIgnoreCase("D") and !time.equalsIgnoreCase("E") in the second while loop. I've read that a variable terminates after the loop is done. Can someone please help me out?
import java.util.Scanner; // Needed for the Scanner class
import java.text.NumberFormat; // Needed for number format class
public class movie
{
// Create a Scanner object to read input.
static Scanner keyboard = new Scanner (System.in);
public static void main(String[ ] args)
{
boolean cinema = true; // Needed to apply master loop.
boolean validInput; // Needed to apply initial loop.
boolean fullday; // Needed to apply initial loop.
boolean list = true; // Needed to apply secondary loop.
boolean decision=true; // Needed to apply secondyloop.
boolean yesno = true; // Needed to apply secondary loop.
NumberFormat nf = NumberFormat.getNumberInstance( ); // Defines the number format.
double adult = 8.25; // Stores the adult ticket price.
double day; // Stores the day time ticket price.
double children1; // Stores the children ticket prices.
double children2; // Stores the children ticket prices.
double total1; // Stores the total cost.
double total2; // Stores the total cost.
double total; // Stores the overall total cost.
int atickets; // Stores the number of adult tickets.
int ctickets; // Stores the number of child tickets.
int enter; // To hold the movie choice.
int capacity; // The total theater capacity.
String answer; // To hold an answer to the first Yes or No question.
String answer2; // To hold an answer to the Day or Evening question.
String time; // To hold an answer to the Day or Evening question.
String sol; // To hold an answer to the second Yes or No question.
nf.setMinimumFractionDigits(2); // Sets the minimum decimal places.
nf.setMaximumFractionDigits(2); // Sets the maximum decimal places.
System.out.println("\nWelcome to the Movie Theater");
// Calculate the price of a day ticket.
day = adult * 0.80;
// Calculate the price of a child's ticket based on the evening price.
children1 = adult * 0.70;
// Calculate the price of a child's ticket based on the day price.
children2 = day * 0.70;
while(cinema)
{
while(list)
{
System.out.println("\nHere are the movies showing presently: ");
System.out.println(" ");
System.out.println(" Title" + " Daytime Price" + " Child Daytime Price"+ " Evening Price" + " Child Evening Price");
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("1. Burn After Reading " + nf.format(day) + " " + nf.format(children2) + " " + nf.format(adult) + " " + nf.format(children1));
System.out.println("2. Journey to the Center of the Earth 3D " + nf.format(day) + " " + nf.format(children2) + " " + nf.format(adult) + " " + nf.format(children1));
System.out.println("3. Tropic Thunder " + nf.format(day) + " " + nf.format(children2) + " " + nf.format(adult) + " " + nf.format(children1));
System.out.println("4. Lakeview Terrace " + nf.format(day) + " " + nf.format(children2) + " " + nf.format(adult) + " " + nf.format(children1));
System.out.println(" ");
System.out.print("\nPlease choose a movie number: ");
do
{
enter = keyboard.nextInt( ); // Stores the chosen movie.
validInput = true;
if ( (enter < 1) || (enter > 4)) // Defines the invalid boundaries (but true by definition).
{
System.out.print ("\n(You must pick a movie 1 through 4): ");
validInput = false;
}
}
while(!validInput);
System.out.print("\nYou chose movie number " + enter + ". Is this correct? (Y or N)?: ");
do
{
answer = keyboard.next ();
yesno = true;
if (!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")) // Defines the invalid boundaries (but true by definition).
{
System.out.print ("\n(You must pick Y or N): ");
yesno = false;
}
}
while (!yesno);
do
{
if (answer.equalsIgnoreCase("N")); // Ignores case sensitivity.
else if (answer.equalsIgnoreCase("Y")) // Ignores case sensitivity.
list = false; // If Yes is recorded, then we may proceed. If no is recorded, then we begin the loop over again.
else
yesno = false;
}
while (!yesno);
}
while(decision)
{
System.out.print("\nIs this a day or an evening show?: (Please answer with D for day or E for evening): ");
do // Makes sure the movie choice is valid.
{
time = keyboard.next( ); // Stores the chosen movie.
fullday = true;
if (!time.equalsIgnoreCase("D") && !time.equalsIgnoreCase("E")) // Defines the invalid boundaries (but true by definition).
{
System.out.print ("\n(You must type in D or E): ");
fullday = false;
}
}
while (!fullday);
System.out.print("\nYou chose " + time + ". Is this correct? (Y or N)?: ");
do // Asks the user if he/she is sure of the choice. If yes, then the program continues on. If not, the program loops back to the beginning menu.
{
answer2 = keyboard.next (); // Stores the answer to the question above.
fullday = true; // If Yes is recorded, then we may proceed. If no is recorded, then we begin the loop over again.
if (!answer2.equalsIgnoreCase("Y") && !answer2.equalsIgnoreCase("N"))
{
System.out.print ("\n(You must pick Y or N): ");
fullday = false;
}
}
while (!fullday);
do
{
if (answer2.equalsIgnoreCase("N")); // Ignores case sensitivity.
else if (answer2.equalsIgnoreCase("Y")) // Ignores case sensitivity.
decision = false; // If Yes is recorded, then we may proceed. If no is recorded, then we begin the loop over again.
else
fullday = false;
}
while(!fullday);
}
// Calculate the adult tickets.
System.out.print("\nHow many adult tickets would you like to purchase?: ");
atickets = keyboard.nextInt( );
// Calculate the children's tickets.
System.out.print("\nHow many children's tickets would you like to purchase?: ");
ctickets = keyboard.nextInt( );
// Calculate the final balance based on the day or evening pick.
if (time.equalsIgnoreCase("D"))
total1 = atickets * day + ctickets * children2;
else if (time.equalsIgnoreCase("E"))
total2 = atickets * adult + ctickets + children1;
// Calculate the final balance.
total = total1 + total 2;
System.out.println("Your total is $" + nf.format(total) + ".");
break;
}
}
}