I'm practice to make a program that calculate total value of five products. I want the user can keep input the quantities of one of the five product without return the main menu. So I create a while loop to solve the problems. But the problem I have now, how do I make it tract the values the user input. I mean for example, when the user type 1 for the product number, the program display " enter the quantity of product ", and the program would multiply the price of product one to its quantity and store in productTotal1. Then the user continue enter the other value of other product. But what if the user type 1 for the product 1 again and enter its quantity. How do I make the program add previews value, which is the value for total product 1, to the values for product 1 that the user enter afterward? Like the user first enter 2.98*1 and store it in totalProduct1, then the user input 2.98*2 again for product 1. How do I make it add 2.98 + 2.98*2 and store in totalProduct1? Thank you so much. Sorry I kind of wordy. I'm just trying to explain what I'm trying to make the program do.

import java.util.Scanner;

public class RetailSale1
{
	//method to pause until a key is pressed
	public static void pause() 
	{ 
		try 
		{ 
			System.out.print("Press <Enter> to continue..."); 
			System.in.read(); 
		} 
		catch (Exception e)
		{
			System.err.printf("Error %s%c\n",e.getMessage(),7);
		}
	}//end pause
	
	public static void main(String[] args)
	{
		Scanner keyBd = new Scanner( System.in );
		char selection;
		double totalSold;
		int productNumber;
		double quanties;
		double product1 = 2.98;
		double product2 = 4.50;
		double product3 = 3.98;
		double product4 = 4.49;
		double product5 = 6.87;
		double productTotal1 = 0;
		double productTotal2 = 0;
		double productTotal3 = 0;
		double productTotal4 = 0;
		double productTotal5 = 0;

		do{//display menu
			System.out.println("\n--------------");
			System.out.println("1. Enter Product sold");
			System.out.println("2. Display total retail sale");
			System.out.println("3. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
						
			switch (selection){
				case '1':
					System.out.println("\nPlease Enter the product number(1-5): ");
					productNumber = keyBd.nextInt();
					while (productNumber <= 5)
					{					
						System.out.println("Please Enter how many quanties sold: ");
						quanties = keyBd.nextInt();
					
						switch (productNumber)
						{
						case 1:
							productTotal1 = product1 * quanties;
						break;
						case 2:
							productTotal2 = product2 * quanties;
						break;
						case 3:
							productTotal3 = product3 * quanties;
						break;
						case 4:
							productTotal4 = product4 * quanties;
						break;
						case 5:
							productTotal5 = product5 * quanties;
						break;
						default:
							System.out.println("Invalid Product Numbers")
				
						}
						System.out.println("Please Enter the product number(1-5): ");
						productNumber = keyBd.nextInt();
					}
					pause();
					break;
				case '2':
					totalSold = productTotal1 + productTotal2 + productTotal3 + productTotal4 + productTotal5;
					System.out.printf("\nThe total retail value of all products sold is %.2f\n", totalSold);
					pause();
					break;
				case '3':
					//recognize as valid selection but do nothing
					break;
				default :
					System.out.printf("%c\n",7);
					System.out.println("Invalid Selection");
			}//end switch

		}while( selection != '3');
		
		

	}//end main()

}

Hi,
You need to increment the productTotal<productNumber> every time you add something.

Like this:

switch (productNumber)
						{
						case 1:
							productTotal1 += product1 * quanties;
						break;
						case 2:
							productTotal2 += product2 * quanties;
						break;
						case 3:
							productTotal3 += product3 * quanties;
						break;
						case 4:
							productTotal4 += product4 * quanties;
						break;
						case 5:
							productTotal5 += product5 * quanties;
						break;
						default:
							System.out.println("Invalid Product Numbers");
				
						}
						System.out.println("Please Enter the product number(1-5): ");
						productNumber = keyBd.nextInt();
					}
					pause();
					break;

Instead of productTotal1 = product1 * quanties;
you put productTotal1 += product1 * quanties;

The assignment operator += will add the new value to the last one.

In the menu you should create a reset function if you want to delete the values.
Like reset values for product...
or reset all

