Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient:

public Letter(String from, String to)

Supply a method

public void addLine(String line)

to add a line of text to the body of the letter.

Supply a method

public String getText()

that returns the entire text of the letter. The text has the form:

Dear recipient name:
blank line
first line of the body
second line of the body 
. . .
last line of the body
blank line
Sincerely,
blank line
sender name

Hints: (1) Use the concat method to form a longer string from two shorter strings. (2) The special string "\n" represents a new line. For example, the statement

body = body.concat("Sincerely,").concat("\n");

adds a line containing the string "Sincerely," to the body.

Complete the following file:

Letter.java

/** This class models a simple letter. */ public class Letter { private String sender; private String recipient; private String body; /** Constructs a letter with a given sender and recipient. @param from the sender @param to the recipient */ public Letter(String from, String to) {
. . .
} /** Adds a line to the body of this letter. */ public void addLine(String line) {
. . .
} /** Gets the body of this letter. */ public String getText() {
. . .
} }

Use the following files:

LetterPrinter.java

public class LetterPrinter
{
   public static void main(String[] args)
   {
      Letter dearJohnLetter = new Letter("Mary", "John");

      dearJohnLetter.addLine("I am sorry we must part.");
      dearJohnLetter.addLine("I wish you all the best.");
      
      System.out.println(dearJohnLetter.getText());
   }
}

LetterTester.java

/**
   This program tests the Letter class.
*/
public class LetterTester
{ 
   public static void main(String[] args)
   {  
      Letter farewellLetter = new Letter("John", "Mary");

      farewellLetter.addLine("So long.");
      farewellLetter.addLine("Thanks for all the fish.");
      
      System.out.print("Text: ");
      System.out.println(farewellLetter.getText().replace("\n", "|"));
      System.out.println("Expected: Dear Mary:||So long.|Thanks for all the fish.||Sincerely,||John");
   }
}