Question #53394

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.

Expert's answer

Answer on question #53394 – Programming | 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.

Answer:


import java.util.Scanner;
public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // Scanner for keyboard input
        Scanner keyBoard = new Scanner(System.in);
        // total Retail
        double totalRetail = 0;
        // Use a sentinel-controlled loop to determine when the
        // program should stop looping and display the final results.
        while (true) {
            // display menu
            System.out.println("1 - Product 1, $2.98");
            System.out.println("2 - Product 2, $4.50");
            System.out.println("3 - Product 3, $9.98");
            System.out.println("4 - Product 4, $4.49");
            System.out.println("5 - Product 5, $6.87");
            System.out.println("6 - Exit program");
            System.out.print("Enter product number: ");
            int productNumber = keyBoard.nextInt();
            // exit program
            if (productNumber == 6) {
                break;
            }
            // Enter quantity sold
            System.out.print("Enter quantity sold: ");
            int quantitySold = keyBoard.nextInt();
            // use a switch statement to determine the retail price
            // for each product.
            switch (productNumber) {
                case 1:
                    totalRetail += 2.98 * quantitySold;
                    break;
                case 2:
                    totalRetail += 4.50 * quantitySold;
                    break;
                case 3:
                    totalRetail += 9.98 * quantitySold;
                    break;
                case 4:
                    totalRetail += 4.49 * quantitySold;
                    break;
                case 5:
                    totalRetail += 6.87 * quantitySold;
                    break;
            }
            // display result
            System.out.println("\nThe total retail value of all products sold: $" + totalRetail);
        }
    }
}


Final results:

```

1 - Product 1, $2.98

2 - Product 2, $4.50

3 - Product 3, $9.98

4 - Product 4, $4.49

5 - Product 5, $6.87

6 - Exit program

Enter product number: 1

Enter quantity sold: 2

The total retail value of all products sold: $5.96

1 - Product 1, $2.98

2 - Product 2, $4.50

3 - Product 3, $9.98

4 - Product 4, $4.49

5 - Product 5, $6.87

6 - Exit program

Enter product number: 3

Enter quantity sold: 4

The total retail value of all products sold: $45.88

1 - Product 1, $2.98

2 - Product 2, $4.50

3 - Product 3, $9.98

4 - Product 4, $4.49

5 - Product 5, $6.87

6 - Exit program

Enter product number: 5

Enter quantity sold: 6

```

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS