An online retailer sells five products whose retail prices are as follows:
Product 1, $2.98
Product 2, $4.50
Product 3, $3.98
Product 4, $4.49
Product 5, $6.87
Write an application that reads (from the keyboard) a series of pairs of numbers as follows:
product number
quantity sold
Run your program from a simple menu with the following options
Enter products sold
Display total retail value
Exit
Your program should
allow for entry of multiple products without returning to the main menu (use a sentinel-controlled loop)
use a switch statement to determine the retail price for each product
calculate and display the total retail value of all products sold
import java.util.Scanner;
public class Store {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int choice;
double product1 = 0, product2 = 0, product3 = 0, product4 = 0, product5 = 0, total = 0;
do {
System.out.println("Enter product # (0 to quit): ");
String tempChoice = in.nextLine();
choice = Integer.parseInt(tempChoice);
switch (choice) {
case 1:
System.out.println("Enter product quantity: ");
String tempQuantity = in.nextLine();
product1 += Integer.parseInt(tempQuantity) * 2.98;
total += product1;
break;
case 2:
System.out.println("Enter product quantity: ");
tempQuantity = in.nextLine();
product2 += Integer.parseInt(tempQuantity) * 4.50;
total += product2;
break;
case 3:
System.out.println("Enter product quantity: ");
tempQuantity = in.nextLine();
product3 += Integer.parseInt(tempQuantity) * 3.98;
total += product3;
break;
case 4:
System.out.println("Enter product quantity: ");
tempQuantity = in.nextLine();
product4 += Integer.parseInt(tempQuantity) * 4.49;
total += product4;
break;
case 5:
System.out.println("Enter product quantity: ");
tempQuantity = in.nextLine();
product5 += Integer.parseInt(tempQuantity) * 6.87;
total += product5;
break;
}
} while (choice != 0);
System.out.println("Sum of product 1 sold: $" + product1);
System.out.println("Sum of product 2 sold: $" + product2);
System.out.println("Sum of product 3 sold: $" + product3);
System.out.println("Sum of product 4 sold: $" + product4);
System.out.println("Sum of product 5 sold: $" + product5);
System.out.println("Total sold : $" + total);
}
}
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!
Learn more about our help with Assignments:
JavaJSPJSF