Repeat while answer yes - do while loop
Can someone help, it seems like either i type in "y" or "n", the program won't go back to main menu...
also compile error:
variable itemPrice might not have been initialized.
Should i change to while loop instead? I'm new to while and do-while loop.. Please advice.
import java.text.*; // to use DecimalFormat
import java.util.*; // to use Scanner class
import javax.swing.*; // In order to use JOptionPane
public class ModOrderSystemA
{
public static final double TAX_AMOUNT = 0.50;
public static void main(String[] args)
{
String choice, itemName, input, total, answer= "Y";
int quantity;
double itemPrice, priceBeforeTax, priceAfterTax;
boolean anyMore = true;
Scanner keyboard = new Scanner (System.in);
//Display the food menu
do
{
choice =
JOptionPane.showInputDialog("Welcome to the Restruant!\n" +
"Here is today's special: \n" +
"A. Rice $1.50\n" +
"B. Noddle $2.00\n" +
"C. Soup $2.50\n" +
"Please enter your order(A,B,C:");
choice = choice.toUpperCase();
if (! choice.equals ("A") &&
! choice.equals ("B") &&
! choice.equals ("C"))
{
JOptionPane.showMessageDialog (null,
"We don't have choice " + choice + " in our menu.\n" +
"The order has to be A,B,C,D,E,F or G");
}
//else if(choice.equals ("A") &&
// choice.equals ("B") &&
// choice.equals ("C"))
else
{
input =
JOptionPane.showInputDialog("How many would you like to order:");
quantity = Integer.parseInt(input);
if (quantity <= 0)
{
JOptionPane.showMessageDialog (null,
"Error: Invalid quantity.\n");
}
else
{
// Display name of the Food
// Set price of the item
if (choice.equals ("A"))
{
itemName = " Rice";
itemPrice = 1.50;
}
else if (choice.equals ("B"))
{
itemName = " Noddle";
itemPrice = 2.00;
}
else
{
itemName = " Soup";
itemPrice = 2.50;
}
JOptionPane.showMessageDialog (null,
" You ordered " + quantity + itemName);
answer = JOptionPane.showInputDialog("Would you like another order?\n" +
"Please enter Y or y for another order.\n" +
"Enter N or n to exit");
answer = keyboard.next ();
}
}
if(!answer.equalsIgnoreCase("y"))
{
anyMore = false;
priceBeforeTax = itemPrice * quantity;
priceAfterTax = priceBeforeTax + TAX_AMOUNT;
DecimalFormat oneAfter = new DecimalFormat ("0.00");
JOptionPane.showMessageDialog(null,
"Here is the total price for your orders: \n" +
"Price before tax is $" + oneAfter.format(priceBeforeTax) +
"\nTax is $" + TAX_AMOUNT +
"\nPrice after tax is $" + oneAfter.format(priceAfterTax) +
"\n\nThank you for your order and See you again!");
}
}while(answer.equalsIgnoreCase("y"));
}
}