Question

In: Computer Science

Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first...

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.

Solutions

Expert Solution

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...


Related Solutions

C# Simple Text File Merging Application - The idea is to merge two text files located...
C# Simple Text File Merging Application - The idea is to merge two text files located in the same folder to create a new txt file (also in the same folder). I have the idea, and I can merge the two txt files together, however I would like to have an empty line between the two files. How would I implement this in the code below? if (File.Exists(fileNameWithPath1)) { try { string[] file1 = File.ReadAllLines(fileNameWithPath1); string[] file2 = File.ReadAllLines(fileNameWithPath2); //dump...
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
Creation of Program Application (Development Task 1) This program should create two output files. You may...
Creation of Program Application (Development Task 1) This program should create two output files. You may use .txt files. Create the following files: Deposists.txt Withdrawls.txt 1. The program application should have the following inputs: Amount for Deposits (Only Accept Positive Amounts) Amount for Withdrawals (Only Accept Positive Amounts). The subsequent program will subtract the positive amount. 2. The program application should have the following outputs: Deposists.txt Withdrawals.txt Total of Deposits and Withdrawals back to the console
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Write a program in java which has two arrays of size 4 and 5; merge them...
Write a program in java which has two arrays of size 4 and 5; merge them and sort.
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT