Write a java program that will perform a mail merge. Background When companies n
ID: 3714498 • Letter: W
Question
Write a java 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
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. Starter code given
public class MailMerge {
public static final int MAXLETTERSIZE = 15;
public static void main(String [] args) throws FileNotFoundException {
String [] letter = new String[MAXLETTERSIZE];
String letterFileName = args[0];
String dataFileName = args[1];
}
// Generates one form letter for one customer
// letter = an array containing lines of text representing the template letter
// letterSize = the number of lines currently stored in the letter array
// data = an array containing the customer data - one piece of data in each array position
// Output: writes a txt file containing the lines from the letter, with the template items
// replaced with the customer data
public static void generateLetter(String [] letter, int letterSize, String [] data) throws FileNotFoundException {
}
}
Write a in java 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
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. Starter code given
public class MailMerge {
public static final int MAXLETTERSIZE = 15;
public static void main(String [] args) throws FileNotFoundException {
String [] letter = new String[MAXLETTERSIZE];
String letterFileName = args[0];
String dataFileName = args[1];
}
// Generates one form letter for one customer
// letter = an array containing lines of text representing the template letter
// letterSize = the number of lines currently stored in the letter array
// data = an array containing the customer data - one piece of data in each array position
// Output: writes a txt file containing the lines from the letter, with the template items
// replaced with the customer data
public static void generateLetter(String [] letter, int letterSize, String [] data) throws FileNotFoundException {
}
}
Explanation / Answer
//This code will do the required mail merge functionaity.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MailMerge {
public static int MAXLETTERSIZE =15;
public static void main(String[] args) {
if(args.length==2){
String letterFileName = args[0];
String dataFileName = args[1];
FileReader fileReader = null;
BufferedReader bufferedReader = null;
FileReader fileReader1 = null;
BufferedReader bufferedReader1 = null;
List letterList = new ArrayList();
String line = null;
String[] letter = new String[MAXLETTERSIZE];
int letterSize=0;
//read the letter file
try {
// FileReader reads text files in the default encoding.
fileReader = new FileReader(letterFileName);
// Wrap FileReader in BufferedReader to read line by line
bufferedReader = new BufferedReader(fileReader);
int i=0;
while((line = bufferedReader.readLine()) != null) {
letter[i]=line;
i+=1;
}
letterSize =i;
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + letterFileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + letterFileName + "'");
}
finally {
try {
if (bufferedReader != null)
bufferedReader.close();
if (fileReader != null)
fileReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//read the data file
try {
// FileReader reads text files in the default encoding.
fileReader1 = new FileReader(dataFileName);
// Wrap FileReader in BufferedReader to read line by line
bufferedReader1 = new BufferedReader(fileReader1);
while((line = bufferedReader1.readLine()) != null) {
String[] data = line.split(",");
generateLetter(letter,letterSize,data);
}
// close files
bufferedReader1.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + dataFileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + dataFileName + "'");
}
finally {
try {
if (bufferedReader1 != null)
bufferedReader1.close();
if (fileReader1 != null)
fileReader1.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}else{
System.out.println("PLease enter both file names");
}
}
/**
*
* @param letter
* @param letterSize
* @param data
*/
public static void generateLetter(String[] letter, int letterSize , String[] data){
//create the new file name
String FILENAME = data[1]+".txt";
//Copy the riginal letter template to new specific letter content
String[] content = new String[letterSize];
content=Arrays.copyOf(letter,letterSize);
for(int i=0;i<letterSize;i++){
if(letter[i].contains("<title>")){
content[i]=content[i].replace("<title>", data[0]);
}
if(letter[i].contains("<name>")){
content[i]=content[i].replace("<name>", data[1]);
}
if(letter[i].contains("<balance>")){
content[i]=content[i].replace("<balance>", data[2]);
}
if(letter[i].contains("<days>")){
content[i]=content[i].replace("<days>", data[3]);
}
}
BufferedWriter bw = null;
FileWriter fw = null;
//write to the output file
try {
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
for(int k=0;k<letterSize;k++){
bw.write(content[k]);
}
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.