public class Menu
extends java.lang.Object
Constructor and Description |
---|
Menu()
Default constructor.
|
Menu(java.lang.String menuTitle)
Constructor with title.
|
Modifier and Type | Method and Description |
---|---|
void |
addOption(java.lang.String optionText)
Adds an option to the menu.
|
void |
display()
Displays the menu title and options, with a blank line between the
title and the options.
|
int |
getChoice()
Same as the two-parameter version, except it uses a default prompt
and a default maximum number of tries.
|
int |
getChoice(int maxNumberOfTries)
Same as the two-parameter version, except it uses the default prompt
and the maximum number of tries input.
|
int |
getChoice(java.lang.String prompt)
Same as the two-parameter version, except it uses the input prompt
and the default maximum number of tries, which is 3.
|
int |
getChoice(java.lang.String prompt,
int maxNumberOfTries)
Prompts the user to enter a menu choice and then reads the choice
entered by the user at the keyboard.
|
void |
setTitle(java.lang.String menuTitle)
Sets (or resets) the title of an already existing menu.
|
public Menu()
None. Post-conditions:
A Menu object has been contructed with the title "Empty Menu" and no options.
public Menu(java.lang.String menuTitle)
===== Error: Title cannot exceed 70 characters. The title ... title that was too long goes here ... was not added to the menu. This menu has been given the default title: Empty Menu
menuTitle
- The title to be given to the menu.
Pre-conditions:None. Post-conditions:
A Menu object has been contructed with a title and no options.
public void setTitle(java.lang.String menuTitle)
===== Error: Title length cannot exceed 70 characters. The title ... text of title that was too long appears here ... was not added to the menu.
menuTitle
- The title to be given to the menu.
Pre-conditions:This Menu object and menuTitle have been initialized. Post-conditions:
The title of this Menu object has been set to the value in menuTitle, or a message has been displayed indicating that title was too long and original title has been retained.
public void addOption(java.lang.String optionText)
If the number of options on the menu is already 20, the following message is displayed, followed by a pause:
===== Error: Maximum number of menu options (20) exceeded. The option ... text of option that could not be added goes here was not added to the menu.If the length of the option being added exceeds 70 characters, the following message is displayed, followed by a pause:
===== Error: Option cannot exceed 70 characters in length. The option ... text of option that was too long appears here ... was not added to the menu.
optionText
- The text of the option to be added to the menu.
Pre-conditions:This Menu object and optionText have been initialized. Post-conditions:
The option in optionText has been added to this Menu object, and given the next available option number, or a message has been displayed indicating that the option was too long and the Menu object remains unchanged.
public void display()
This menu currently has no options.These three lines are centered vertically, more or less, on an otherwise blank screen. Pre-conditions:
This Menu object has been initialized. Post-conditions:
This Menu object has been displayed on the screen.
public int getChoice(java.lang.String prompt, int maxNumberOfTries)
===== Error: Menu has no options from which to choose.If the user enters an invalid menu choice, the following message is displayed, and the user gets to make another choice, unless the maximum number of tries allowed has been reached:
Error: Menu option choice invalid. Choice must be a value from 1 to #. Try again.Here # represents the maximum option number on the current menu. If the user makes the "maximum number of tries" invalid choices, the following message is displayed, and the method returns a value of -1:
Sorry, but that last choice was also invalid, and you only get # chances.Here # represents the maximum number of tries the user is permitted to enter a valid menu option.
Enter choice:and if the user enters a value < 2 for the maximum number of permitted tries, that value is set to 2.
prompt
- The prompt used to ask the user for a menu choice.maxNumberOfTries
- The maximum number of tries the user has
to enter a valid menu choice.
Pre-conditions:prompt and maxNumberOfTries are both initialized. Post-conditions:
A valid menu choice has been returned, or one or more error messaqes have been displayed and a value of -1 has been returned.
public int getChoice()
Enter the number of your menu choice here and then press Enter:and the default maximum number of tries is 3.
None. Post-conditions:
A valid menu choice has been returned, or one or more error messaqes have been displayed and a value of -1 has been returned.
public int getChoice(java.lang.String prompt)
prompt
- The prompt used to ask the user for a menu choice.
Pre-conditions:prompt has been initialized. Post-conditions:
A valid menu choice has been returned, or one or more error messaqes have been displayed and a value of -1 has been returned.
public int getChoice(int maxNumberOfTries)
maxNumberOfTries
- The maximum number of tries the user has
to enter a valid menu choice.maxNumberOfTries has been initialized. Post-conditions:
A valid menu choice has been returned, or one or more error messaqes have been displayed and a value of -1 has been returned.