In: Computer Science
Write a mail merge application titled EmailMerge.java
You will use two files for this program. The first is a text file that contains a template letter.
template.txt
[ Dear <>,
Because you are <> years old and <>, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99.
To claim your gift, call us immediately.
Thank you,
Office of Claims Department ]
The tags <>, <>, and <> are placeholders for the person’s name, age, and gender.
The second file contains the name, age, and gender information of people separated by spaces on each line.
people.txt
[ John 35 male
Sally 28 female
Megan 55 female
Megan 22 female
Sally 18 female
Megan 19 female ]
Your program will read the two files and print out the letter with the placeholders replaced by the actual values from the second file. Save each personalized letter to files. For example John.txt, Sally.txt, and Megan.txt, and so on.
Sample output for John
Dear John,
Because you are 35 years old and male, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99.
To claim your gift, call us immediately.
Thank you,
Office of Claims Department
If two people have the same name, create a new file by appending an index for the new file. For example, if there was another John in people.txt file, you will create John-1.txt for the new personalized letter.Assume that all the files that you read/write are in the same directory or the project build path of EmailMerge.java.
Ensure that your programs work well by checking them against different test cases. At the minimum submit three new and different test case reports with your program submission.
Both txt files must be used. For each individual person there must be a new file created for them. The program should be reading the names directly from people.txt and appropriately assigning them to the template, there shouldn't be any user input.
Please post screenshots of code, output and package explorer.
Code must include comments to explain function of each section.
import java.util.*;
import java.io.*;
public class EmailMerge{
public static void main(String[] args) throws IOException {
Scanner tempFile = new Scanner(new File("template.txt"));
String template = "";
while(tempFile.hasNextLine()){
template += tempFile.nextLine()+"\n";
}
tempFile.close();
Scanner peopleFile = new Scanner(new File("people.txt"));
ArrayList<String> peopleNames = new ArrayList<String>();
while(peopleFile.hasNext()){
String tempCopy = template;
String tempValue = peopleFile.next();
String fileName = tempValue;
if(peopleNames.contains(tempValue)){
int c = 0;
for(int i=0; i<peopleNames.size(); i++){
if(peopleNames.get(i).equals(tempValue))
c++;
}
fileName += "-"+c;
}
peopleNames.add(tempValue);
PrintWriter pw = new PrintWriter(new File(fileName+".txt"));
int pos = tempCopy.indexOf("<>");
while(pos!=-1){
tempCopy = tempCopy.substring(0, pos) + tempValue+tempCopy.substring(pos+2);
pos = tempCopy.indexOf("<>");
if(pos!=-1)
tempValue = peopleFile.next();
}
pw.println(tempCopy);
pw.close();
}
peopleFile.close();
}
}
Let me know if you have any clarifications. Thank you...