dimanche 4 septembre 2016

If-Else require variable initialization

In order to make the first If-Else statement work, I had to initialize the "quantity" and "productNo" variables with "= 0". This was not specified in the assignment. Is there another way to make the If-Else work without initializing the variables?

The first If statement allows the user to enter the sentinel"-1" at the very first prompt of the program to exit if desired (which is part of the prompt text). The second If-Else statement at the end of the program allows the user to use the sentinel value at any point to exit.

This is a Java Application that calculates the total order retail amounts for a selection of products and quantities purchased. There are 5 products with 5 constant costs per unit. The user enters a product # and then a quantity. The output is the product #, quantity ordered, line cost and order total. It has a sentinel value of "-1" which when entered allows the user to quit the program.

import java.util.Scanner;
public class Program3
{   
 public static void main(String[] args) 
 {

  final double COST_PROD1 = 2.98, COST_PROD2 = 4.50, 
  COST_PROD3 = 9.98, COST_PROD4 = 4.49, COST_PROD5 = 6.87;
  int productNo = 0;
  int quantity = 0;
  double lineAmount = 0;    
  double orderAmount = 0;

   Scanner inputDevice = new Scanner(System.in);


   System.out.print("Enter Product No. (1-5) or -1 to Quit: >> ");
   productNo = inputDevice.nextInt();


    if (productNo == -1)
        {
    System.out.println("You have entered -1. You have exited the program."); 
    System.out.println("Order grand total = $0.00");    
        }

     else 
     {
       System.out.print("Please enter unit quantity >> ");
       quantity = inputDevice.nextInt();
      }  

    while(productNo != -1)
        {    
     switch(productNo)
        {
     case 1:
     lineAmount = quantity * COST_PROD1;
     orderAmount += lineAmount;
       System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
         + "Total Order");
       System.out.println("   " + productNo + "          " + quantity + 
         "          " + lineAmount + "         " + orderAmount); 
       break;

      case 2:
        lineAmount = quantity * COST_PROD2;
        orderAmount += lineAmount;
         System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
            + "Total Order");
         System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount); 
           break;

       case 3:
         lineAmount = quantity * COST_PROD3;
         orderAmount += lineAmount;
         System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
           + "Total Order");
         System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount);  
        break;

        case 4:
        lineAmount = quantity * COST_PROD4;
        orderAmount += lineAmount;
          System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
            + "Total Order");
          System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount); 
          break;

          case 5:
          lineAmount = quantity * COST_PROD5;
          orderAmount += lineAmount;
          System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
             + "Total Order");
           System.out.println("   " + productNo + "          " + quantity + 
             "          " + lineAmount + "         " + orderAmount); ; 
           break;

          default:
            System.out.println("You have entered an invalid product number.  
            Try again.");
             break;
        }//end switch

        System.out.print("Enter Product No. (1-5) or -1 to Quit: >> ");
        productNo = inputDevice.nextInt();
            if (productNo == -1)
       {
        System.out.println("You have entered -1. You have exited the 
              program."); 
        System.out.println("Order grand total..." + orderAmount);  
        }
        else 
        {
       System.out.print("Please enter unit quantity >> ");
       quantity = inputDevice.nextInt();
          }
     } // end while     
   } // end main
  } // end class

Aucun commentaire:

Enregistrer un commentaire