cause now your productTotal for every product will reset only after you terminate the program

What you do mean by create a reset function? Do you might to explain it a little bit more? I also have another question. I set the while loop productNumber > 0. When I run the program, I type 6 or something greater, it would display "please the enter the quantity sold: " then "invalid product number" How do I make the program just display invalid product number when the user enter something greater than 1? I tried to move something or add something into the program, but it won't work. Is there anyway to do it?

import java.util.Scanner;

public class RetailSale1
{
	//method to pause until a key is pressed
	public static void pause() 
	{ 
		try 
		{ 
			System.out.print("Press <Enter> to continue..."); 
			System.in.read(); 
		} 
		catch (Exception e)
		{
			System.err.printf("Error %s%c\n",e.getMessage(),7);
		}
	}//end pause
	
	public static void main(String[] args)
	{
		Scanner keyBd = new Scanner( System.in );
		char selection;
		double totalSold;
		int productNumber;
		double quanties;
		double product1 = 2.98;
		double product2 = 4.50;
		double product3 = 3.98;
		double product4 = 4.49;
		double product5 = 6.87;
		double productTotal1 = 0;
		double productTotal2 = 0;
		double productTotal3 = 0;
		double productTotal4 = 0;
		double productTotal5 = 0;

		do{//display menu
			System.out.println("\n--------------");
			System.out.println("1. Enter Product sold");
			System.out.println("2. Display total retail sale");
			System.out.println("3. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
						
			switch (selection){
				case '1':
					System.out.println("\nPlease Enter the product number(1-5) or enter 0 to exist: ");
					productNumber = keyBd.nextInt();
					while (productNumber > 0)
					{					
						System.out.println("Please Enter how many quanties sold: ");
						quanties = keyBd.nextInt();
						switch (productNumber)
						{
						case 1:
							productTotal1 += product1 * quanties;
						break;
						case 2:
							productTotal2 += product2 * quanties;
						break;
						case 3:
							productTotal3 += product3 * quanties;
						break;
						case 4:
							productTotal4 += product4 * quanties;
						break;
						case 5:
							productTotal5 += product5 * quanties;
						break;
						default:
							System.out.println("Invalid Product Numbers");
						break;
				
						}
						System.out.println("Please Enter the product number(1-5): ");
						productNumber = keyBd.nextInt();
					}
					pause();
					break;
				case '2':
					totalSold = productTotal1 + productTotal2 + productTotal3 + productTotal4 + productTotal5;
					System.out.printf("\nThe total retail value of all products sold is %.2f\n", totalSold);
					pause();
					break;
				case '3':
					//recognize as valid selection but do nothing
					break;
				default :
					System.out.printf("%c\n",7);
					System.out.println("Invalid Selection");
			}//end switch

		}while( selection != '3');
		
		

	}//end main()

}

A reset function was just a suggestion. Because you modified productTotal3 += product3 * quanties; , if you don't exit the application, the productTotal will always increment. It will be nice if you will create a 4th menu option to reset the total retail sale or the total for a specific product. The "extra mile" in your homework.

Now for you code. It's obvious that it would display "please the enter the quantity sold: " if you input any kind of number.
Test case:
1. case 1: //from menu
2. println("\nPlease Enter the product number(1-5) or enter 0 to exist: ");
3. productNumber = keyBd.nextInt(); //next number you input
4. while productNumber == 1 true
5. println("Please Enter how many quanties sold: ");
6. quanties = keyBd.nextInt(); // you ask for the quantities
7. switch // and do the math
8. productTotal1 += product1 * quanties;
9. println("Please Enter the product number(1-5): "); //next line after the switch
10.productNumber = keyBd.nextInt(); //next input

11. while productNumber == 8 true // and now the problems appear :)

12. println("Please Enter how many quanties sold: ");
13. quanties = keyBd.nextInt(); // you ask for the quantities
14. switch // and only here your code will know to print that the number is no good
the default case. You print the "quanties" line and only after that your switch will know what to do.

Always do that on a white paper. Or use an IDE and debug your code, but it's better on a paper, cause you will learn faster.
I hope you see the problem and that it will help you to see the fix for it.

PS. The default case appears to late in your code. Now, the while (productNumber > 0) is not enough. Use while producNumber != 0, so if you input 0 the program exits to menu. For the numbers greater than 5 use an if statement. I gave you too much. :)
good luck

