Hi,
I'm having trouble with...I don't know, either choosing the correct loop to use or maybe using the one I need but having it incorrectly placed. Right now, the program will give the "Invalid item number" message from my else statement if I enter an invalid value in the first prompt only and then close. I need it to do that on any prompt for the item number, not just the first one. I also noticed that it seems my switch statement and the calculations work for the first set of prompts but after that, no matter what item number I enter, it still seems to think that it's the value for the first case that I entered but the calculations still work based on the quantity I put in, it just still calculates it using the value from whatever case I entered in the first prompt. I may be wrong but, I'm thinking this also has to do with my loop, whether it be that I'm using the wrong kind or that it's just placed wrong. I don't think I can use a for loop because what I need is a sentinel-controlled loop that will stop when an invalid number (a value not 1-5) is entered into the prompt asking what item the user would like to order. I don't think the for loop allows that. I am still new to programming so I may be way off the mark here. Anyway, here's my code. Can anyone see where I might be messing up?
/**
Program #3
Function: Calculate order total using user input.
Program by: Barbara Harris (March 31, 2010)
*/
import javax.swing.JOptionPane;
public class order
{
public static void main(String[] args)
{
String quantityString;
int quantity;
String productString;
int product = 0;
double total;
double grandtotal = 0;
int x;
productString = JOptionPane.showInputDialog(null, "What product (1-5) would you like to order?", "First Item Number", JOptionPane.QUESTION_MESSAGE);
product = Integer.parseInt(productString);
x = product;
if(x > 0 && x <= 5)
{
while(x > 0 && x <=5)
{
quantityString = JOptionPane.showInputDialog(null, "How many of that product would you like to order?", "Quantity", JOptionPane.QUESTION_MESSAGE);
quantity = Integer.parseInt(quantityString);
double prodprice = 0;
switch(product)
{
case 1:
prodprice = 2.00;
break;
case 2:
prodprice = 4.00;
break;
case 3:
prodprice = 6.00;
break;
case 4:
prodprice = 5.00;
break;
case 5:
prodprice = 3.00;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid product number.", "Error", JOptionPane.INFORMATION_MESSAGE);
break;
}
total = quantity * prodprice;
grandtotal = grandtotal + total;
JOptionPane.showMessageDialog(null, "The total amount for this product is $" + total + ".", "Item Total", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "The total of your entire order so far is $" + grandtotal + ".", "Grand Total", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showInputDialog(null, "Please enter the number of the next item you would like to add to your order.", "Item Number", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid item number.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}