Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows:

Respuesta :

Answer:

Follows are the code to the given question:

import java.util.*;//import package for input  

public class Main//defining a class Main  

{

public static void main(String[] arp)//defining main method  

  {

   double total=0,sub_Total=0;//defining double variable

   int Units,product_Num;//defining integer variable

   Scanner obq = new Scanner(System.in);//creating Scanner class object for input value

   System.out.print("Product: ");//print message

   product_Num=obq.nextInt();//input value

   while(product_Num>=1 && product_Num<=5)//defining loop for input value

   {

       sub_Total=0;//assign value 0 in sub_Total

       switch(product_Num)//use switch for check condition

          {

          case 1://for case 1

              {

              System.out.print("Enter No of units :");//print message

              Units=obq.nextInt();//input value

              sub_Total=Units*2.98;//calculating sub_Total value

              total+=sub_Total;//add value in total variable

              System.out.printf("Sub Total :$%.2f\n",sub_Total);//print value

          break;//use break keyword

          }

          case 2://for case 2

              {

              System.out.print("Enter No of units :");//print message

              Units=obq.nextInt();//input value

              sub_Total=Units*4.50;//calculating sub_Total value

              total+=sub_Total;//add value in total variable

              System.out.printf("Sub Total :$%.2f\n",sub_Total);//print value

          break;//use break keyword

          }

          case 3://for case 3

              {

              System.out.print("Enter No of units :");//print message

              Units=obq.nextInt();//input value

              sub_Total=Units*9.98;//calculating sub_Total value

              total+=sub_Total;//add value in total variable

              System.out.printf("Sub Total :$%.2f\n",sub_Total);//print value

          break;//use break keyword

          }

          case 4://for case 4

              {

              System.out.print("Enter No of units :");//print message

              Units=obq.nextInt();//input value

              sub_Total=Units*4.49;//calculating sub_Total value

              total+=sub_Total;//add value in total variable

              System.out.printf("Sub Total :$%.2f\n",sub_Total);//print value

          break;//use break keyword

          }

        case 5://for case 5

              {

              System.out.print("Enter No of units :");//print message

              Units=obq.nextInt();//input value

              sub_Total=Units*6.87;//calculating sub_Total value

              total+=sub_Total;//add value in total variable

              System.out.printf("Sub Total :$%.2f\n",sub_Total);//print value

          break;//use break keyword

          }

       }

  System.out.print("Product: ");//print message  

  product_Num=obq.nextInt();//input value

      }

      System.out.printf("\nOvertoal Total :$%.2f\n",total);//print total value

  }

}

Output:

Product: 1

Enter No of units :1

Sub Total :$2.98

Product: 2

Enter No of units :2

Sub Total :$9.00

Product: 3

Enter No of units :3

Sub Total :$29.94

Product: 4

Enter No of units :4

Sub Total :$17.96

Product: 5

Enter No of units :5

Sub Total :$34.35

Product: -1

Overtoal Total :$94.23

Explanation:

In this program, two double variables "total,sub_Total" and two integers "Units,product_Num" is used, in the next step,  the scanner class is used for an input value, for this, a while loop is defined that uses the switch for input value and calculate  its respective value, and use the print method to print its value.