/**
* Input Validation System
*
* File: InputSystem.java
* @author: Michael Smith
*/
// Package to be put in
package util;
// Package needed
import java.util.*;
public class InputSystem {
/** Empty Constructor for Javadoc */
public InputSystem(){
}
/**
* Take a Message, and a Regular Expression, Display the Message,
* and prompt the user for an input that will continue untill the
* input matches the provided Regular Expression
* @param myMessage String to be the message displayed to the end user
* @param regExp String to be the Regular Expression used for Validation
* @return A validated result as String
*/
public static String get(String myMessage, String regExp){
// Create and Configure the Scanner
Scanner scanner = new Scanner(System.in);
String lineSeparator = System.getProperty("line.separator");
scanner.useDelimiter(lineSeparator);
// Tempoary String to be used for Scanner Input
String temp;
// Keep Prompting untill valid entry
do{
System.out.print(myMessage + "\t");
temp = scanner.next();
}while(!(temp.matches(regExp)));
// return Valid Result
return temp;
}
/**
* Prompt the user for a Yes/No response, and returns a boolean. Useful for
* asking a user to continue with an action in a loop.
* @param myMessage String to be displayed to the user
* @return Boolean based on response.
*/
public static Boolean yesNo(String myMessage){
String userInput;
userInput = get(myMessage, "[yYnN]");
if((userInput.equals("n")) || (userInput.equals("N"))){
return false;
}else{
return true;
}
}
/**
* Wait for User Input before proceding with the rest of the code
* @param myMessage String to be displayed to the user
*/
public static void wait(String myMessage){
Scanner scanner = new Scanner(System.in);
String lineSeparator = System.getProperty("line.separator");
scanner.useDelimiter(lineSeparator);
System.out.print(myMessage);
scanner.next();
}
}