THis program is supposed to take a number (1-5) from the user. After each number is entered, it should ask how many of each product they want, then show the total for that product and and overall total at the end. I have to use a swich statement. And stop execution when a neg 1 is entered. My question is this: I have my switch statement inside the while loop. The program asks the user to enter a number one time, and then asks to enter the amount of the item five times, totals each item, and then show the overall total. like this
Enter number 1-5:
user >> a number
enter amount of this product
user >> a number
enter amount of this product
user >> a number
and it asks that three more times, totaling it each time.

it should be like this
Enter a number
>>1
How many
>>1
the total of this product is
Enter a number
>>2
How many
>>2
the total of this product is
and so on until this has comleted 5 times
then it should display the overall total.

How can I change my loop structure to get this program right?

/* Lauren Hicks
CS 2265: Advanced Programming Using Java
Week 2 Programming Assignment-
Java Application to Calculate the Sum of Different
Quantities of Miscellaneous Retail Items with User Interaction
*/
import javax.swing.JOptionPane;
public class Retail
{
	public static void main(String[] args)
	{
	int val = 1, selection=0;
	String selectionString, optionOneString, optionTwoString, optionThreeString, optionFourString, optionFiveString; 
	int optionOne, optionTwo, optionThree, optionFour, optionFive;
	double priceOne = 2.98, priceTwo = 4.50, priceThree = 9.98, priceFour = 4.49, priceFive = 6.87;
	double orderTotal = 0, opOneTotal = 0, opTwoTotal=0, opThreeTotal=0, opFourTotal=0, opFiveTotal=0;
	
	while (val<6)
	{
		selectionString = JOptionPane.showInputDialog(null, "Enter a number, 1-5, representing a product type. Entry of -1 stops program execution", "Product Type", JOptionPane.INFORMATION_MESSAGE);
		selection = Integer.parseInt(selectionString);
		while (selection != 1 && selection !=2 && selection !=3 && selection != 4 && selection != 5)
		{
		System.exit(0);
		}
	
	switch(selection)
	{
	case 1:
		optionOneString = JOptionPane.showInputDialog(null, "Enter how many of this product you wish to purchase ", "Product Amount", JOptionPane.INFORMATION_MESSAGE);
		optionOne = Integer.parseInt(optionOneString);
		opOneTotal = priceOne * optionOne;
		System.out.println("The total for product one is: $" + priceOne);
	case 2:
		optionTwoString = JOptionPane.showInputDialog(null, "Enter how many of this product you wish to purchase ", "Product Amount", JOptionPane.INFORMATION_MESSAGE);
		optionTwo = Integer.parseInt(optionTwoString);
		opTwoTotal = priceTwo * optionTwo;
		System.out.println("The total for product two is: $" + opTwoTotal);
	case 3:
		optionThreeString = JOptionPane.showInputDialog(null, "Enter how many of this product you wish to purchase ", "Product Amount", JOptionPane.INFORMATION_MESSAGE);
		optionThree = Integer.parseInt(optionThreeString);
		opThreeTotal = priceThree * optionThree;
		System.out.println("The total for product three is: $" + opThreeTotal);
	case 4:
		optionFourString = JOptionPane.showInputDialog(null, "Enter how many of this product you wish to purchase ", "Product Amount", JOptionPane.INFORMATION_MESSAGE);
		optionFour = Integer.parseInt(optionFourString);
		opFourTotal = priceFour * optionFour;
		System.out.println("The total for product four is: $" +opFourTotal);
	   
	case 5:
		optionFiveString = JOptionPane.showInputDialog(null, "Enter how many of this product you wish to purchase ", "Product Amount", JOptionPane.INFORMATION_MESSAGE);
		optionFive = Integer.parseInt(optionFiveString);
		opFiveTotal = priceFive + optionFive;
		System.out.println("The total for product five is: $" + opFiveTotal);
	
		orderTotal = opOneTotal + opTwoTotal + opThreeTotal + opFourTotal + opFiveTotal;
		System.out.println("The total price of your order is: $" + orderTotal);
		System.exit(0);
	}
}
}
}

i think you just want for the loop to iterate one and then ask the user to either repeat the process or stop
if i'm correct i think you'd want to do something like this:

import java.util.Scanner // put this at the top

// code

Scanner keyboard = new Scanner(System.in);
int p = 0;
while(p!=-1)
{
//code
System.out.println("Enter a -1 to end the program or another number to repeat the process");
p = keyboard.nextInt();
}

i hope this helps

The program asks the user to enter a number one time, and then asks to enter the amount of the item five times, totals each item, and then show the overall total.

What is the purpose of the first number the system asks the user to enter? Is it to verify that the user wants to continue the program? Either way, for the second part, where the system asks the user to enter the amount of the item five times, you should use a for loop.

for (int i = 0; i < 5; i++){
//Ask user for the amount of the item
//add this amount to the total amount
}

P.S.

If you click "Reply w/ Quote" on my post, you will see how to use code tags for java syntax. Although it is appreciated that you at least used them period :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.