Hi,
I've been working on this program for nearly two days straight and I just can't get it quite right. So it's supposed to get input from the user and apply it to a switch statement. There are 5 cases that represent the cost per item for each case. Then it asks the user for the quantity of that item that they'd like to order. After that, it should tell the user the total for the quantity of the chosen item and then what the overall order total is up to that point. It is supposed to continue asking them what item and quantity they want and displaying the total for the entered quantity of the chosen item and keep totaling up the total purchase amount until an invalid item number is entered. I suppose at that point it should NOT ask how many of that item they would like (because they entered an invalid item number) and go straight to telling them the overall total for the order and then end. Anyway, so far my program will ask for item number and quantity, and it seems to correctly calculate the totals for the first entry. It will then ask for the next item number, but when it's entered, the program ends. I have also noticed that if I enter an invalid item number for the first prompt, it will say that it's invalid but it will continue on to ask for the quantity, and then say that the totals are 0.0. I have tried moving various pieces of my code here and there and everywhere but I can't figure out the right order. I may be wrong but, I feel like I have everything there that I need but possibly in the wrong order. Can someone PLEASE point me in the right direction?
/**
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;
String productString;
int product = 0;
double grandtotal = 0;
int quantity = 0;
productString = JOptionPane.showInputDialog(null, "What product (1-5) would you like to order?", "Product Number", JOptionPane.QUESTION_MESSAGE);
product = Integer.parseInt(productString);
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.ERROR_MESSAGE);
}
int x = product;
if(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);
}
else
{
JOptionPane.showMessageDialog(null, "The program will now close.", "Warning!", JOptionPane.ERROR_MESSAGE);
}
double 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 product that you would like to order.", "Product Number", JOptionPane.QUESTION_MESSAGE);
}
}