A website sells three products whose retail prices are as follows: product 1, $2
ID: 3584069 • Letter: A
Question
A website sells three products whose retail prices are as follows: product 1, $2.98; product 2 $4.50; and product 3, $9.98. Write an application that reads a series of pairs of numbers as follows: a) product number b) quantity sold Your application should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the application should stop looping display the final results.Explanation / Answer
package sales; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner( System.in ); int prodNumber, quantity, choice = 1; double total = 0; while(choice != 2){ System.out.print("Product number: "); prodNumber = in.nextInt(); System.out.print("Quantity sold: "); quantity = in.nextInt(); switch(prodNumber){ case 1: total += 2.98*quantity; break; case 2: total += 4.5*quantity; break; case 3: total += 9.98*quantity; break; case 4: total += 4.49*quantity; break; case 5: total += 6.87*quantity; break; default: System.out.println("error: wrong number of product!"); break; } System.out.print("To continue enter 1, To show total enter 2: "); choice = in.nextInt(); } System.out.print(" TOTAL: "); System.out.println(total); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.