/* HelloWorld.java */

/**
 * A class of objects containing a "Hello, World!" message 
 * with a variable number of newlines ("\n") before and after the text.
 *
 * @author Zach Tomaszewski
 * @version  06 Sept 2004
 */
public class HelloWorld {

  private static final String MESSAGE = "Hello, World!";
  private int preNewLines;
  private int postNewLines;
  

  /**
   * Creates a HelloWorld with the given parameters.
   * @param preNew    The number of newlines to print before 
   *                   the contained message
   * @param postNew   The number of newlines to print after the message
   */
  public HelloWorld(int preNew, int postNew){
    this.preNewLines = preNew;
    this.postNewLines = postNew;
  }

  /**
   * Creates a HelloWorld with 0 pre-message "\n"s and 1 post-message "\n"'s
   */
  public HelloWorld(){
    this.preNewLines = 0;
    this.postNewLines = 1;
  }
  
  
  /**
   * Returns how many "\n"s will be printed before the contained message.
   */
  public int getPreMessageNewlines() {
    return this.preNewLines;
  }

  /**
   * Sets how many "\n"s will be printed before the contained message.
   *
   * @param pre  The new number of newlines to be printed
   */
  public void setPreMessageNewlines(int pre) {
    this.preNewLines = pre;
  }


  /**
   * Returns how many "\n"s will be printed after the contained message.
   */
  public int getPostMessageNewlines() {
    return this.postNewLines;
  }  

  /**
   * Sets how many "\n"s will be printed after the contained message.
   *
   * @param post  The new number of newlines to be printed
   */
  public void setPostMessageNewlines(int post) {
    this.postNewLines = post;
  }


  /**
   * Returns a String of this object, containing pre-newlines, the message,
   * and post-newlines.
   *
   * @return  A String of this object.
   */
  public String toString() {
    return (repeatChar('\n', preNewLines) + MESSAGE 
            + repeatChar('\n', postNewLines));
  }


  /**
   * Returns a String with the given character repeated.
   * <code>times</code> must be >= 0, or an empty String is returned.
   *
   * @param ch  The char to repeat
   * @param times  The number of times to repeat the char
   * @return The String formed by repeating ch.
   */
  private static String repeatChar(char ch, int times) {
    String repeated = "";
    for (int i = 0; i < times; i++) {
      repeated += ch;
    }
    return repeated;
  }
  

  /**
   * Tests HelloWorld
   */
  public static void main(String[] args) {
    System.out.println("Test of the number of pre- and post-message newline " +
                       "characters.");
    System.out.println("Default HelloWorld: ");
    HelloWorld hw1 = new HelloWorld();
    System.out.println(hw1);

    System.out.println("HelloWorld -- 2 pre and 1 post newlines: ");
    HelloWorld hw2 = new HelloWorld(2, 1);
    System.out.println(hw2);
    
    System.out.println("Getting pre [2]: " + hw2.getPreMessageNewlines());
    System.out.println("Getting post [1]: " + hw2.getPostMessageNewlines());

    System.out.println("Setting pre to 3.");
    hw2.setPreMessageNewlines(3);
    System.out.println("Setting post to 2.");
    hw2.setPostMessageNewlines(2);
    System.out.println("HelloWorld -- 3 pre, 2 post newlines:");
    System.out.println(hw2);
    
    System.out.println("--- End Test ---");
  }
}
