lundi 8 avril 2019

Making a burger menu with a Switch statement and Do While loop for an invalid entry

Im trying to make a food menu. Im using a switch statement for the menu and i need to implement a loop for when a wrong entry is input by the user. Also when i run the program it does not let me input an aswer forthe "Would you like cheese?" question.

I have tries a Do-While loop but it just repeats the default case infinately.

import java.util.Scanner;

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

Scanner keyBoard = new Scanner(System.in);
int menu;
String cheese;
boolean wantCheese = false;
double price = 0;




 System.out.println("=============Whataburger==========");
 System.out.println("(1) Whataburger $2.99");
 System.out.println("(2) Double Meat Whataburger $3.99");
 System.out.println("(3) Triple Meat Whataburger $4.99");
 System.out.println("(4) Jalapeño and Cheese Sandwich $5.50");
 System.out.println("(5) Bacon and Cheese Sandwich $6.99");
 System.out.println("(6) Salad $4.99");
 System.out.println("=================================");
 System.out.println(" Enter the menu item that you want: ");
 menu = keyBoard.nextInt();


 do{
 switch(menu){

 case 1: 

 price = 2.99;

 System.out.println("You have chosen a Whataburger for $" + price);

 break;

 case 2: 

 price = 3.99;

 System.out.println("You have chosen a Double Meat Whataburger for $" + 
                    price);


 break; 

 case 3: 

 price = 4.99;

 System.out.println("You have chosen a Triple Meat Whataburger for $" + 
                       price);

 break;

 case 4: 

 price = 5.50;

 System.out.println("You have chosen a Jalapeño and Cheese Sandwhich for 
                   `enter code here`$" + price);

 break; 

 case 5: 

 price = 6.99;

 System.out.println("You have chosen a Bacon and Cheese Sandwhich for $" 
 + price);

 break; 

 case 6: 

 price = 4.99;

 System.out.println("You have chosen a Salad for $" + price);
 break;

 default:
  System.out.println("menu option does not exist, try again");
  }

  }while(menu >6);

  System.out.println("Would you like cheese with that?");
  cheese = keyBoard.nextLine();
  cheese = cheese.toLowerCase();  
  if(cheese.equals("yes"))
   wantCheese = true;

  if (wantCheese)
  {
   price += 0.50;
   price = price +(price * .0875);
   }
   else
   {
   price = price + (price* .0875);
   }
   System.out.println("Your total is: " + price);





   }
    }

Aucun commentaire:

Enregistrer un commentaire