Click here to Skip to main content
15,845,744 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
MenuHelper class

This class is designed to provide a useful method that can be used when implementing value and type safe input checking in your programs. You will need to see the MenuHelper class API for a list of methods that should be implemented. 

You should also create a main method in this class that can be used to test out your code. You should use the method created in this class to display three menus to the user to provide a choice for a meal (you should have three options) and two sides (each should have four options) and store the user selection. Make sure to format your menu text so it looks nice. 

After the user has made each of the three selections, you should print out their responses. Note, that it is not necessary to display the name of what was chosen, merely the number that was selected. Also, you should not hardcode any part of the message to be displayed into the method. Your method should just display whatever the message parameter is. 

An example of the output (user input shown in red) might look like this: 

Please choose a main course:
 1) Chicken
 2) Beef
 3) Pork
Please enter a selection 13: 2

Please choose a side:
 1) Baked Potato
 2) Soup
 3) Salad
 4) Vegetables
Please enter a selection 14: 4

Please choose a side:
 1) Baked Potato
 2) Soup
 3) Salad
 4) Vegetables
Please enter a selection 14: 1

You selected options 2, 4, 1 for your choices.



Methods:

Identifier:	displayMenu(String message, int min, int max)
Parameters:	message – The menu text that should be displayed
min – The minimum allowable menu option
max – The maximum allowable menu option
Return Value:	int – The choice selected by the user
Other:	This method should be static.

When this method is called, it will display the String parameter which is the menu text which should include the possible selections. This method should then prompt the user to make a selection and perform value and type-safe user input checking until a user has made a valid selection. If the user does not enter an int value, they should be told "Please enter a number between $min and $max" where $min and $max are the values of the parameters min and max. 


What I have tried:

i tried but it failed the test...
Posted
Updated 30-Mar-17 7:35am
Comments
CHill60 29-Mar-17 19:03pm    
You tried what and it failed what test?
[no name] 29-Mar-17 19:22pm    
We aren't here to do your homework assignments for you.
Member 13036251 30-Mar-17 15:36pm    
Hide Expand Copy Code
//***********************************************************************************************************************************************
// Purpose: When this method is called, it will display the String parameter which is the menu text which should include the possible selections.
// This method should then prompt the user to make a selection and perform value and type-safe user input checking until a user has made
// a valid selection. If the user does not enter an int value, they should be told “Please enter a number between $min and $max” where
// $min and $max are the values of the parameters min and max.
//***********************************************************************************************************************************************

import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner;
import java.io.*;

// The MenuHelperClass
public class MenuHelperClass
{
public static int displayMenu(String message, int min, int max)
{
int choice = 0;
String input = null;
String error = "";

while (choice < min || choice > max) {

System.out.println(message);
Scanner in = new Scanner(System.in);
choice = in.nextInt();
error = "Please enter a number between " + min + " and " + max;
System.out.print(error);
}

return choice;
}

// arguments are passed using the text field below this editor
public static void main(String[] args)
{
String[][] menuArray = new String [][] { { "Please select a main course:\n 1) Chicken\n 2) Beef\n 3) Pork\n Please make a selection 1-3:", "1", "3"},
{ "Please chose a side:\n 1) Baked Potato\n 2) Soup\n 3) Salad\n 4) Vegetables Please make a selection 1-4:", "1", "4"},
{ "Please chose a side:\n 1) Baked Potato\n 2) Soup\n 3) Salad\n 4) Vegetables Please make a selection 1-4:", "1", "4"}};

int rows = 3;
int columns = 3;
String[] selections = new String[3];
int selectionIndex = 0;

for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++) {

String message = menuArray[i][j];
int min = Integer.parseInt(menuArray[i+1][j+1]);
int max = Integer.parseInt(menuArray[i+2][j+2]);
int output = MenuHelperClass.displayMenu(message, min, max);
selections[selectionIndex] = Integer.toString(output);
selectionIndex++;
break;
}
System.out.println( "\n" );
}

String selectedValues = "";
for(int z=0; z < 3; z++){
selectedValues += selections[z] + ", ";
}
System.out.println("You selected options "+ selectedValues +" for your choices.");
}

}

If you could figure out the readline part, it should work. Might need alittle more debugging.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 
I got this much done

package Questions;

import java.util.Scanner;

/**
 *
 * @author BA-Ll-ab
 */
public class MenuHelper {
    private String message;
    private int min;
    private int max;
    
      public static int displayMenu(String message, int min, int max) {

       int choice = 0;
       System.out.println(message);
       String error = "";
       while (choice < min || choice > max) {
           System.out.print(error);
           try {
               choice = Integer.parseInt(scanner.nextLine());
           } catch (NumberFormatException e) {
           }
           error = "Please enter a number between " + min + " and " + max;
       }

       return choice;
   }


    
    
    public static void main(String[] args) {
        
    }
}
 
Share this answer
 
Comments
Patrice T 29-Mar-17 19:54pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900