Question

Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

Let's fix the displayMenu() method!

First, edit the method to do the following:

  1. We want to also allow the user to quit. Include a "Quit" option in the print statements!
  2. Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!)
    What is the method's return type now?                            [ Select ]                       ["int", "double", "String"]      
  3. Include input validation for the user's choice before returning the value. For example, if the user enters something that is not an option (such as a 10), tell them to choose again. Keep asking them to enter a number until the input is valid. Only return an integer that is a valid option.
    What will you use to create this input validation?                            [ Select ]                       ["if-statement", "loop"]      

Back in the main() method, add a loop now as well! The user should be able to keep choosing options until they decide to quit. Only stop iterating once they've chosen the option to quit.

Display a goodbye message at the end of the program.

My code is below;

import java.util.Scanner;

public class CoffeeShop {

   public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int mainMenuChoice = 0;

System.out.println("Welcome to the coffee shop!");

double total = 0.0;

do{

   displayMainMenu();

   mainMenuChoice = scan.nextInt();

   char selection;

   switch(mainMenuChoice){

case 1:

   coffeeSubmenu();

   selection = scan.next().charAt(0);

   total += calculateCoffeePrice(selection);

   break;

case 2:

pastrySubmenu();

selection =scan.next().charAt(0);

   total += calculatePastryPrice(selection);

   break;

case 3:

sodaSubmenu();

selection = scan.next().charAt(0);

   total += calculateSodePrice(selection);

   break;

case 4:

break;

   }

}while(mainMenuChoice != 4);

System.out.println("Total: $" + total);

   }

   private static double calculatePastryPrice(char selection) {

double price = 0.0;

if(selection == 'A'){

price = 4.99;

} else if (selection == 'B'){

price = 2.49;

}

return price;

}

   private static double calculateSodePrice(char selection) {

double price = 0.0;

if(selection == 'A'){

price = 2.99;

} else if (selection == 'B'){

price = 2.49;

}

return price;

}

   public static double calculateCoffeePrice(char selection) {

double price = 0.0;

if(selection == 'A'){

   price = 2.99;

} else if (selection == 'B'){

   price = 3.99;

}

return price;

   }

   public static void displayMainMenu() {

System.out.println("Please select the item type.");

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

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

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

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

   }

   public static void coffeeSubmenu() {

System.out.println("Please select the coffee.");

System.out.println("A. Drip Coffee");

System.out.println("B. Latte");

   }

   public static void pastrySubmenu() {

System.out.println("Please select the pastry.");

System.out.println("A. Croissant");

System.out.println("B. Muffin");

   }

   public static void sodaSubmenu() {

System.out.println("Please select a soda.");

System.out.println("A. Coca-Cola");

System.out.println("B. Pepsi");

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// CoffeeShop.java

import java.util.Scanner;

public class CoffeeShop {
   static Scanner scan = new Scanner(System.in);
   public static void main(String[] args) {

       char selection;

       int mainMenuChoice = 0;

       System.out.println("Welcome to the coffee shop!");

       double total = 0.0;

       do {

          
           mainMenuChoice=displayMainMenu();


      

           switch (mainMenuChoice) {

           case 1:

               selection=coffeeSubmenu();

                 

               total += calculateCoffeePrice(selection);

               break;

           case 2:

               selection=pastrySubmenu();

               total += calculatePastryPrice(selection);

               break;

           case 3:

               selection =sodaSubmenu();

               total += calculateSodePrice(selection);

               break;

           case 4:

               break;
          
           }
          
          

       } while (mainMenuChoice != 4);

       System.out.println("Total: $" + total);
      
       System.out.println("\nGood Bye!");

   }

   private static double calculatePastryPrice(char selection) {

       double price = 0.0;

       if (selection == 'A') {

           price = 4.99;

       } else if (selection == 'B') {

           price = 2.49;

       }

       return price;

   }

   private static double calculateSodePrice(char selection) {

       double price = 0.0;

       if (selection == 'A') {

           price = 2.99;

       } else if (selection == 'B') {

           price = 2.49;

       }

       return price;

   }

   public static double calculateCoffeePrice(char selection) {

       double price = 0.0;

       if (selection == 'A') {

           price = 2.99;

       } else if (selection == 'B') {

           price = 3.99;

       }

       return price;

   }

   public static int displayMainMenu() {

       int choice;
       while(true)
       {
           System.out.println("Please select the item type.");

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

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

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

           System.out.println("4. Quit");
           System.out.print("Enter Choice :");
           choice=scan.nextInt();
           if(choice<1 || choice>4)
           {
               System.out.println("** Invalid Choice **");
           }
           else
               break;
       }
      
       return choice;
  

   }

   public static char coffeeSubmenu() {

       char ch;
   while(true)
   {
       System.out.println("\nPlease select the coffee.");

           System.out.println("A. Drip Coffee");

           System.out.println("B. Latte");
           System.out.print("Enter Choice :");
           ch=scan.next().charAt(0);
          
           if(ch=='A' || ch=='a' || ch=='B' || ch=='b')
           {
               break;
           }
           else
           {
               System.out.println("** Invalid Choice **");
           }
   }
   return ch;
  

   }

   public static char pastrySubmenu() {

       char ch;
   while(true)
   {
       System.out.println("\nPlease select the pastry.");

       System.out.println("A. Croissant");

       System.out.println("B. Muffin");
       System.out.print("Enter Choice :");
       ch=scan.next().charAt(0);
       if(ch=='A' || ch=='a' || ch=='B' || ch=='b')
       {
           break;
       }
       else
       {
           System.out.println("** Invalid Choice **");
       }
}
return ch;

   }

   public static char sodaSubmenu() {
       char ch;
   while(true)
   {
       System.out.println("\nPlease select a soda.");

       System.out.println("A. Coca-Cola");

       System.out.println("B. Pepsi");
       System.out.print("Enter Choice :");
       ch=scan.next().charAt(0);
       if(ch=='A' || ch=='a' || ch=='B' || ch=='b')
       {
           break;
       }
       else
       {
           System.out.println("** Invalid Choice **");
       }
}
return ch;
   }

}

==============================

Output:

Welcome to the coffee shop!
Please select the item type.
1. Coffee
2. Pastry
3. Soda
4. Quit
Enter Choice :1

Please select the coffee.
A. Drip Coffee
B. Latte
Enter Choice :D
** Invalid Choice **

Please select the coffee.
A. Drip Coffee
B. Latte
Enter Choice :A
Please select the item type.
1. Coffee
2. Pastry
3. Soda
4. Quit
Enter Choice :5
** Invalid Choice **
Please select the item type.
1. Coffee
2. Pastry
3. Soda
4. Quit
Enter Choice :2

Please select the pastry.
A. Croissant
B. Muffin
Enter Choice :B
Please select the item type.
1. Coffee
2. Pastry
3. Soda
4. Quit
Enter Choice :3

Please select a soda.
A. Coca-Cola
B. Pepsi
Enter Choice :A
Please select the item type.
1. Coffee
2. Pastry
3. Soda
4. Quit
Enter Choice :4
Total: $8.47

Good Bye!

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Let's fix the displayMenu() method! First, edit the method to do the following: We want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ram reads user input with command line argument, two inte ment, two integers and in between...

    ram reads user input with command line argument, two inte ment, two integers and in between tor. The programme will determine whether the input is valid, an rompt info if not. Read the codes, design the isNumber0 method t progran an operator. The program. (5 marks) public class ArithmeticTest ( public static void main(Stringl] args) ( if (isNumber(args[e]) & isNumber(args[2])) ( int value1 Integer.parseInt (args[e]); int value Integer.parseInt (args [2]); char op args[1].charAt (e); calcAndDisplay (value1, value2, op); ) else...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT