Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will perform a mail merge. Program Description: When your p

ID: 3714821 • Letter: W

Question

Write a program that will perform a mail merge.

Program Description:

When your program starts, it will be given the name of letter file and the name of a data file. Your program should read the letter file, and for each place-holder-code found in the letter file, you should replace the place-holders with data found in the data file. Your program should create one output file for each line of data in the data file (in other words, if there are 3 lines of data in the data file, then your program should create 3 output files).

Your program must store the all of the integers in an array. The maximum number of integers that you can expect is 100, however there may not necessarily be that many. Your program should allow the user to continue entering numbers until they enter a negative number or until the array is filled - whichever occurs first. If the user enters 100 numbers, you should output an message stating that the maximum size for the list has been reached, then proceed with the calculations.

Your starter code includes a file named collection1.txt. The contents of this file is as follows:

Dear <title> <name>,

Please remember that the balance on your account (<balance>) remains
unpaid. It was due to be paid in full <days> days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have already sent your payment, please disregard this letter and accept our gratitude.

Thank you

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In the letter, the codes <title>, <name>, <balance>, and <days> indicate the place-holders which need to be filled in with customer data.

Your starter code also includes a file named collectionNames1.txt. The contents of this file is as follows:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Mr.,Smith,102.50,10

Mrs.,James,59.76,14
Ms.,Abrams,147.70,12
Mr.,Sessoms,112.10,18

In the data file, each line of text represents one customer. The customer data on one line is separated by commas. Thus the first line of data from the data file is:
      Mr.,Smith,102.50,10
and this represents the following data:
      <title> = "Mr."
      <name> = "Smith:
      <balance> = 102.50
      <days> = 10

You should use these files to test your program, and you are also encouraged to create your own TXT files to test with.

Output Files

For the example "collectionNames1.txt", there are four lines of data, so your program is expected to create four output files. The names of the ouput files should match the names of the customers. So for this example, you would create a file named Smith.txt, a file named James.txt, a file named Abrams.txt and a file named Sessoms.txt.

The contents of each of these files should be a copy of the input file, but with the correct data in the place holders. So the Smith.txt file should look like this:

Dear Mr. Smith,

Please remember that the balance on your account (102.50) remains
unpaid. It was due to be paid in full 10 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have already sent your payment, please disregard this letter and accept our gratitude.

Thank you

and the James.txt file should look like this:

Dear Mrs. James,

Please remember that the balance on your account (59.76) remains
unpaid. It was due to be paid in full 14 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have already sent your payment, please disregard this letter and accept our gratitude.

Thank you

The algorithm that you can use to create this program is as follows:

Create an array of Strings to hold the lines of text from the letter file. This is a partially-filled array, as you will not know how many lines of text will need to be stored.

Open the letter file. Read all of the lines of text from this file and store each line into a different position in the letter array.

Close the letter file as you will no longer need it.

Open the data file.

For each line of text found in the data file:

Read the line and store it into a simple String variable.

Use the split method from the String class to split the line of text into four separate strings (the four pieces of data which are separated by commas). The result of this method will produce a String array where each position in the array will contain one of the pieces of data (i.e. pos 0 will contain the title, pos 1 will contain the name, pos 2 will contain the balance and pos 3 will contain the days).

Open an output file where the name of the file should be the customer's name, followed by ".txt". So if the customer's name is "Smith" then the filename should be "Smith.txt".

For each String found in the letter array (the first array):

