Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I dont know how to loop programs and still familarizing with switch statements c

ID: 663926 • Letter: I

Question

I dont know how to loop programs and still familarizing with switch statements can you please help me make this program loop back and make the menu start over unless the enter 0 to exit also I need to know how to make all the dollar values add up if the customer chooses multiple types of coffee

below is the questions and my code I have started


//5. Write an application that displays a menu of three items for the Jivin’ Java Coffee
// Shop as follows:
// (1) American   1.99
// (2) Espresso   2.50
// (3) Latte   2.15

// Prompt the user to choose an item using the number (1, 2, or 3) that corresponds to
// the item, or to enter 0 to quit the application. After the user makes the first selection,
// if the choice is 0, display a total bill of $0. Otherwise, display the menu again. The user
// should respond to this prompt with another item number to order or 0 to quit. If the
// user types 0, display the cost of the single requested item. If the user types 1, 2, or 3,
// add the cost of the second item to the first, and then display the menu a third time. If
// the user types 0 to quit, display the total cost of the two items; otherwise, display the
// total for all three selections. Save the file as Coffee.java.
//Teacher add on
// Use a switch statement to determine which menu option the user requested.


import java.util.Scanner;
public class Coffee
{
public static void main(String[]args)
{
int coffeeType;
double coffeePrice;
String message;
boolean loop = true;


final int AMERICAN_COFFEE = 1;
final int ESPRESSO_COFFEE = 2;
final int LATTE_COFFEE = 3;
final int EXIT_CODE = 0;
final double AMERICAN_PRICE = 1.99;
final double ESPRESSO_PRICE = 2.50;
final double LATTE_PRICE = 2.15;
final double EXIT_PRICE = 0.00;
Scanner input = new Scanner(System.in);

System.out.println("Welcome to Javin' Java Coffee Please enter the number corresponding with the coffee type");
System.out.println("___________________________________");
System.out.println(" ");
System.out.println("(1) American   1.99");
System.out.println("(2) Espresso   2.50");
System.out.println("(3) Latte   2.15");
System.out.println("(0) Exit menu");
System.out.println("___________________________________");

coffeeType = input.next().charAt(0);
while(loop)

switch(coffeeType)
{
  

case (AMERICAN_COFFEE):
coffeePrice = AMERICAN_PRICE;
message = "American";
break;
case (ESPRESSO_COFFEE):
coffeePrice = ESPRESSO_PRICE;
message = "Espresso";
break;
case (LATTE_COFFEE):
coffeePrice = LATTE_PRICE;
message = "Latte";
break;
case (EXIT_CODE):
coffeePrice = EXIT_PRICE;
message = "Thank you for your selection, we'll start brewing your coffee now!";
break loop;
default:
coffeePrice = 0;
message = "invalid code";
if(coffeType)
{
loop = false;
}
}
System.out.println("Thank you for choosing " + message);
System.out.println("Your total price is: $" + coffeePrice);

}
}

Explanation / Answer

//Modified Coffee java program.
//All the modifications are highlighted with bold letters and added comments whereever necessarray.

