This is the problem I am suppose to solve:
A mail order house sells six different products whose retail prices are as follows:
Product 1, $3.75;
Product 2, $5.95;
Product 3, $8.75;
Product 4, $6.92;
Product 5, $8.75;
Product 6, $7.87.

Write an application program that reads a series of pairs of numbers (on daily basis) as follows:

Product number
Quantity sold for one day 

Your program should use switch structure to help determine the retail price for each product.
It should calculate and display the total retail value of all products sold last week (5-days).
Use a sentinel-controlled loop to determine when the program should stop looping and display
the final results.

This is the code I have so far:

import java.util.Scanner;

public class MailOrderHouse {
    public static void main(String[] args) {

        // Create a scanner
        Scanner input = new Scanner(System.in);

        System.out.print("What Product(number) would you like to buy?" +
                " (0 exits the program): ");
        int product = input.nextInt();

        System.out.print("How many of this product would you like to buy?" +
                " (0 exits the program): ");
        int numberOfProducts = input.nextInt();

        double productPrice = 0;
        switch (product) {
        case 1: productPrice = 3.75;
        break;
        case 2: productPrice = 5.95;
        break;
        case 3: productPrice = 8.75;
        break;
        case 4: productPrice = 6.92;
        break;
        case 5: productPrice = 8.75;
        break;
        case 6: productPrice = 7.87;
        break;
        default: productPrice = 0.00;
        break;
    }


        double totalPurchase = productPrice * numberOfProducts;
        double sum = totalPurchase;

        for(int x = 1; x <= 5; x++) {
            while (product != 0 && numberOfProducts != 0) {
                sum += totalPurchase;

                System.out.print("What is the product number?" +
                        " (0 exits the program): ");
                product = input.nextInt();

                System.out.print("How many of this product were sold?" +
                        " (0 exits the program): ");
                numberOfProducts = input.nextInt();
            }

        }   



        System.out.println("The total sold last week was: $" + sum);

    }

}

I am not sure what I'm doing wrong but the output I am getting is not correct. Please help!

the output I am getting is not correct.

Can you post the console contents from when you execute the program that shows what you entered and what the program printed out?

Can you explain what the two loops at lines 39 and 40 are supposed to do?

Edited 4 Years Ago by NormR1

This is what is printed out:
What is the product number? (0 exits the program): 5
How many of this product were sold? (0 exits the program): 5
What is the product number? (0 exits the program): 6
How many of this product were sold? (0 exits the program): 6
What is the product number? (0 exits the program): 0
How many of this product were sold? (0 exits the program): 0
The total sold last week was: $131.25

And sorry the for loop should not be there, only the while loop. The while loop is for when the user wants to leave the client they just enter 0's as inputs and the program ends.

The calculations themselves are wrong 5 * 8.75 + 6 * 7.87 = 90.97 not 131.25. Not sure where I went wrong though.

This article has been dead for over six months. Start a new discussion instead.