Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BELOW IS A JAVA CODE) import java.util.Scanner; public class Assignment2Q2 { public static void main(String[] args) { System. out .println(+++++++++++++++++++++++++++++++); System. out .println(Welcome to Online

BELOW IS A JAVA CODE)

import java.util.Scanner;

public class Assignment2Q2

{

public static void main(String[] args)

{

System.out.println("+++++++++++++++++++++++++++++++");

System.out.println("Welcome to Online Order Program!");

System.out.println("+++++++++++++++++++++++++++++++");

String order="no";

int choice;

String type ="no";

String meat;

String food = "";

double withoutMeat=7.5;

double withBeef=25.5;

double withPork=17.5;

double totalCost=0;

double cost=0;

double totalAmount=0;

double delivery=0;

double deliveryCharge=5;

double tips=0;

Scanner a=new Scanner(System.in);

System.out.println(" 1. Hamburger");

System.out.println(" 2. Pizza");

System.out.println(" 3. Noodle");

System.out.println(" 4. Salad");

System.out.println(" 5. Sandwich");

System.out.println(" 6. Exit");

System.out.println("+++++++++++++++++++++++++++++++");

System.out.println("");

do

{

System.out.print("Please Enter your choice (1-6): ");

choice=a.nextInt();

switch(choice)

{

case 1:

food = "Hamburger";

System.out.println("");

System.out.println("Your choice is: " + food);

System.out.println("");

break;

case 2:

food = "Pizza";

System.out.println("");

System.out.println("Your choice is: "+ food);

System.out.println("");

break;

case 3:

food = "Noodle";

System.out.println("");

System.out.println("Your choice is: " + food);

System.out.println("");

break;

case 4:

food = "Salad";

System.out.println("");

System.out.println("Your choice is: " + food);

System.out.println("");

break;

case 5:

food = "Sandwich";

System.out.println("");

System.out.println("Your choice is: " + food);

System.out.println("");

break;

case 6:

System.out.println("");

System.out.println("Your choice is: Finish the Order!");

System.out.println("");

System.out.println("No need to pay the delivery fee.");

System.out.println("");

System.out.println("The total price is $0.0.");

System.out.println("");

System.out.println("Thank you for ordering!");

System.exit(0);

break;

default:

System.out.println("");

System.out.println("That is a wrong input. Please try again! ");

System.out.println("");

break;

}

}

while(choice>6||choice<1);

System.out.print("Would you like to have some meat on your " + food + "? ");

type=a.next();

if(type.equalsIgnoreCase("yes"))

{

System.out.println("");

System.out.print("Beef or Pork? ");

meat = a.next();

if(meat.equalsIgnoreCase("beef"))

cost = withBeef;

else if(meat.equalsIgnoreCase("pork"))

cost = withPork;

else

cost = 0;

}

totalCost+=cost;

System.out.println("");

System.out.print("Would you like to have more food from the menu? ");

order=a.next();

if(order.equalsIgnoreCase("no"))

{

System.out.println("");

System.out.print("The total price is less than $50. You need to pay the delivery fee $5.");

System.out.println("");

}

if(totalCost<50)

{

delivery=deliveryCharge;

}

System.out.println("");

System.out.print("Would you like to tip? Please enter the amount (if no, enter 0): ");

tips=a.nextDouble();

totalAmount=totalCost+delivery+tips;

System.out.println("");

System.out.println("The total price is $"+totalAmount);

System.out.println("");

System.out.println("Thank you for ordering!");

a.close();

}

}

My code works fine, however, when the user writes "Yes", to the question where it says "Would you like to have more food from the menu?", it does not go back and says "Please enter your choice".

The output looks like this:

Please Enter your choice (1-6): 3

Your choice is: Noodle

Would you like to have some meat on your Noodle? yes

Beef or Pork? beef

Would you like to have more food from the menu? yes

Would you like to tip? Please enter the amount (if no, enter 0):

However, after the user asks for more food, I want the program to re-ask the "Please enter your choice (1-6)" question. Right now, it just continues to the tip section. How can I fix it?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

Students also viewed these Databases questions

Question

Explain the LAMP framework.

Answered: 1 week ago