
/**
 * A class for some methods that print things.
 *
 * @author Mark Young (A00000000)
 */
public class Utilities {

    /** 
     * Print a title.
     * <p>
     * The title is printed underlined with hyphens, 
     * and 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) {
        // print a blank line
        System.out.println();

        // print the title
        System.out.println(title);

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

        // print a blank line
        System.out.println();
    }
    
    /** Maximum length of a line printed by printParagraph */
    public static final int MAX_LINE_LENGTH = 76;

    /** 
     * Print a paragraph, wrapped to MAX_LINE_LENGTH characters per line.
     * <p>
     * The paragraph has a blank line printed after it.
     * If the text contains a word of over MAX_LINE_LENGTH 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 > MAX_LINE_LENGTH) {
                // 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();
    }
}
