Write a Java application that prompts the user for pairs of inputs of a product
ID: 638298 • Letter: W
Question
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 calculate and display the total retail value of all products sold, after all 5 pairs of inputs are completed. You must also display the total after each new pair of input values is entered.
Explanation / Answer
The Simplest Solution is :
/* package whatever; */
import java.util.*;
import java.lang.*;
import java.io.*;
class Calculator
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
System.out.println("Enter product number :");
int product = in.nextInt();
System.out.println("Enter product quantity :");
int quantity = in.nextInt();
double total = 0;
for(int i = 0; i < 5; i++){
switch(product){
case 1:
total += quantity * 2.98;
break;
case 2:
total += quantity * 4.50;
break;
case 3:
total += quantity * 9.98;
break;
case 4:
total += quantity * 4.49;
break;
case 5:
total += quantity * 6.87;
break;
default:
break;
}
System.out.println("Total : "+total);
System.out.println("Enter product number :");
product = in.nextInt();
if(product < 0 || product > 5)
break;
System.out.println("Enter product quantity :");
quantity = in.nextInt();
if(quantity < -1 || quantity > product)
break;
}
System.out.println("Final Total : "+total);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.