import java.io.*; import java.util.*; public class LetterGenerator { private ArrayList text = new ArrayList(40); private String textFileName; private Iterator textLines; private String accountType; public LetterGenerator(String aTextFileName, String anAccountType) { textFileName = aTextFileName; accountType = anAccountType; } public static void main(String[] argv) throws IOException { if (argv.length == 2) (new LetterGenerator(argv[0], argv[1])).run(); else System.out.print("Usage:\njava LetterGenerator textFileName accountTypeToSelect\n"); } private void printEnvelope(BankAccount anAccount) { String line; Name name = anAccount.getName(); Address address = anAccount.getAddress(); System.out.println("____________________________________________________"); System.out.println(""); System.out.println(name.getNameString()); if ((line = address.getLine1()) != null) System.out.println(line); if ((line = address.getLine2()) != null) System.out.println(line); if ((line = address.getLine3()) != null) System.out.println(line); } private void printLetter( BankAccount anAccount ) { Name name = anAccount.getName(); System.out.println("____________________________________________________"); System.out.println(""); System.out.println("Dear " + name.getTitle() + name.getLastName() + ','); System.out.println(""); textLines = text.iterator(); while (textLines.hasNext()) System.out.println(textLines.next()); } private void run() throws IOException { String line; Iterator accounts = BankAccountFile.getCurrentInstance().getAccountsForType(accountType); BufferedReader textStream = new BufferedReader(new FileReader(textFileName)); while ((line = textStream.readLine()) != null) // While not end-of-file text.add((Object)line); textStream.close(); while (accounts.hasNext()) { BankAccount account = (BankAccount)accounts.next(); printEnvelope(account); printLetter(account); } } }