Question
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
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
Your starter code includes a file named collection1.txt. The contents of this file is as follows:
Dear
In the letter, the codes
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:
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 "
Using the replace method from the String class, replace "
Using the replace method from the String class, replace "
Using the replace method from the String class, replace "
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).
Remember that at this stage, COMMENTING IS A REQUIREMENT! Make sure you FULLY comment your program. You must include comments that explain what sections of code is doing. Notice the key word "sections"! This means that you need not comment every line in your program. Write comments that describe a few lines of code rather than "over-commenting" your program.
You MUST write comments about every method that you create. Your comments must describe the purpose of the method, the purpose of every parameter the method needs, and the purpose of the return value (if the method has one).
Build incrementally. Don't tackle the entire program at once. Write a little section of the program, then test it AND get it working BEFORE you go any further. Once you know that this section works, then you can add a little more to your program. Stop and test the new code. Once you get it working, then add a little bit more.
Make sure you FULLY test your program! Make sure to run your program multiple times, inputting combinations of values that will test all possible conditions for your IF statements and loops. Also be sure to test border-line cases.
Testing Your Program
After you compile your program, you will run your program by typing the name of your program, followed by the name of the letter file and the name of the data file as follows:
MailMerge collection1.txt collectionNames1.txt
You may create your own letter and data files to test with if you like. If you do so, simply provide the correct file names when you go to execute your program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started