/* JunkMail.java  -  Craig Persiko  -  CS 111A
   Junk Mail Personalizer - sample program using file i/o
*/
import java.util.Scanner;

import java.io.*;


class JunkMail
{
  public static void main(String[] args) throws IOException
  {
    final String NAME_CODE = "#N#"; // constant

    String lineIn;
    int name_idx;
    String inFileName, outFileName, customerName;
    Scanner inFile, keyboard;
    PrintWriter outFile;
    File file;

    System.out.println("Welcome to the Junk Mail Personalizer.");

    // Set up keyboard input reader:

    keyboard = new Scanner(System.in);

    // Get filenames from user:

    do
    {
      System.out.println("Enter name of file with text of template letter using name codes");
      inFileName = keyboard.nextLine();
      file = new File(inFileName);
    }while (!file.exists());

    System.out.println("Enter name of output file to create with complete letter");
    outFileName = keyboard.nextLine();

    System.out.println("Please enter the name of letter recipient");
    customerName = keyboard.nextLine();

    // Set up file input reader and open file:

    inFile = new Scanner(file);

    // Set up file output stream and open file:

    outFile = new PrintWriter(new File(outFileName));

    while (inFile.hasNext()) // while there are lines in the file

    {
      lineIn = inFile.nextLine();
      name_idx = lineIn.indexOf(NAME_CODE);
      while (name_idx > -1) // while NAME_CODE is in lineIn

      {
        // output text before name:

        outFile.print(lineIn.substring(0, name_idx));
        outFile.print(customerName); // output name

        // cut off that beginning part of lineIn that we've handled already:

        lineIn = lineIn.substring(name_idx + NAME_CODE.length());
        name_idx = lineIn.indexOf(NAME_CODE);
      }
      // print remainder of line (entire line if NAME_CODE not found)

      outFile.println(lineIn);
    }

    System.out.println("Personalized letter has been saved!");

    inFile.close();
    outFile.close();
  }
}

/*  Sample Output:

-bash-3.2$ javac JunkMail.java
-bash-3.2$ java JunkMail
Welcome to the Junk Mail Personalizer.
Enter name of file with text of template letter using name codes
trash
Enter name of file with text of template letter using name codes
IguanaLetter.txt
Enter name of file with text of template letter using name codes
iguanaLetter.txt
Enter name of output file to create with complete letter
myIggy.txt
Please enter the name of letter recipient
Craig Persiko
Personalized letter has been saved!
-bash-3.2$ cat iguanaLetter.txt
Hello, #N#!

Have you heard yet about the Bay Area's #1 iguana trainers?
Don't you think it's time your friends said, "What a talented
iguana you have, #N#!"  Well, #N#, look no further.
We will teach your iguana to do flips in the air, jump
backwards, and do the moonwalk, all on command!

Call now, and we'll include a free book, "Iguana Psychology",
to help you, #N#, and your iguana really become close.
Just mention offer #N-P.

Iguana Lovers - (415) LUV-IGUANA
-bash-3.2$ cat myIggy.txt
Hello, Craig Persiko!

Have you heard yet about the Bay Area's #1 iguana trainers?
Don't you think it's time your friends said, "What a talented
iguana you have, Craig Persiko!"  Well, Craig Persiko, look no further.
We will teach your iguana to do flips in the air, jump
backwards, and do the moonwalk, all on command!

Call now, and we'll include a free book, "Iguana Psychology",
to help you, Craig Persiko, and your iguana really become close.
Just mention offer #N-P.

Iguana Lovers - (415) LUV-IGUANA
-bash-3.2$

*/


syntax highlighted by Code2HTML, v. 0.9