Natadad
Natadad

Reputation: 29

Java Menu Using Do while loop keeps repeating

This code should display a menu and afterwards it should give the user the possibility to choose from the menu. The user can choose 3 items along with the quantity and afterwards the total price is displayed and the program stops. This is what the program should look like when it runs:

This is the menu for Tal'Qroq Restourant:


This is the menu of pizzas

A.Margherita ..... $5.50
B.Capricosa ..... $6.50
C.Funghi ..... $7.00
D.Vegeterian..... $7.00
E.Tropical..... $8.00
F.Meat ..... $8.00
G.Salami..... $8.00
H.Maltija..... $8.00
I.Calzona..... $8.50
J.Tal'Qroq special..... $8.00 

Enter your pizza order according to the menu letters:

After the user inputs the pizza the quantity is asked and that works fine. The user must be asked 3 times to enter the pizza and quantity but instead the loop wont stop and keep asking and asking infinitely and this is my problem! The following is the code:

public class Menu{
    public static void main (String[]args){

    float total = 0;
    char cas = 0;
    int quant = 0;
    int count = 0;

    System.out.println("This is the menu for Tal'Qroq Restourant:");
    System.out.println("\n");
    System.out.println("This is the menu of pizzas");
    System.out.println("\n");
    System.out.println("A.Margherita ..... $5.50");
    System.out.println("\n");
    System.out.println("B.Capricosa ..... $6.50");
    System.out.println("\n");
    System.out.println("C.Funghi ..... $7.00");
    System.out.println("\n");
    System.out.println("D.Vegeterian..... $7.00");
    System.out.println("\n");
    System.out.println("E.Tropical..... $8.00");
    System.out.println("\n");
    System.out.println("F.Meat ..... $8.00");
    System.out.println("\n");
    System.out.println("G.Salami..... $8.00");
    System.out.println("\n");
    System.out.println("H.Maltija..... $8.00");
    System.out.println("\n");
    System.out.println("I.Calzona..... $8.50");
    System.out.println("\n");
    System.out.println("J.Tal'Qroq special..... $8.00");
    System.out.println("\n");

    float a = 5.50f;
    float b = 6.50f;
    float c = 7.00f;
    float d = 7.00f;
    float e = 8.00f;
    float f = 8.00f;
    float g = 8.00f;
    float h = 8.00f;
    float i = 8.00f;
    float j = 8.00f;


    do{
        System.out.print("Enter your pizza order according to the menu letters: ");
        cas = Keyboard.readChar(); 

        System.out.print("Enter the ammount of pizza you want: ");
        quant = Keyboard.readInt();

         if(cas == 'a' || cas == 'A'){
        System.out.println("Total for " + quant + " Margherita is :" + (a*quant));
        System.out.println("\n");
        total = total + (a*quant);
        count = count++;

        }else if(cas == 'b' || cas == 'B'){
            System.out.println("Total for " + quant + " Capricosa is :" + (b*quant));
            System.out.println("\n");
            total = total + (b*quant);
            count = count++;

        }else if(cas == 'c' || cas == 'C'){
            System.out.println("Total for " + quant + " Funghi is :" + (c*quant));
            System.out.println("\n");
            total = total + (c*quant);  
            count = count++;

        }else if(cas == 'd' || cas == 'D'){
            System.out.println("Total for " + quant + " Vegeterian is :" + (d*quant));
            System.out.println("\n");
            total = total + (d*quant);
            count = count++;

        }else if(cas == 'e' || cas == 'E'){
            System.out.println("Total for " + quant +  " Tropical is :" + (e*quant));
            System.out.println("\n");
            total = total + (e*quant);
            count = count++;

        }else if(cas == 'f' || cas == 'F'){
            System.out.println("Total for " + quant + " Meat is :" + (f*quant));
            System.out.println("\n");
            total = total + (f*quant);
            count = count++;

        }else if(cas == 'g' || cas == 'G'){
            System.out.println("Total for " + quant + " Salami is :" + (g*quant));
            System.out.println("\n");
            total = total + (g*quant);
            count = count++;

        }else if(cas == 'h' || cas == 'H'){
            System.out.println("Total for " + quant + " Calzona is :" + (h*quant));
            System.out.println("\n");
            total = total + (h*quant);
            count = count++;

        }else if(cas == 'i' || cas == 'I'){
           System.out.println("Total for " + quant + " Maltija is :" + (i*quant));
           System.out.println("\n");
           total = total + (i*quant);
           count = count++;

        }else if(cas == 'j' || cas == 'J'){
           System.out.println("Total for " + quant + " Tal'Qroq special is :" + (j*quant));
           System.out.println("\n");
           total = total + (j*quant);
           count = count++;
        }else{
        System.out.println("Your selection isn't avaliable in our Menu!");
        System.out.println("\n");
    }    
    } while (count <= 3);
    System.out.println("Your total is €" + total);
}

}

Any answers or help is highly appreciated :).

Upvotes: 2

Views: 785

Answers (4)

Natadad
Natadad

Reputation: 29

Thank you for your early responses :).The program worked fine. I can't believe a made such a stupid mistake count ++ instead of ++ count.I also had the change int count to 1 instead of 0.

Upvotes: 0

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

Your statement count = count++ looks wrong. Use just count++ or count = count + 1.

Using count = count++ creates byte code somewhat like this:

 temp = count
 count = count + 1
 count = temp

So in effect the count is not getting incremented.

Using count = ++count should work, but logically looks wrong:

count = count + 1
count = count

Best not try the funny characteristic of compilers.

Upvotes: 3

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

You have several things that you could improve in your program. First, let's get to the bug, you are saying count = count ++ but by doing this, the compiler will start by incrementing the value of count, and then revert count to its old value, since count++ will be returning not the incremented value. To simplify, this statement does nothing, and do not increment the value of count.

When you want to increment, use the postfix operator: count++;

By analysing your program, you can notice a lot of duplicate code that could be reduced!

For example, when comparing the inputs, you can use .equals(), this way you don't have to split the case for upper and lowercase:

(cas == 'f' || cas == 'F')

is equivalent to

cas.equals('f')

Which you can repeat for the rest of the cases.

Inside each if statement, you are doing the same system.out.println. For simplicity you can declare a variable for print, like

String typeofPizza;

And then inside each if

typeofPizza = "Margherita";

This way, you can remove the print to the end of the ifclauses:

System.out.println("Total for " + quant + typeofPizza + "is :" + total);

Upvotes: 0

eldrior
eldrior

Reputation: 75

int i = 1;
i = i++;
System.out.prinln(i); //-> 1

You just have to write "i++;" and then you are good.

And btw. Never Ever save money in a float or a double ;)

Edit:

int i = 1;
i = ++i;

That would be possible because you are increasing i before you assign the new value.

Upvotes: 0

Related Questions