Edited 5 Years Ago by hertze_bogdan: n/a

@ hertze_bogdan

Always do that on a white paper. Or you an IDE and debug your code, but it's better on a paper.

++

@ hertze_bogdan

Always do that on a white paper. Or you an IDE and debug your code, but it's better on a paper.

++

That's the spirit!

Yeah, it is so much clear after list it out. I fixed it somehow, but I hope this is what you mean. About the rest thing, I think I'll do it on my own when I turn in my homework. By the way, I was thinking about you !=0, but I thought it might case a logic errors or something. About the debug tool, how do you use it? My teacher say he is going to teach us, but.... he does not teach us yet. I'm using eclipses or jGrasp. Thank you for explain so detail.

import java.util.Scanner;

public class RetailSale2
{
	public static void pause() 
	{ 
		try 
		{ 
			System.out.print("Press <Enter> to continue..."); 
			System.in.read(); 
		} 
		catch (Exception e)
		{
			System.err.printf("Error %s%c\n",e.getMessage(),7);
		}
	}
	
	public static void main(String[] args)
	{
		Scanner keyBd = new Scanner( System.in );
		char selection;
		double totalSold;
		int productNumber;
		double quanties;
		double product1 = 2.98;
		double product2 = 4.50;
		double product3 = 3.98;
		double product4 = 4.49;
		double product5 = 6.87;
		double productTotal1 = 0;
		double productTotal2 = 0;
		double productTotal3 = 0;
		double productTotal4 = 0;
		double productTotal5 = 0;

		do{
			System.out.println("\n--------------");
			System.out.println("1. Enter Product sold");
			System.out.println("2. Display total retail sale");
			System.out.println("3. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
						
			switch (selection){
				case '1':
					System.out.println("\nPlease Enter the product number(1-5) or enter 0 to exist: ");
					productNumber = keyBd.nextInt();
					while (productNumber != 0)
					{					
												
						switch(productNumber)
						{
							case 1:
							System.out.println("Please enter the quanties sold?");
							quanties = keyBd.nextInt();
							productTotal1 += product1 * quanties;
							System.out.printf("retail price for product 1 is %.2f", productTotal1);
							break;
							case 2:
							System.out.println("Please enter the quanties sold?");
							quanties = keyBd.nextInt();
							productTotal2 += product2 * quanties;
							System.out.printf("retail price for product 2 is %.2f", productTotal2);
							break;
							case 3:
							System.out.println("Please enter the quanties sold?");
							quanties = keyBd.nextInt();
							productTotal3 += product3 * quanties;
							System.out.printf("retail price for product 3 is %.2f", productTotal3);
							break;
							case 4:
							System.out.println("Please enter the quanties sold?");
							quanties = keyBd.nextInt();
							productTotal4 += product4 * quanties;
							System.out.printf("retail price for product 4 is %.2f", productTotal4);
							break;
							case 5:
							System.out.println("Please enter the quanties sold?");
							quanties = keyBd.nextInt();
							productTotal5 += product5 * quanties;
							System.out.printf("retail price for product 5 is %.2f", productTotal5);
							break;
							default:
								System.out.println("Invaild product number!!");
			
						}
						
						System.out.println("\nPlease Enter the product number(1-5) or enter 0 to exist: ");
						productNumber = keyBd.nextInt();
					}
					pause();
					break;
				case '2':
					totalSold = productTotal1 + productTotal2 + productTotal3 + productTotal4 + productTotal5;
					System.out.printf("\nThe total retail value of all products sold is %.2f\n", totalSold);
					pause();
					break;
				case '3':
					break;
				default :
					System.out.printf("%c\n",7);
					System.out.println("Invalid Selection");
			}

		}while( selection != '3');
		
		

	}//end main()

}
This question has already been answered. Start a new discussion instead.