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