Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created with IntelliJ IDEA.
- * User: Bobby
- * Date: 11/2/13
- * Time: 7:53 AM
- A mail-order house sells five products whose retail prices are as follows. Product 1, $2.98; Product 2,
- $4.50; Product 3 $9.98; Product 4 $4.49; and Product 5 $6.87. Write an application that reads a series of
- pairs of number as followers:
- a) product number
- b) quantity sold
- Your program 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 sentinel-control (while or for
- statement) loop to determine when the program should stop looping and display the result.
- */
- import java.util.Scanner;
- public class Mailorders {
- public static void main(String[] args) {
- double products[] = {2.98, 4.5, 9.98, 4.49, 6.87}; //product prices
- int productNumber;
- int quantity;
- double totalSale = 0;
- Scanner input = new Scanner(System.in);
- System.out.println("Enter product # (Enter -1 to quit):");
- productNumber = input.nextInt();
- while (productNumber != -1) {
- System.out.println("Enter quantity #:");
- quantity = input.nextInt();
- switch (productNumber) {
- case 1:
- totalSale += quantity * products[0];
- break;
- case 2:
- totalSale += quantity * products[1];
- break;
- case 3:
- totalSale += quantity * products[2];
- break;
- case 4:
- totalSale += quantity * products[3];
- break;
- case 5:
- totalSale += quantity * products[4];
- break;
- default:
- System.out.println("Error");
- break;
- }
- System.out.println("Total sale is: " + totalSale);
- System.out.println("Enter product # (Enter -1 to quit):");
- productNumber = input.nextInt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment