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
  • Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double...

    Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double inches) { return 2.54 * inches; }public static double inTOmeter(double inches) { return 0.0254 * inches; }public static double inTOft(double inches) { return 0.083333 * inches; }public static double inTOyd(double inches) { return 0.0277778 * inches; }public static double inTOkm(double inches) { return 0.000254 * inches; }public static double ftTOmeter(double feet) { return 0.3048 * feet; }public static double ftTOyd(double feet) { return 0.0003048...

  • 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...

  • When I invoke the forward or backward method the current_number isnt being changed. Not sure if...

    When I invoke the forward or backward method the current_number isnt being changed. Not sure if its something in my switch case or my methods.. package project4; import java.awt.image.BufferedImage; import java.net.URL; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class PictureViewer { final static int MIN_NUMBER = 0; final static int MAX_NUMBER = 8; static int image_number = 1;    public static int forward(int current_number) { if (current_number < MAX_NUMBER) { return current_number ++; }...

  • This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...

    This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;...

  • 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...

  • Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e.,...

    Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e., priority based on the radius of the circle). The application must be menu controlled similar to Project 4 and provide the following. Allow insertion of a "Circle" object/structure in the Priority Queue data structures. Allow display of all elements from Priority Queue data structure by Invoking a method/function "DisplayPriorityQueue" (uses "DeQueue" method). Allow for deletion of the Queue This is what I have so...

  • 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...

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