
import java.util.Scanner;

/**
 * A class for some helpful input and output methods.
 *
 * @author Mark Young (A00000000)
 */
public class Utilities {

    public static final Scanner KBD = new Scanner(System.in);
    private static final int SCREEN_WIDTH = 76;

    /**
     * Get a Scanner defined on System.in. The intent is that there should be
     * only one Scanner connected to System.in for the whole program -- so we
     * can avoid buffering issues that arise when we have multiple Scanners
     * connected to a single source.
     *
     * @return the Scanner from this class that's connected to System.in
     */
    public static Scanner getKeyboard() {
        return KBD;
    }

    /**
     * Prompt the user and wait for them to press the enter key. Add blank lines
     * before and after the prompt.
     */
    public static void pause() {
        System.out.print("\n...press enter...");
        KBD.nextLine();
        System.out.println();
    }

    /**
     * Print a title.
     * <p>
     * The title is underlined with hyphens, with blank lines before and after.
     * The capitalization of the title is not changed for printing.
     *
     * @param title the title to be printed
     */
    public static void printTitle(String title) {
        printTitle(title, '-');
    }

    /**
     * Print a title using a particular underlining character.
     * <p>
     * The title is underlined with the given character, with blank lines before
     * and after. The capitalization of the title is not changed for printing.
     *
     * @param title the title to be printed
     * @param under the character to use to underline the title
     */
    public static void printTitle(String title, char under) {
        // print blank line before
        System.out.println();

        // print the title and underline it
        System.out.println(title);
        for (int i = 1; i <= title.length(); ++i) {
            System.out.print(under);
        }
        System.out.println();

        // print blank line after
        System.out.println();
    }

    /**
     * Print a paragraph, wrapped to SCREEN_WIDTH characters per line.
     * <p>
     * The paragraph has a blank line printed after it. If the text contains a
     * word of over SCREEN_WIDTH characters, that word will overflow the right
     * edge of the paragraph. (This method does not hyphenate words.)
     * <p>
     * The method assumes that the line it starts on was empty. If it is not
     * empty, the first line of the paragraph may overflow the right margin.
     *
     * @param text the text of the paragraph to be printed.
     */
    public static void printParagraph(String text) {
        // create variable for how many characters are on the current line
        int usedSoFar = 0;

        // break string into words
        String[] words = text.split(" ");

        // for each word
        for (int i = 0; i < words.length; ++i) {
            // need to print every character of the word, plus a space
            int spaceNeeded = words[i].length() + 1;

            // if there's not enuf space for this word
            if (usedSoFar + spaceNeeded > SCREEN_WIDTH) {
                // end the line, giving us a new, blank line
                System.out.println();
                usedSoFar = 0;
            }

            // print the word; update the number of characters on this line
            System.out.print(words[i] + " ");
            usedSoFar += spaceNeeded;
        }
        // end the last line of the paragraph
        System.out.println();

        // print a blank line
        System.out.println();
    }

}
