I am writing a simple app that is required to have a switch structure, used with a sentinal. It compiles fine, but I am only able to as far as my second JOption pane and enter data. After that the program stops. I think the the problem is somewhere in my use of a while statement with a switch. See below:
public class MailOrder {
public static void main(
String args[] )
{
int product, // first number to add
quantitySold; // second number to add
double total, // total of all products
allValue;
String productNumber; // product sold
// read in productNumber from user
productNumber =
JOptionPane.showInputDialog(
"Enter number for product sold 1 through 5\nEnter -1 to Quit\n");
String quantity; // number of products sold
// read in quantity from user
quantity =
JOptionPane.showInputDialog(
"Enter quantity of product sold");
// convert numbers from type String to type integer
product = Integer.parseInt( productNumber );
quantitySold = Integer.parseInt( quantity );
total = 0;
allValue = 0;
while ( product != -1 ) {
// add Retail Value to total
allValue = total + allValue;
switch(product) {
case 1:
total = total + (quantitySold * 2.98);
break;
case 2:
total = total + (quantitySold * 4.50);
break;
case 3:
total = total + (quantitySold * 9.98);
break;
case 4:
total = total + (quantitySold * 4.49);
break;
case 5:
total = total + (quantitySold * 6.87);
default:
JOptionPane.showMessageDialog(
null, "Invalid value entered" );
// read in productNumber from user
productNumber =
JOptionPane.showInputDialog(
"Enter number for product sold 1 through5\nEnter -1 to Quit\n");
// read in quantity from user
quantity =
JOptionPane.showInputDialog(
"Enter quantity of product sold");
// convert numbers from type String to type integer
product = Integer.parseInt( productNumber );
quantitySold = Integer.parseInt( quantity );
}
// termination phase
DecimalFormat precisionTwo = new DecimalFormat( "0.00" );
if ( product == -1 ) {
// add Retail Value to total
allValue = total + allValue;
// display value of all products sold
JOptionPane.showMessageDialog( null,
"Total Retail Value of all products sold Last Week" + precisionTwo.format(allValue),
"Value of all Products",
JOptionPane.INFORMATION_MESSAGE );
}
System.exit( 0 ); //terminate the program
}
}
}
/****************************END*****************************/