//Coffee.java
import java.util.Scanner;
public class Coffee
{
   public static void main(String[]args)
   {
      
       int coffeeType;
       //declare and intiailaze the coffeePrice to zero
       double coffeePrice = 0;
       //set an empty message
       String message = "";

       boolean loop = true;


       final int AMERICAN_COFFEE = 1;
       final int ESPRESSO_COFFEE = 2;
       final int LATTE_COFFEE = 3;
       final int EXIT_CODE = 0;

       //coffee prices
       final double AMERICAN_PRICE = 1.99;
       final double ESPRESSO_PRICE = 2.50;
       final double LATTE_PRICE = 2.15;
      
       //exit code
       final int EXIT_PRICE = 0;

       //create a variable called TOTAL_PRICE to zero
       double TOTAL_PRICE = 0.00;

       //create an instance of Scanner class to read input
       Scanner input = new Scanner(System.in);

       //continue the menu for loop until user enter 0 to stop reading from
       //keyboard input

       while(loop)
       {
      
           //Insert the menu of choice to select by user inside the while loop
           //to ask user each time after every selection until user enter 0 to exit

          
           System.out.println(" Welcome to Javin' Java Coffee " +
                   "Please enter the number corresponding with the coffee type");
           System.out.println("___________________________________");
           System.out.println("                                   ");
           System.out.println("(1) American     1.99");
           System.out.println("(2) Espresso     2.50");
           System.out.println("(3) Latte     2.15");
           System.out.println("(0) Exit menu");
           System.out.println("___________________________________");

           //read integers from the keyboard using nextInt() method
           coffeeType = input.nextInt();

           switch(coffeeType)
           {
           //Remove brackets () for constants values of case labels
           case AMERICAN_COFFEE:
               coffeePrice = AMERICAN_PRICE;
               message = "American";
               //Add the coffeePrice to the TOTAL_PRICE until user enter 0 to exit
               TOTAL_PRICE+=coffeePrice;

               break;
              
           case ESPRESSO_COFFEE:
               coffeePrice = ESPRESSO_PRICE;
               message = "Espresso";
               //Add the coffeePrice to the TOTAL_PRICE until user enter 0 to exit
               TOTAL_PRICE+=coffeePrice;

               break;
              
           case LATTE_COFFEE:
               coffeePrice = LATTE_PRICE;
               message = "Latte";
               //Add the coffeePrice to the TOTAL_PRICE until user enter 0 to exit
               TOTAL_PRICE+=coffeePrice;

               break;
          
           case EXIT_CODE:
               coffeePrice = EXIT_PRICE;
               message = "Thank you for your selection," +
                       "we'll start brewing your coffee now!";
               //set loop boolean variable to false to exit from while loop
               loop=false;

               break;
              
           //for invalid coffee codes
           default:
               coffeePrice = 0;
               message = "Invalid code";
               //print a message for invalid code
               System.out.println("Enter a valid coffee code");

           }
          
           //check if user does not enter EXIT_CODE AS 0
           //then display the message and coffeePrice of selected coffee
           //otherwise dont display this message .
           if(coffeePrice!=EXIT_CODE)
           {
               System.out.println("Thank you for choosing " + message+" coffee ");
               System.out.println("Your selected Coffee price is: $ " + coffeePrice);
           }
          

       }//end of while loop
      
       //check if TOTAL_PRICE is not zero and coffePrice is EXIT_CODE
       //then display the user with total price of previously selected coffees
       if(TOTAL_PRICE!=0 && coffeePrice==EXIT_CODE)
           System.out.printf("Your total price is: $ %.2f " ,TOTAL_PRICE);
      

       System.out.println("Thank you.Visit again");
   }
}

----------------------------------------------------------------------------------------------------------------

Sample output:

Welcome to Javin' Java Coffee
Please enter the number corresponding with the coffee type
___________________________________
                                 
(1) American     1.99
(2) Espresso     2.50
(3) Latte     2.15
(0) Exit menu
___________________________________
1
Thank you for choosing American coffee
Your selected Coffee price is: $ 1.99

Welcome to Javin' Java Coffee
Please enter the number corresponding with the coffee type
___________________________________
                                 
(1) American     1.99
(2) Espresso     2.50
(3) Latte     2.15
(0) Exit menu
___________________________________
2
Thank you for choosing Espresso coffee
Your selected Coffee price is: $ 2.5

Welcome to Javin' Java Coffee
Please enter the number corresponding with the coffee type
___________________________________
                                 
(1) American     1.99
(2) Espresso     2.50
(3) Latte     2.15
(0) Exit menu
___________________________________
3
Thank you for choosing Latte coffee
Your selected Coffee price is: $ 2.15

Welcome to Javin' Java Coffee
Please enter the number corresponding with the coffee type
___________________________________
                                 
(1) American     1.99
(2) Espresso     2.50
(3) Latte     2.15
(0) Exit menu
___________________________________
0
Your total price is: $ 6.64
Thank you.Visit again

Hope this helps you