• Menu(String[] options) A normal (non-default) constructor. Takes an array of s
ID: 3571738 • Letter: #
Question
• Menu(String[] options) A normal (non-default) constructor. Takes an array of strings as a parameter to initialize the option list in this Menu. This supplied array of string options may be null or an array of strings of length zero or more. Sets the opening and closing messages to null. Sets the top prompt to “Choose an option:” and the bottom prompt to “Enter an option number:”.
• Menu() A default constructor. Sets all five descriptive attributes to null.
• isEmpty() A facilitator. Determines whether the option list in this menu is null or empty.
• length() A facilitator. Returns zero if the option list in this menu is null; otherwise, it returns the length of the option list in this menu.
• toString() Returns a string formatted using the menu template shown above. Example:
Note the absence of the top and bottom messages in the output above as they were set to null when drinkMenu was created on line 3. The source code above may also choose to remove the top and bottom prompts as follows:
• readOptionNumber() Displays this menu and then inputs an integer number from the keyboard. If the option list is empty, it returns the number entered. Otherwise, it checks that the input integer is a valid option number: between 1 and n, inclusive, where n is the number of options in the option list. The method repeatedly displays this menu and then inputs a number until a valid option number is entered. After a valid option number has been entered, the method returns that number.
• Setter (mutator) and getter (accessor) methods (self-explanatory) – getBottomMessage, setBottomMessage – getBottomPrompt, setBottomPrompt – getTopMessage, setTopMessage – getTopPrompt, setTopPrompt
Opening Message Top Prompt (1) Option one (2) Option two (3) Option three (n) Option n Closing Message Bottom PromExplanation / Answer
eedHi,
Please see the Java classes below. Please comment for any queries/feedbacks.
Thanks,
Anita
Menu.java
import java.util.Scanner;
public class Menu {
private String[] options;
private String TopMessage;
private String Bottommessage;
private String topPrompt;
private String bottompPrompt;
public Menu() {
this.options =null;
this.TopMessage =null;
this.Bottommessage =null;
this.topPrompt =null;
this.bottompPrompt=null;
}
public Menu(String [] options) {
if(null!= options && options.length>0){
this.options= new String[options.length];
this.options=options;
this.TopMessage = null;
this.Bottommessage =null;
this.setTopPrompt("Choose an option:");;
this.setBottompPrompt("Enter an option number:");
}
}
public boolean isEmpty(){
if(null!=this.getOptions() && this.getOptions().length>0){
return false;
}
else{
return true;
}
}
public int length(){
if(null!=this.getOptions() && this.getOptions().length>0){
return this.getOptions().length;
}
else{
return 0;
}
}
public String toString()
{
String val="";
if(null!=this.getTopMessage()){
val = val + this.getTopMessage()+" ";
}
if(null!=this.getTopPrompt()){
val =val+this.getTopPrompt()+" ";
}else{
val=val+" ";
}
for(int i=0; i<length();i++){
val=val+" ("+new Integer(i+1)+") "+this.options[i]+" ";
}
if(null!=this.getBottommessage()){
val= val+ this.getBottommessage()+" ";
}
val=val+"?->";
if(null!=this.getTopPrompt()){
val=val+this.getBottompPrompt()+" ";
}
return val;
}
public int readOptionNumber(){
//Reading the value from console
Scanner scanner = new Scanner(System.in);
int val= scanner.nextInt();
if(val<=length()){
return val;
}else{
System.out.println(this.toString());
val=readOptionNumber();
}
return val;
}
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
public String getTopMessage() {
return TopMessage;
}
public void setTopMessage(String TopMessage) {
this.TopMessage = TopMessage;
}
public String getBottommessage() {
return Bottommessage;
}
public void setBottommessage(String Bottommessage) {
this.Bottommessage = Bottommessage;
}
public String getTopPrompt() {
return topPrompt;
}
public void setTopPrompt(String topPrompt) {
this.topPrompt = topPrompt;
}
public String getBottompPrompt() {
return bottompPrompt;
}
public void setBottompPrompt(String bottompPrompt) {
this.bottompPrompt = bottompPrompt;
}
}
MenuApp.java
public class MenuApp {
public static void main(String [] args){
String [] drink_options = {"Water", "Soda pop", "Beer"};
Menu drinkMenu = new Menu(drink_options);
drinkMenu.setTopPrompt(null);
drinkMenu.setBottompPrompt(null);
//System.out.println(drinkMenu.toString());
String[] option_list={"Water", "Soda pop", "Beer"};
Menu full_menu = new Menu(option_list);
full_menu.setTopMessage("Quench your thirst with our fine drinks!");
full_menu.setBottommessage("Timetoobey your thirst!");
full_menu.setTopPrompt("Choose your thirst crusher: ");
full_menu.setBottompPrompt("Enter a drink number: ");
System.out.println(full_menu.toString());
int choice = full_menu.readOptionNumber();
if(choice>0){
System.out.println("You entered "+choice);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.