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.

Dani AI

Generated

This is a compact, working Java example that matches the requirements posted by : a sentinel-controlled loop that reads pairs (product number, quantity), uses a switch to select the product price, and prints the total retail value. As recommended, posting attempted code and exact compiler/runtime messages makes debugging much easier; ' point about showing effort is noted. The sample below includes simple input validation and formatted output.

import java.util.Scanner;

public class RetailCalculator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double total = 0.0;

        System.out.println("Enter product number (1-5) and quantity. Enter 0 for product to finish.");
        while (true) {
            if (!sc.hasNextInt()) { sc.next(); continue; }
            int product = sc.nextInt();
            if (product == 0) break;

            if (!sc.hasNextInt()) { System.out.println("Quantity missing or invalid."); continue; }
            int qty = sc.nextInt();
            if (qty < 0) { System.out.println("Negative quantity ignored."); continue; }

            double price;
            switch (product) {
                case 1: price = 2.98; break;
                case 2: price = 4.50; break;
                case 3: price = 9.98; break;
                case 4: price = 4.49; break;
                case 5: price = 6.87; break;
                default:
                    System.out.println("Invalid product number: " + product);
                    continue;
            }

            total += price * qty;
        }

        System.out.printf("Total retail value of all products sold: $%.2f%n", total);
        sc.close();
    }
}

Notes and troubleshooting: the sentinel chosen here is product 0. hasNextInt() avoids InputMismatchException on bad tokens. Common compile/runtime issues include missing import java.util.Scanner;, class name/file name mismatch when using public class, and forgetting to close the scanner (not fatal but good practice). For production or precise currency math, consider BigDecimal instead of double. If further help is needed, sharing the exact code attempted and the complete compiler/runtime error text (plus JDK version) will allow targeted fixes, per 's suggestion.

Recommended Answers

All 2 Replies

Like I said in the other thread, post your code and the errors being reported by the compiler.

and show some effort at least

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.