package ui; import java.util.Scanner; public class Console { /** Structured user's console input. */ private static Scanner scanner = new Scanner (System.in); /** Prompts the user for an integer number in the console and returns it. * Doesn't validate the input, yet * @param label the prompt * @return the number the user entered. */ public static int promptForInt (String label) { System.out.print (label + ">"); return scanner.nextInt (); } /** Prompts the user for one of the options and returns the option selected. * Returns the last option if the reply doesn't match any of the options. * @param prompt * @param options * @return option selected, last option if none selected. */ public static String promptForOption (String prompt, String options []) { prompt += " ("; for (String option: options) {prompt += option + "/";} prompt = prompt.substring (0, prompt.length () - 1) + ")>"; System.out.print (prompt); String response = scanner.next (); for (String option: options) { if (response.startsWith (option)) {return option;} } return options [options.length - 1]; } /** Asks the user in the console whether to quit and returns true if she enters "y". * @return true if entry starts with y, false otherwise. */ public static boolean quit () { System.out.print ("Do you want to quit? (y/-)>"); boolean theEnd = scanner.next ().startsWith ("y"); if (theEnd) {System.out.print ("The End.");} return theEnd; } }