piexil

Program 5.17

Nov 1st, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: Bobby
  4.  * Date: 11/2/13
  5.  * Time: 7:53 AM
  6.  
  7.  A mail-order house sells five products whose retail prices are as follows. Product 1, $2.98; Product 2,
  8.  $4.50; Product 3 $9.98; Product 4 $4.49; and Product 5 $6.87. Write an application that reads a series of
  9.  pairs of number as followers:
  10.  a) product number
  11.  b) quantity sold
  12.  Your program should use a switch statement to determine the retail price for each product. It should
  13.  calculate and display the total retail value of all products sold. Use sentinel-control (while or for
  14.  statement) loop to determine when the program should stop looping and display the result.
  15.  */
  16.  
  17. import java.util.Scanner;
  18.  
  19. public class Mailorders {
  20.     public static void main(String[] args) {
  21.         double products[] = {2.98, 4.5, 9.98, 4.49, 6.87}; //product prices
  22.         int productNumber;
  23.         int quantity;
  24.         double totalSale = 0;
  25.         Scanner input = new Scanner(System.in);
  26.         System.out.println("Enter product # (Enter -1 to quit):");
  27.         productNumber = input.nextInt();
  28.         while (productNumber != -1) {
  29.             System.out.println("Enter quantity #:");
  30.             quantity = input.nextInt();
  31.             switch (productNumber) {
  32.                 case 1:
  33.                     totalSale += quantity * products[0];
  34.                     break;
  35.                 case 2:
  36.                     totalSale += quantity * products[1];
  37.                     break;
  38.                 case 3:
  39.                     totalSale += quantity * products[2];
  40.                     break;
  41.                 case 4:
  42.                     totalSale += quantity * products[3];
  43.                     break;
  44.                 case 5:
  45.                     totalSale += quantity * products[4];
  46.                     break;
  47.                 default:
  48.                     System.out.println("Error");
  49.                     break;
  50.             }
  51.             System.out.println("Total sale is: " + totalSale);
  52.             System.out.println("Enter product # (Enter -1 to quit):");
  53.             productNumber = input.nextInt();
  54.         }
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment