/**
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
 */
package com.codeup.console;

import com.codeup.validation.Validator;

import java.io.PrintStream;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class Console {
    private final Scanner input;
    private final PrintStream output;

    public Console(Scanner input, PrintStream output) {
        this.input = input;
        this.output = output;
    }

    public String promptForNonEmptyText(String message) {
        output.print(message);
        String text = input.next();
        if (text.trim().isEmpty()) {
            output.println("Enter a non empty value");
            return promptForNonEmptyText(message);
        }
        return text;
    }

    public int promptForNumber(String message, Validator<Integer> validator) {
        int number;
        output.print(message);
        try {
            number = input.nextInt();
            if (!validator.isValid(number)) {
                output.println(validator.errorMessage());
                return promptForNumber(message, validator);
            }
        } catch (InputMismatchException e) {
            input.next();
            output.println("Enter a valid number");
            return promptForNumber(message, validator);
        }
        return number;
    }

    public int chooseFromList(String message, List options) {
        output.println(message);
        for (int i = 0; i < options.size(); i++) {
            output.println((i + 1) + ") " + options.get(i));
        }
        return input.nextInt() - 1;
    }

    public void askYesNoQuestion(String message, Action action) {
        do {
            action.execute();
            output.println(message);
        } while ("y".equalsIgnoreCase(input.next()));
    }

    public void message(String message) {
        output.println(message);
    }
}

@@ -131,16 +131,64 @@
 nsole;%0A%0A
+import com.codeup.validation.NumberWithinRange;%0A
 import c
@@ -1521,32 +1521,297 @@
 put.println(
-message)
+buildOptionsList(options));%0A        int chosenNumber = promptForNumber(%0A            message,%0A            new NumberWithinRange(1, options.size())%0A        );%0A        return chosenNumber - 1;%0A    %7D%0A%0A    private String buildOptionsList(List options) %7B%0A        String list = %22%22
 ;%0A        fo
@@ -1867,39 +1867,48 @@
     
-output.println((i + 1) + %22) %22 +
+list += String.format(%22%25n%25d) %25s%22, i + 1,
  opt
@@ -1950,27 +1950,12 @@
 urn 
-input.nextInt() - 1
+list
 ;%0A  

Add validation when the user selects from a list.