vendredi 11 mai 2018

Do while is not working correctly

I am making a grocery store java program which lists out options of items and adds them to the cart according to the users input. It works fine for the first option however after that the programs is not functioning correctly. I cannot seem to find the bug anywhere!

Ideally what I want it to ask is:

What would you like to add to your cart? enter the number

(have user enter the number)

how many?

(have user enter number)

Would you like to add anything else to your cart? (y/n)

(enter y or n)

Picture of output of broken do while

Ive also provided the code:

do
               {

        System.out.println("What would you like to add to your cart? Enter its corresponding number");
                choose = userin.nextInt();

                 if(choose > 14) {
                              System.out.println("please choose a valid option!"); 
                              keepGoing = true;
                 }

                 else if(choose == 1) {
                System.out.println("how many?");    
                int quantity;
                quantity = userin.nextInt();

                cart1.addToCart(FoodItems.QUINOA, 7.00, quantity);

                 }


                 System.out.print ("\nWould you like to add anything else to your cart (y/n)? ");
                 keepShopping = userin.next();


                 if(choose ==2) {
                 System.out.println("What would you like to add to your cart? Enter its corresponding number");
                 choose = userin.nextInt(); 


                        System.out.println("how many?");    
                        int quantity;
                        quantity = userin.nextInt();

                         cart1.addToCart(FoodItems.ORGEGG, 3.45, quantity);
                  }
                         System.out.print ("\nWould you like to add anything else to your cart (y/n)? ");
                         keepShopping = userin.next();
                        // }

                  System.out.println("What would you like to add to your cart? Enter its corresponding number");
                  choose = userin.nextInt();

                  if(choose == 3) {
                        System.out.println("how many?");    
                        int quantity;
                        quantity = userin.nextInt();

                        cart1.addToCart(FoodItems.CCNTW, 2.99, quantity);
                        System.out.print ("\nWould you like to add anything else to your cart (y/n)? ");
                        keepShopping = userin.next();

                  }

                  }
              while (keepShopping.equals("y"));

              if(keepShopping.equals("n")) {

                    System.out.println("\n");
                    Register.talking();


                }
              System.out.println(cart1);
              keepGoing = false;
        } 

Aucun commentaire:

Enregistrer un commentaire