Contact Agenda Mailer
A console mail merge in Java that keeps an address book and its distribution lists in flat files and turns an HTML template into one personalised letter per contact, over a string type written by hand on top of a character array.
The problem
Project for the Programming I course at the Universitat de les Illes Balears.
Three things had to work from a menu that keeps coming back until you ask to leave: generate letters, manage the address book, and manage the distribution lists.
A contact is a name, a surname, an email address and a phone number. The address book is a flat text file, one contact per line, fields separated by #. A template is an HTML file carrying the same # markers where a contact’s data belongs — #NOMBRE#, #E-MAIL#, #NUM-TEL#. Generating letters means walking a list of contacts and writing one copy of the template each, with the markers replaced. One delimiter does both jobs: it separates the fields in storage and names them in the template.
What makes this a term’s work rather than a short script is the constraint it was set under. None of the string handling could lean on Java’s own String methods. Finding #NOMBRE# inside a line, cutting the line in two around it and splicing a value of a different length into the gap all had to be built from characters upwards.
How it works
- The string type is written from scratch —
Lineais achar[]capped at 255 and a length, carrying every operation the program needs:adicionCaracterandadicionStringto append,detectarCaracterto search from a position,extraccionSubLineato cut a range out,contieneandesIgualto compare, andsustituirSublineato find a sub-line and replace it with another of a different length. That last one is what the whole merge rests on. - A contact knows how to be a line —
Contactoholds fourLineas and converts both ways: joining the fields with#to store them, and walking a stored line looking for the separators to cut them back out. Every read and write goes through that pair, so the file format lives in exactly one place. - Generating letters — the batch gets a name, which becomes a directory, and a template, which is looked up by name. Either the whole address book or one distribution list supplies the recipients, and each gets
<email>.htmlwith the markers substituted. If the template or the list turns out not to exist, the directory that was just created is removed again rather than left behind empty. - Deleting is the awkward one — a line cannot be removed from a text file in place, so deletion copies the file across to a temporary one, skipping the line that matches, and renames the copy over the original. It runs against the address book and against every distribution list the contact appears in, so no list is left pointing at somebody who is gone.
- One class per direction —
ContactoFicherosLecturaandContactoFicherosEscritura, and the same pair forLinea. A read returns the next record or reports that the file is exhausted, which is what lets every caller be a plain loop.
The keyboard-reading library, LT, comes with the course. Everything else is ours.
Running it
Requires Java 17, which is what the project is set to compile against. It is a NetBeans Ant project, so it opens in the IDE directly, or from a terminal:
ant jar
java -jar dist/entregaFinal.jar
Run it from the repository root. Every path in the program is relative — fitxers/agenda.txt, fitxers/plantilles/, fitxers/llistes/ — so from anywhere else the address book is empty and no template is ever found. Generated letters land in fitxers/generats/<batch name>/, one file per recipient.