// Justify.java
//
//
// Ethan Bolker, April 2004 for CS110

// Not very sophisticated, but it will do.
// right and left methods have too much duplicated code.
//
// Should have overloaded methods to justify numbers.

import java.text.*;

/**
 * Class with static methods for justifying Strings.
 */

public class Justify
{
    /**
     * Right justify.
     *
     * @param text the String to justify.
     * @param length the length of the output String.
     *
     * If text is too long, don't truncate - better to destroy
     * formatting than to destroy information.
     *
     * @return text, padded on the left with blanks as needed.
     */
    public static String right( String text, int length ) 
    {
	if ( text.length() >= length ) {
	    return text;
	}
	int blankCount = length - text.length();
	StringBuffer buf = new StringBuffer(length);
	for (int i = 0; i < blankCount; i++) {
	    buf.append(' ');
	}
	buf.append(text);
	return buf.toString();
    }

    /**
     * Left justify.
     *
     * @param text the String to justify.
     * @param length the length of the output String.
     *
     * If text is too long, don't truncate - better to destroy
     * formatting than to destroy information.
     *
     * @return text, padded on the right with blanks as needed.
     */
    public static String left( String text, int length ) 
    {
	if ( text.length() >= length ) {
	    return text;
	}
	int blankCount = length - text.length();
	StringBuffer buf = new StringBuffer(length);
	buf.append(text);
	for (int i = 0; i < blankCount; i++) {
	    buf.append(' ');
	}
	return buf.toString();
    }

    public static void main( String[] args )
    {
	for( int i = 0; i < args.length; i++ ) {
	    System.out.println("|" + Justify.right(args[i],5) + "|");
	    System.out.println("|" + Justify.left(args[i],5) + "|");
	}
    }
}


