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!