user3287679
user3287679

Reputation: 29

How to add sum multiple statements in a while loop in Java

I'm a beginner when it comes to java. I have a simple java programming assignment. Here is the bases of the program:

"Best Purchase is accepting online orders for electronic devices and accessories. They have developed a program that allows the user to select the device(s) to purchase from a menu of items. Write a program that allows the user (customer) to select items to purchase, calculates the price of the items ordered, and displays a receipt of this information to the customer (assume the sales tax rate is 6.5%)"

Your program should prompt the user (customer) to enter the following information:

The program then outputs the following information:

Here is my issue how to i add the values inside my nested if depending on what numbers the user select?

Ex: The the user selects 1,2,3,10(To exit Loop)

Here is my code:

import java.util.Scanner;
public class OnlinePurchase {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Declare Variables
        Scanner input = new Scanner(System.in);
        String sCustomerName ="";   //Stores the Customer's Name
        int nSelection = 0;         //Stores the value entered by the user
        int nSum = 0;               //Stores sum of values entered
        int nCount = 0;             //Number of values entered
        int nPrice = 0;

        //Declare Constants
        final int SENTINEL = 10; //Used to end loop

        //Promt User to enter the their name.
        System.out.print("Please enter the your name: ");
        //Read Customer Name input from user
        sCustomerName = input.nextLine( );    

        //Print to Blank Line for spacing
        System.out.println("\n");

        //Print Purchase Menu
        System.out.println("BEST PURCHASE PRODUCTS:");
        System.out.println("1. Smartphone..........$249");
        System.out.println("2. Smartphone Case.... $39");
        System.out.println("3. PC Laptop...........$1149");
        System.out.println("4. Tablet..............$349");
        System.out.println("5. Tablet Case.........$49");
        System.out.println("6. eReader.............$119");
        System.out.println("7. PC Desktop..........$889");
        System.out.println("8. LED Monitor.........$299");
        System.out.println("9. Laser Printer.......$399");
        System.out.println("10.Complete my order");

        //Print to Blank Line for spacing
        System.out.println("\n");

        while (nSelection != SENTINEL) {

            //Calculate sum
            nSum = nPrice + nSum;

            //Increment counter
            nCount++; //or nCount = nCount + 1;

            //Promt User to enter the an item from the menu.
            System.out.print("Please enter item from the menu above: ");
            //Read input from user for selected purchase
            nSelection = input.nextInt();

            if (nSelection == 1) {

                nPrice = 249;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 2 ) {

                nPrice = 39;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 3 ) {

                nPrice = 1149; 

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 4 ) {

                  nPrice = 349;

                  //Promt User to enter the an item from the menu.
                  System.out.print("Please enter another item from the menu above: ");
                  //Read input from user for selected purchase
                  nSelection = input.nextInt();
            }
            else if (nSelection == 5 ) {

                nPrice = 49;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 6 ) {

                nPrice = 119;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 7 ) {

                nPrice = 899;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
            else if (nSelection == 8 ) {

                nPrice = 299;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }  
            else if (nSelection == 9 ) {

                nPrice = 399;

                //Promt User to enter the an item from the menu.
                System.out.print("Please enter another item from the menu above: ");
                //Read input from user for selected purchase
                nSelection = input.nextInt();
            }
        }//end while user did not enter 10

        //Print blank line
        System.out.println();

        //Print Thank You message
        System.out.println("Thank you for ordering with Best Purchase,"+sCustomerName);
        //Print number of integers entered
        System.out.println("Total Items Ordered: " + nCount);
         //Print number of integers entered
        System.out.println("Total: $" + nSum);  
    }//end main class
}//end public class

Upvotes: 1

Views: 2124

Answers (1)

pczeus
pczeus

Reputation: 7867

Honestly, your code is over complicated and there is a lot of duplication. You should take advantage of methods for reuse and maybe a switch statement to handle choosing the right price. Also, you should not place all of your program logic in the main method - take advantage of smaller, more concise methods and minimize what you place in main().

Your while loop should simply accept the next line of input, call a processItem() method and terminate if the number entered is your Sentinel value (10).

Try this revision of your code:

import java.util.Scanner;

public class OnlinePurchase {

    String customerName;
    int nSum = 0;               //Stores sum of values entered
    int nCount = 0;             //Number of values entered

    public void printPurchaseMenu(){
        //Print Purchase Menu
        System.out.println("\nBEST PURCHASE PRODUCTS:");
        System.out.println("1. Smartphone..........$249");
        System.out.println("2. Smartphone Case.... $39");
        System.out.println("3. PC Laptop...........$1149");
        System.out.println("4. Tablet..............$349");
        System.out.println("5. Tablet Case.........$49");
        System.out.println("6. eReader.............$119");
        System.out.println("7. PC Desktop..........$889");
        System.out.println("8. LED Monitor.........$299");
        System.out.println("9. Laser Printer.......$399");
        System.out.println("10.Complete my order");
    }

    public void setCustomerName(String name){
        customerName = name;
    }

    public void processInput(int inputValue){

        switch(inputValue){
          case 1: addItem(249); break;
          case 2: addItem(39); break;
          case 3: addItem(1149); break;
          case 4: addItem(349); break;
          case 5: addItem(49); break;
          case 6: addItem(119); break;
          case 7: addItem(889); break;
          case 8: addItem(299); break;
          case 9: addItem(399); break;
          case 10: printResults(); break;
          default: System.out.println("You entered an invalid value.");
        }
    }

    public void addItem(int price){
        nSum += price;
        nCount++;
    }

    public void printResults(){
        //Print Thank You message
        System.out.println("\nThank you for ordering with Best Purchase," + customerName);
        //Print number of integers entered
        System.out.println("Total Items Ordered: " + nCount);
         //Print number of integers entered
        System.out.println("Total: $" + nSum);
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        OnlinePurchase onlinePurchase = new OnlinePurchase();

        //Declare Variables
        Scanner input = new Scanner(System.in);
        int nSelection = 0;  //Stores the value entered by the user

        //Declare Constants
        final int SENTINEL = 10; //Used to end loop

        //Prompt User to enter the their name.
        System.out.print("Please enter the your name: ");

        //Read Customer Name input from user
        onlinePurchase.setCustomerName(input.nextLine( ));

        //Print Purchase Menu
        onlinePurchase.printPurchaseMenu();

        while (nSelection != SENTINEL){
            System.out.print("Please enter item from the menu above: ");
            nSelection = input.nextInt();

            onlinePurchase.processInput(nSelection);
            System.out.println("\n");
      }//end while user did not enter 10
  }//end main method
}//end public class

Upvotes: 2

Related Questions