Nicholas N Peck Nicholas N Peck - 1 month ago 7
Java Question

I am trying to finish this code for this menu and cant figure out why it won't loop correctly?

I have this code that I am doing and cant for the life of me figure out why it isn't looping correctly and adding the amount of the products together it seems like it only reads the first statement in the while loop and thats all the data it pulls from. I need it to also not to keep looping back to the first while statement but to continue on with the same phrase "Please enter another item from the menu above: " any help is appreciated, Thanks guys.

import java.util.Scanner;
public static void main(String[] args) { //Declare Variables

Scanner input = new Scanner(System.in);
int nProduct = 0; //Stores the value entered by the user
int nPrice = 0; //Stores sum of values entered
int nCount = 0;
int nSum = 0;
double dTax = 0.0;
double dTotal = 0.0;

final int SENTINEL = 10; //Used to end loop
final double TAX = .065;


System.out.print("Please enter the your name: ");
String sName = input.nextLine( );

System.out.println("");

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");

while (nProduct != SENTINEL) {
nSum = nPrice + nSum;
nCount++;
System.out.print("Please enter item from the menu above: ");
nProduct = input.nextInt();

if (nProduct == 1) {
nPrice += 249;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 2) {
nPrice += 39;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 3 ) {
nPrice += 1149;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 4 ) {
nPrice += 349;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 5 ) {
nPrice += 49;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 6 ) {
nPrice += 119;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 7 ) {
nPrice += 899;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if (nProduct == 8 ) {
nPrice += 299;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}

else if(nProduct == 9 ) {
nPrice += 399;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
}

dTax = (nPrice * TAX);
dTotal = dTax + nPrice;

System.out.println("");
System.out.println("Thank you for ordering with Best Purchase,"+sName);
System.out.println("Total Items Ordered: " + nCount);
System.out.println("Price of items ordered: $" + nSum);
System.out.println("Sales Tax: $" + dTax);
System.out.println("Total amount due: $" + dTotal);
}

Answer

I agree with Frank...but it looks like nSum is not going to come out correctly here.

Instead of...

nPrice += xxx;

in each statement...you should simply put

nSum += xxx;

and stop evaluating sum again at the top. As you have it now, you are constantly increasing the value of nPrice. Another option is to simply set nPrice in each if statement, and evaluate nSum at the top or bottom of the loop.

nPrice = xxx;

You are also getting an extra count because you are processing nCount++ BEFORE you actually read the item. Meaning if you type in "10" to quit, you already added an item.

You could try something like this. It's messy and took my 5 minutes, and is probably not the best way but it should work:

System.out.print("Please enter item from the menu above: ");
nProduct = input.nextInt()

while (nProduct != SENTINEL) {
    nProduct = input.nextInt()

    if (nProduct == 1) {
        nSum += 249;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 2) {
        nSum += 39;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 3 ) {
        nSum += 1149;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 4 ) {
        nSum += 349;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 5 ) {
        nSum += 49;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 6 ) {
        nSum += 119;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 7 ) {
        nSum += 899;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
    else if (nProduct == 8 ) {
        nSum += 299;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    } 
    else if(nProduct == 9 ) {
        nSum += 399;
        System.out.print("Please enter another item from the menu above:  ");
        nCount++;
    }
}