Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CalcSales.java \"An online retailer sells five products whose retail prices are

ID: 662476 • Letter: C

Question

CalcSales.java

"An online retailer sells five products whose retail prices are as follows: Product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49 and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:

a) product number

b) quantity sold

Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results."

Explanation / Answer

/*An online retailer sells five products whose retail prices are as follows:
* Product 1, $2.98;
* product 2, $4.50;
* product 3, $9.98;
* product 4, $4.49 and
* product 5, $6.87.
* java program that prompts user to enter product number and quantity
* and stops the loop if the sentinal value 99 is entered otherwiser read
* the input from user and print the total cost of products sold */

//CalcSales.java
import java.util.Scanner;
public class CalcSales
{
   public static void main(String[] args)
   {
       //Scanner class object to read input
       Scanner scanner=new Scanner(System.in);

       //cost of five products
       final double PRODUCT1=2.98;
       final double PRODUCT2=4.50;
       final double PRODUCT3=9.98;
       final double PRODUCT4=4.49;
       final double PRODUCT5=6.87;

       //sentinal value to stop reading product numbers and quantity
       int sentinal=99;

       //integer variables to read input product number and quantity
       int productNumber;
       int quantity;

       //to store total cost of items sold
       double totalCost=0;

       System.out.println("***PRODUCTS***");
       System.out.println("Product 1, $2.98");
       System.out.println("Product 2, $4.50");
       System.out.println("Product 3, $9.98");
       System.out.println("Product 4, $4.49");
       System.out.println("Product 5, $6.87");

       System.out.println("Enter product number and quantity number or " +
               "99 to stop reading input");
      
       //prompt for input values
       productNumber=scanner.nextInt();      
       quantity=scanner.nextInt();

       //check if the product number and quantity is not sentinal value
       while(productNumber!=sentinal || quantity!=sentinal)
       {
           //use switch case to select the appropriate the prodcut number
           //and calcualte the total cost
           switch (productNumber)
           {
           case 1:
               totalCost+=PRODUCT1*quantity;
               break;
           case 2:
               totalCost+=PRODUCT2*quantity;
               break;
           case 3:
               totalCost+=PRODUCT3*quantity;
               break;
           case 4:
               totalCost+=PRODUCT4*quantity;
               break;
           case 5:
               totalCost+=PRODUCT5*quantity;
               break;              
           //if invalid product number is entered then print a message
           default:
               System.out.println("Invalid product number");
               break;
           }

           System.out.println("Enter product number and quantity number or " +
                   "99 to stop reading input");
           productNumber=scanner.nextInt();  

           //break the while loop if the product number is sentinal
           if(productNumber==sentinal)
               break;

          
           quantity=scanner.nextInt();
           //break the while loop if the quantity is sentinal
           if(quantity==sentinal)
               break;
       }

       //print total cost of retails products sold
       System.out.printf("Retail price of all products sold $: %.2f ",totalCost);
   }
}//end of class CalcSales


------------------------------------------------------------------------------------------------------------------------------------------

sample output:

***PRODUCTS***
Product 1, $2.98
Product 2, $4.50
Product 3, $9.98
Product 4, $4.49
Product 5, $6.87
Enter product number and quantity number or
99 to stop reading input
1 5
Enter product number and quantity number or 99 to stop reading input
1 5
Enter product number and quantity number or 99 to stop reading input
2 5
Enter product number and quantity number or 99 to stop reading input
3 5
Enter product number and quantity number or 99 to stop reading input
99
Retail price of all products sold $: 102.20

Hope this helps you