Make a copy of the string (so you don't mess up the original)

Using the replace method from the String class, replace "<title>" with the customer's title.

Using the replace method from the String class, replace "<name>" with the customer's name.

Using the replace method from the String class, replace "<balance>" with the customer's balance.

Using the replace method from the String class, replace "<days>" with the customer's days overdue.

Write the changed line into the output file

Close the output file

Technical notes, restrictions, and hints:

There is no keyboard input and no screen output for this program. All input data comes from the input files, and all output should be written to files.

You may assume that the form letter will not contain more than 15 lines. Therefore, the partially-filled array that you will create to hold the form letter should be created to be size 15.

There is a MAXLETTERSIZE constant which is already set to 15. Use it to create the letter array.

DO NOT attempt to read the entire data file and store the whole thing into an array. This file could actually contain more lines of data than you have RAM memory to hold --- so it may not even be possible to create an array large enough to hold the entire file. You should read and process one line of data at a time.

The starter code contains lines which already correctly get the filenames. The filenames are passed into your program from the command line, and will be found in the "args" array which is a parameter to the main method. args[0] will contain the letter file name and args[1] will contain the data file name.

There is the beginning of a generateLetter method in the starter code. You should use this method to create the letter for ONE customer. Thus, you will call this method one time for each line of data found in the data file. The parameters for this method are as follows:

letter = the array containing the lines of text from the template letter

letterSize = the used size of the letter array (remember, it's partially filled)

data = an array containing the four pieces of customer data (title, name, balance, days)

Absolutely NO other global variables (class variables) are allowed (except for the keyboard object).

NO BREAK STATEMENTS TO EXIT LOOPS

project part a getting started 1. Click the Open IDE button 2. In the IDE window, look in the leftmost column and find the CSCI 1010 folder and inside it, find the project11 folder 3. Open the Java file - MailMerge.java the project Write a program that will perform a mail merge Background When companies need to send out the same letter to many different customers, they usually have a form letter which contains "place-holder codes". Then, they will also create a list of data that will be used to fill in the place-holders in the form letter. A mail merge program is designed to create one copy of the template letter for each person's name that appears in the list. Program Description When your program starts, it will be given the name of letter file and the name of a data file. Your program should read the letter file, and for each place-holder-code found in the letter file, you should replace the place-holders with data found in the data file. Your program should create one output file for each line of data in the data file (in other words, if there are 3 lines of data in the data file, then your program should create 3 output files). Your program must store the all of the integers in an array. The maximum number of integers that you can expect is 100, however there may not necessarily be that many. Your program should allow the user to continue entering numbers until they enter a negative number or until the array is filled whichever occurs first. If the user enters 100 numbers, you should output an message stating that the maximum size for the list has been reached, then proceed with the calculations. Input Files The names of the input files are given to your program from the command line, when you type the command to execute your program. So, for example, you can test your program with the example files given by typing: MailMerge collection1.txt collectionNames1.txt

Explanation / Answer

Hi.. I have written java program for the above.

MailMerge.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MailMerge {

public static final int MAXLETTERSIZE = 15;

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub

  String[] letter1 = new String[MAXLETTERSIZE];
  String letterFileName = args[0];
  String dataFileName = args[1];
  
  
  File letterFile = new File(letterFileName);
  BufferedReader br1 = new BufferedReader(new FileReader(letterFile));
  
  String line1="";
  int i=0;
  while((line1=br1.readLine())!=null){
   letter1[i] = line1;
   i++;
   //System.out.println(line1);
  }
  
  
  
  File dataFile = new File(dataFileName);
  BufferedReader br = new BufferedReader(new FileReader(dataFile));
  
  String line="";
  while((line=br.readLine())!=null){
   
   String[] data = line.split(",");
   generateLetter(letter1,i,data);
   
  }
  System.out.println("Done");
  
}

public static void generateLetter(String[] letter, int letterSize, String[] data) throws IOException{
  String[] latest1 = new String[letterSize];
  
  for(int m=0;m<letterSize;m++){
   latest1[m] = letter[m];
  }

  for(int i=0;i<data.length;i++){

   for(int j=0;j<letterSize;j++){
    
    if(i==0){
     latest1[j]=latest1[j].replace("<title>",data[0]);
    }else if(i==1){
     latest1[j]=latest1[j].replace("<name>",data[1]);
    }else if(i==2){
     latest1[j]=latest1[j].replace("<balance>",data[2]);
    }else if(i==3){
     latest1[j]=latest1[j].replace("<days>",data[3]);
    }
    
   }
   
  }
  
  File f1 = new File("C:/Users/Desktop/"+data[1]+".txt");
  BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
  for(int k=0;k<letterSize;k++){
   bw.write(latest1[k]);
    bw.newLine();
  }
  bw.close();
}

}

collection1.txt

Dear <title> <name>,

Please remeber that the balance on your account (<balance>) remains
unpaid. It was due to be paid in full <days> days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have
already sent your payment, please disregard this letter and accept
our gratitude.

Thank you.

collectionNames1.txt

Mr.,Smith,102.50,10
Mrs.,James,59.76,14
Ms.,Abhrams,147.70,12
Mr.,Sessoms,112.10,18

Output Files:

Smith.txt

Dear Mr. Smith,

Please remeber that the balance on your account (102.50) remains
unpaid. It was due to be paid in full 10 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have
already sent your payment, please disregard this letter and accept
our gratitude.

Thank you.

James.txt

Dear Mrs. James,

Please remeber that the balance on your account (59.76) remains
unpaid. It was due to be paid in full 14 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have
already sent your payment, please disregard this letter and accept
our gratitude.

Thank you.

Abhrams.txt

Dear Ms. Abhrams,

Please remeber that the balance on your account (147.70) remains
unpaid. It was due to be paid in full 12 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have
already sent your payment, please disregard this letter and accept
our gratitude.

Thank you.

Sessoms.txt

Dear Mr. Sessoms,

Please remeber that the balance on your account (112.10) remains
unpaid. It was due to be paid in full 18 days ago. Enclosed is an
envelope in which you may mail your payment. If, by chance, you have
already sent your payment, please disregard this letter and accept
our gratitude.

Thank you.

Please check the code and let me know any querries. Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote