Write a Java application that prompts the user for pairs of
Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows:
Product 1 1 unit (cost is $2.98 per unit)
Product 2 2 units (cost is $4.50 per unit)
Product 3 3 units (cost is $9.98 per unit)
Product 4 4 units (cost is $4.49 per unit)
Product 5 5 units (cost is $6.87 per unit)
Your application must show the user’s input of each pair of product number and quantity, the calculated line cost for that product, and the cumulative order cost so far. Then, after the sentinel value is input your program must display the total retail cost for the full order.
For full possible credit you must show all 11 user inputs (5 product number and quantity pairs and the sentinel value) and all the corresponding program outputs, not just a subset of the inputs and outputs. Showing just a subset of all required inputs and outputs will result in lost points.
Solution
Hi friend, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] productQuantity = new int[5];
double[] unitPrice = {2.98, 4.50, 9.98, 4.49, 6.87};
double[] totalPrice = new double[5];
double totalComulativePrice = 0;
int prodt;
int quantity;
while(true){
System.out.print(\"Enter product number: \");
prodt = sc.nextInt();
if(prodt>5 || prodt < 1) // if product number is out of range, then stop
break;
System.out.print(\"Enter quantity sold: \");
quantity = sc.nextInt();
if(quantity < 0) // if quantity entered is less than 0 then stop
break;
switch(prodt){
case 1:
productQuantity[0] += quantity; // adding quantity sold
totalPrice[0] += quantity*unitPrice[0];
break;
case 2:
productQuantity[1] += quantity; // adding quantity sold
totalPrice[1] += quantity*unitPrice[1];
break;
case 3:
productQuantity[2] += quantity; // adding quantity sold
totalPrice[2] += quantity*unitPrice[2];
break;
case 4:
productQuantity[3] += quantity; // adding quantity sold
totalPrice[3] += quantity*unitPrice[3];
break;
case 5:
productQuantity[4] += quantity; // adding quantity sold
totalPrice[4] += quantity*unitPrice[4];
break;
}
totalComulativePrice += quantity*unitPrice[prodt-1]; // adding current product price in total
}
System.out.println(\"Product number 1 is sold \"+productQuantity[0]+\" number of quantity for $\"+totalPrice[0]);
System.out.println(\"Product number 2 is sold \"+productQuantity[1]+\" number of quantity for $\"+totalPrice[1]);
System.out.println(\"Product number 3 is sold \"+productQuantity[2]+\" number of quantity for $\"+totalPrice[2]);
System.out.println(\"Product number 4 is sold \"+productQuantity[3]+\" number of quantity for $\"+totalPrice[3]);
System.out.println(\"Product number 5 is sold \"+productQuantity[4]+\" number of quantity for $\"+totalPrice[4]);
System.out.println(\"Entered sentinal value is : \"+prodt);
System.out.println(\"Total commulative total is $\"+totalComulativePrice);
}
}
/*
Sample Output:
Enter product number: 1
Enter quantity sold: 1
Enter product number: 2
Enter quantity sold: 2
Enter product number: 3
Enter quantity sold: 3
Enter product number: 4
Enter quantity sold: 4
Enter product number: 5
Enter quantity sold: 5
Enter product number: -1
Product number 1 is sold 1 number of quantity for $2.98
Product number 2 is sold 2 number of quantity for $9.0
Product number 3 is sold 3 number of quantity for $29.94
Product number 4 is sold 4 number of quantity for $17.96
Product number 5 is sold 5 number of quantity for $34.35
Entered sentinal value is : -1
Total commulative total is $94.23
*/



