Created the abstract class Application.java and JavaDocs.
This commit is contained in:
parent
80c6c74a66
commit
d007cc2212
@ -1,9 +1,130 @@
|
||||
package Practice;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class is used to implement the generic features and UI of the problems.
|
||||
*
|
||||
* @author Jonathan Turner
|
||||
* @version Spring 2024
|
||||
*/
|
||||
public abstract class Application {
|
||||
|
||||
private final int numOfOptions;
|
||||
private final String[] menuOptions;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new application with specified menu options.
|
||||
*
|
||||
* @precondition options != null && options.length > 1
|
||||
* @postcondition a new application is created.
|
||||
*
|
||||
* @param options the options for the program.
|
||||
*/
|
||||
public Application(String[] options) {
|
||||
if (options == null || options.length == 0) {
|
||||
throw new IllegalArgumentException("The options must be valid and options.length < 1.");
|
||||
}
|
||||
this.numOfOptions = options.length;
|
||||
this.menuOptions = options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used as the main sentential loop and loops until the exit option is selected.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition none
|
||||
*/
|
||||
public void start() {
|
||||
|
||||
int option = this.getOption();
|
||||
while (option != (this.numOfOptions+1)) {
|
||||
this.executeOption(option);
|
||||
option = this.getOption();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to handle the option being selected.
|
||||
*
|
||||
* @precondition 0 < option <= this.numOfOptions
|
||||
* @postcondition none
|
||||
*
|
||||
* @param option the Option being executed.
|
||||
*/
|
||||
public abstract void executeOption(int option);
|
||||
|
||||
protected String getString(String prompt, String prefix, String error) {
|
||||
System.out.println(prompt);
|
||||
System.out.print(prefix);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String input = sc.nextLine();
|
||||
|
||||
while (input == null || input.isEmpty()) {
|
||||
System.out.println(error);
|
||||
System.out.print(prefix);
|
||||
input = sc.nextLine();
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user input to a number. A prompt must be provided prior to running this method.
|
||||
* If the input was not an integer/(unable to be autoboxed), returns -1.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition none
|
||||
*
|
||||
* @return the integer input, if invalid -1.
|
||||
*/
|
||||
protected int getIntegerInput() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try {
|
||||
String textInput = sc.nextLine();
|
||||
return Integer.parseInt(textInput);
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the menu options.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition the menu is displayed.
|
||||
*/
|
||||
private void displayMenu() {
|
||||
System.out.println("-----------------MAIN MENU--------------");
|
||||
for (int index = 0; index < this.numOfOptions; index++) {
|
||||
System.out.println((index+1) + ". " + this.menuOptions[index]);
|
||||
}
|
||||
System.out.println("3. Exit program");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the menu of options, asks for an input, and if that input is invalid it prints an error and
|
||||
* prompts the user again for the input.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition none
|
||||
*
|
||||
* @return the option selected
|
||||
*/
|
||||
private int getOption() {
|
||||
this.displayMenu(); // Prints out the menu
|
||||
|
||||
// Asks for the option and begins the loop until a valid option is gathered.
|
||||
System.out.print("Enter option number: ");
|
||||
int input = getIntegerInput();
|
||||
while (input < 1 || input > (this.numOfOptions+1)) { // Compares it to the valid options available.
|
||||
System.out.println("\nPlease enter a valid input.");
|
||||
System.out.print("Enter option number: ");
|
||||
input = getIntegerInput();
|
||||
}
|
||||
System.out.println();
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user