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);
		
		
	}	 
}

Recommended Answers

All 6 Replies

you'll need to put a loop (for or while) in your code.
a switch on it's own will not repeat itself

for the other problem .. if you enter a number, it's either > 0, or <= 5
for instance: 17 is invalid, but still: 17 > 0
-2 is also invalid, but: -2 <= 5
so you basically told your program to ask for the quantity, whether you got valid input or not

Hello,

I think the best idea would be to create an endless loop, something like:

while (1 == 1)
{
switch statement....
}

Then, in the switch statement, set the condition for 'default' to be 'continue'. This way, anything other then 1-5 will pass to the default condition, essentially escaping the loop. So, something like:

default:
      continue;
      break;

This will escape the endless loop and let you conitnue to the remaining portions of the program. Of course, you need more then just the switch statement in the loop, as you need to be keeping a running total and re-prompting.

Hope this helps!!

you'll need to put a loop (for or while) in your code.
a switch on it's own will not repeat itself

for the other problem .. if you enter a number, it's either > 0, or <= 5
for instance: 17 is invalid, but still: 17 > 0
-2 is also invalid, but: -2 <= 5
so you basically told your program to ask for the quantity, whether you got valid input or not

I see what you mean. I didn't even think of that. But how do you suggest I make ONLY 1-5 valid input? The code won't accept something like 0 < x < 6, will it? I can't think how to go about fixing that if it won't accept that format.

As for the loops, I did have a loop in there. I tried for and I tried while but I still didn't have much luck so I took it out. Maybe I wrote them wrong or placed them in the wrong place. I'll try again and if I still can't get it, I'll post the code with the loop that I put in it in here.

I see what you mean. I didn't even think of that. But how do you suggest I make ONLY 1-5 valid input? The code won't accept something like 0 < x < 6, will it? I can't think how to go about fixing that if it won't accept that format.

As for the loops, I did have a loop in there. I tried for and I tried while but I still didn't have much luck so I took it out. Maybe I wrote them wrong or placed them in the wrong place. I'll try again and if I still can't get it, I'll post the code with the loop that I put in it in here.

Change from or to and, so instead of (x > 0 || x <=5), it should be (x > 0 && x <=5). This way it MUST fall between 0 and 5, else it is invalid.

Alternativly, you can do it long hand and go with (x == 1 || x == 2 || x == 3 || x == 4 || x == 5).

Change from or to and, so instead of (x > 0 || x <=5), it should be (x > 0 && x <=5). This way it MUST fall between 0 and 5, else it is invalid.

Alternativly, you can do it long hand and go with (x == 1 || x == 2 || x == 3 || x == 4 || x == 5).

Hi,

Thanks! Not long after I asked that question, I remembered I could use &&. Sorry about that. Anyway, now 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 and invalid value in the first prompt only and then close. I need it to to 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 updated code. Can you 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);
   	}
	}            
}

I think my program is FINALLY doing what it's supposed to now!!! Thanks to everyone who responded. You were all a big help! Just in case someone else who needs help comes across this post, I'm pasting my WORKING code in this message. Thanks again to any and everyone who helped!

/**
      Program #3
      
      Function: Calculate order total using user input.
      
      Program by: Barbara Harris (March 31, 2010)
      
*/

import javax.swing.JOptionPane;
public class order1
{
   public static void main(String[] args)
   {
   
   	String quantityString;
      int quantity;
		String productString;
      int product;
      double total;
      double grandtotal = 0;
      int x;
	
     

           
			
			productString = JOptionPane.showInputDialog(null, "What product (1-5) would you like to order?", "Item Number", JOptionPane.QUESTION_MESSAGE);
     		product = Integer.parseInt(productString);
			x = product;     
        
			 
      	while(x > 0 && x <=5)
   		{
						
      						
					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);
     					break;
         	}
				
				quantityString = JOptionPane.showInputDialog(null, "How many of that product would you like to order?", "Quantity", JOptionPane.QUESTION_MESSAGE);
         	quantity = Integer.parseInt(quantityString);

				
				total = quantity * prodprice;
         	grandtotal = grandtotal + total;

				
         	JOptionPane.showMessageDialog(null, quantity + " of product #" + product + " costs $" + total + ".", "Item Total", JOptionPane.INFORMATION_MESSAGE);
         	JOptionPane.showMessageDialog(null, "The total of your entire order so far is $" + grandtotal + ".", "Grand Total", JOptionPane.INFORMATION_MESSAGE);
      		productString = JOptionPane.showInputDialog(null, "Please enter the number of the next item that you'd like to order.", "Item Number", JOptionPane.QUESTION_MESSAGE);
				product = Integer.parseInt(productString);
				x = product;	
			}
			
			JOptionPane.showMessageDialog(null, "Invalid item number!", "Error", JOptionPane.ERROR_MESSAGE);
			JOptionPane.showMessageDialog(null, "Your order total is $" + grandtotal + ".", "Order Complete", JOptionPane.INFORMATION_MESSAGE);
			
		}
	}
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.