CompSci
251:
Fall
2011
Programming
Assignment
#8
Due
Tuesday,
November
29,
11:59pm
Topics
Covered:
Exception
Handling,
Files,
String
For
this
program,
you
will
implement
a
very
simple
mail
merge.
In
a
mail
merge,
a
form
letter
or
similar
document
is
written
with
blanks
at
various
places
(e.g.
first
name,
last
name,
address,
etc.).
This
is
then
merged
with
a
database
containing
entries
appropriate
for
filling
in
the
blanks.
For
simplicity,
we
will
store
both
the
form
letter
and
the
database
as
text
files.
The
Database
For
simplicity,
we
will
use
db
format
to
store
our
database:
this
consists
of
a
text
file
with
exactly
one
database
entry
per
line.
The
entry
is
represented
as
a
colon- separated
list
of
fields.
The
file
below
has
entries
with
three
fields,
title,
first
name,
and
last
name.
names.txt:
:Mr:Wilberforce:Gates: :Ms:Alex:Trumbull: :Dr:Felix:Unger: :Dr:Dre:
Notice
that
your
program
should
work
with
any
file
formatted
as
above--that
is,
you
cannot
know
the
number
of
fields
each
entry
has
in
advance.
You
will
be
responsible
for
writing
the
DbEntry
Class
(UML
below).
Each
DbEntry
object
represents
exactly
one
entry
from
the
database
(line
in
the
db
file).
DbEntry - fields : ArrayList<String> + DbEntry(String) + getFieldfield : int) throws IndexOutOfBoundsException : String + toString() : String
The
constructor
takes
a
string
in
db
format
and
initializes
the
DbEntry
object
accordingly
-
so
that
it
has
the
same
number
of
fields,
with
the
same
values.
The
getField
method
takes
the
index
for
the
field
and
returns
its
value.
IMPORTANT:
DbEntry
fields
are
indexed
starting
at
1,
unlike
ArrayList
elements.
If
there
is
no
entry
at
that
index,
your
implementation
of
getField
throws
an
IndexOutOfBoundsException
(you
can
just
let
the
IndexOutofBoundsException
thrown
by
the
ArrayList
pass
through.).
The
toString
method
returns
the
entry
written
in
db
format.
The
Form
Letter
The
form
letter
will
be
stored
in
a
text
file
that
contains
the
basics
of
the
letter,
but
with
some
blank
slots,
which
we
will
denote
with
two
blocks
of
three
pound
signs.
Thus,
###1###
will
represent
a
blank
to
be
filled
by
the
value
from
the
first
field
of
each
database
entry.
(A
more
sophisticated
mail
merge
program
would
use
named
fields
instead.)
One
example
form
letter
would
be
the
file
sales.txt,
shown
below.
sales.txt:
Dear ###2###, We here at WidgetMatics Widget Works, LLC, are honored to be able to bring you, ###1### ###2### ###3###, the opportunity of a lifetime. Now, ###1### ###3###, you can get in on the ground floor of WidgetMatic Widget Works, LLC's latest expansion---into cyberspace!!! For the insanely low price of $1,000.00, you, ###1### ###2### ###3### can pre-order a case of 50 (yes, FIFTY), of our new virtual widgets. These new virtual widgets can be used for virtually anything. And, at $20 each, they are virtually a steal. Hurry ###2### - quantities will be limited! Regards, Albert Schweitzer WidgetMatics Widget Works, LLC VP of Marketing
You
will
write
a
FormLetter
class,
objects
of
which
represent
form
letters
inside
the
program.
The
UML
for
a
form
letter
is
shown
below:
FormLetter - filename : String + FormLetter(filename : String) + getFilename() : String + fillForm(e : DbEntry, outputFilename : String) : boolean
The
fillForm
method
actually
performs
the
mail
merge,
taking
the
DbEntry
that
will
be
used
to
fill
the
blanks
and
the
name
of
the
resulting
output
file
and
producing
the
appropriate
output
file.
Several
things
could
go
wrong
in
this
method.
Your
program
must
handle
every
exception
that
might
be
thrown
by
the
Java
API
classes
that
you
are
using
and
by
the
DbEntry
class.
The
possible
exceptions
are:
One
of
the
files
might
not
open.
In
this
case,
the
method
should
print
an
appropriate
error
message
and
return
false.
If
they
both
open,
the
method
will
return
true
when
it
has
finished
processing
the
form.
The
blank
may
not
be
well-formed.
In
our
implementation,
a
missing
close
###
will
result
in
a
StringIndexOutOfBoundsException.
This
should
print
an
appropriate
error
message
and
replace
the
unmatched
(opening)
###
with
an
empty
string.
(There
are
ways
to
write
the
program
that
avoid
this
exception,
but
they
are
not
straightforward.
So,
we
cant
guarantee
that
youll
encounter
it.)
The
part
between
the
two
###
markers
might
not
be
an
integer
causing
a
NumberFormatException.
In
this
case,
the
blank
should
again
be
replaced
with
the
empty
string
and
an
error
message
printed.
The
integer
index
in
the
blank
might
not
correspond
to
an
actual
field
in
the
DbEntry.
If
the
DbEntry
throws
a
IndexOutOfBoundsException,
we
will
once
more
print
an
error
message
and
replace
the
blank
with
the
empty
string.
Note:
StringIndexOutOfBoundsException
is
a
subclass
of
IndexOutOfBoundsException.
You
may
need
to
keep
this
in
mind
when
ordering
catch
blocks.
The
format
of
each
line
will
not
change
in
the
output
file.
The
only
change
will
be
the
text
substitutions.
The
Driver
Your
driver
will
be
in
a
separate
class
called
MailMerge.
It
will
prompt
the
user
for
both
the
database
filename
and
the
form
filename.
If
the
database
file
cannot
be
opened,
the
user
should
be
allowed
to
re-enter
filenames
until
either
the
open
succeeds
or
the
user
quits.
(This
will
also
require
exception
handling
to
notice
that
the
file
didnt
open.)
Once
both
file
names
are
correctlyentered,
the
program
will
generate
a
merged
output
file
for
each
entry
in
the
database.
The
files
will
be
sequentially-numbered
based
on
the
filename
of
the
original
file.
For
example,
the
form
letter
file
sales.txt
will
produce
output
files
sales1.txt
sales2.txt
etc.
Design
The
public
class
interfaces
have
been
provided
for
you.
However,
several
of
the
tasks
being
performed
are
fairly
complex.
So,
you
may
write
as
many
(private)
helper
methods
as
you
find
convenient.
For
example,
our
solution
has
methods
for
parsing
an
entry
in
db
format
and
for
generating
numbered
output
file
names.
Sample
Runs:
Mail Merge ========== Enter name of database file: names.txt Enter name of form file: coupon.txt Generating form letter #1 ... Could not open file! coupon.txt (No such Unable to generate form letter #1 Generating form letter #2 ... Could not open file! coupon.txt (No such Unable to generate form letter #2 Generating form letter #3 ... Could not open file! coupon.txt (No such Unable to generate form letter #3 Generating form letter #4 ... Could not open file! coupon.txt (No such Unable to generate form letter #4 Merge completed. Goodbye!
file or directory)
file or directory)
file or directory)
file or directory)
Mail Merge ========== Enter name of database file: namz.txt Unable to open file "namz.txt" Enter different filename, or 'q' to quit: namez.txt Unable to open file "namez.txt" Enter different filename, or 'q' to quit: q Goodbye!
Mail Merge ========== Enter name of database file: name.txt Unable to open file "name.txt" Enter different filename, or 'q' to quit: nams.txt Unable to open file "nams.txt" Enter different filename, or 'q' to quit: names.txt Enter name of form file: sales.txt Generating form letter #1 ... Generating form letter #2 ... Generating form letter #3 ... Generating form letter #4 ... 4: No field #3 in entry :Dr:Dre: Using "" 5: No field #3 in entry :Dr:Dre: Using "" 10: No field #3 in entry :Dr:Dre: Using "" Merge completed.
Goodbye!
Mail Merge ========== Enter name of database file: names Unable to open file "names" Enter different filename, or 'q' to quit: names.txt Enter name of form file: offer.txt Generating form letter #1 ... 1: Poorly formatted blank number; Skipping. 1: Poorly formatted blank marker; Skipping. 9: No field #0 in entry :Mr:Wilberforce:Gates: Using "" Generating form letter #2 ... 1: Poorly formatted blank number; Skipping. 1: Poorly formatted blank marker; Skipping. 9: No field #0 in entry :Ms:Alex:Trumbull: Using "" Generating form letter #3 ... 1: Poorly formatted blank number; Skipping. 1: Poorly formatted blank marker; Skipping. 9: No field #0 in entry :Dr:Felix:Unger: Using "" Generating form letter #4 ... 1: Poorly formatted blank number; Skipping. 1: Poorly formatted blank marker; Skipping. 5: No field #3 in entry :Dr:Dre: Using "" 9: No field #0 in entry :Dr:Dre: Using "" 9: No field #3 in entry :Dr:Dre: Using "" Merge completed. Goodbye!
Files
The
files
names.txt
and
sales.txt
used
in
the
sample
runs
are
shown
above.
Here
are
the
other
files
referenced
in
the
sample
runs.
sales1.txt:
Dear Wilberforce, We here at WidgetMatics Widget Works, LLC, are honored to be able to bring you, Mr Wilberforce Gates, the opportunity of a lifetime. Now, Mr Gates, you can get in on the ground floor of WidgetMatic Widget Works, LLC's latest expansion---into cyberspace!!! For the insanely low price of $1,000.00, you, Mr Wilberforce Gates can pre-order a case of 50 (yes, FIFTY), of our new virtual widgets. These new virtual widgets can be used
for virtually anything. And, at $20 each, they are virtually a steal. Hurry Wilberforce - quantities will be limited! Regards, Albert Schweitzer WidgetMatics Widget Works, LLC VP of Marketing
sales2.txt:
Dear Alex, We here at WidgetMatics Widget Works, LLC, are honored to be able to bring you, Ms Alex Trumbull, the opportunity of a lifetime. Now, Ms Trumbull, you can get in on the ground floor of WidgetMatic Widget Works, LLC's latest expansion---into cyberspace!!! For the insanely low price of $1,000.00, you, Ms Alex Trumbull can pre-order a case of 50 (yes, FIFTY), of our new virtual widgets. These new virtual widgets can be used for virtually anything. And, at $20 each, they are virtually a steal. Hurry Alex - quantities will be limited! Regards, Albert Schweitzer WidgetMatics Widget Works, LLC VP of Marketing
sales3.txt:
Dear Felix, We here at WidgetMatics Widget Works, LLC, are honored to be able to bring you, Dr Felix Unger, the opportunity of a lifetime. Now, Dr Unger, you can get in on the ground floor of WidgetMatic Widget Works, LLC's latest expansion---into cyberspace!!! For the insanely low price of $1,000.00, you, Dr Felix Unger can pre-order a case of 50 (yes, FIFTY), of our new virtual widgets. These new virtual widgets can be used for virtually anything. And, at $20 each, they are virtually a steal. Hurry Felix - quantities will be
limited! Regards, Albert Schweitzer WidgetMatics Widget Works, LLC VP of Marketing
sales4.txt:
Dear Dre, We here at WidgetMatics Widget Works, LLC, are honored to be able to bring you, Dr Dre , the opportunity of a lifetime. Now, Dr , you can get in on the ground floor of WidgetMatic Widget Works, LLC's latest expansion---into cyberspace!!! For the insanely low price of $1,000.00, you, Dr Dre can pre-order a case of 50 (yes, FIFTY), of our new virtual widgets. These new virtual widgets can be used for virtually anything. And, at $20 each, they are virtually a steal. Hurry Dre - quantities will be limited! Regards, Albert Schweitzer WidgetMatics Widget Works, LLC VP of Marketing
offer.txt:
Hello ###1## ###name### So ###2### you are wondering where your money went. We at Fourteenth National Bank now offer you, ###1### ###3### a special service for our best customers. For $29.95 a month, we will track all you're account balances. Just send us all your account infomation, you're social security number, and your mother's maiden name and we will keep ###0### ###3###'s money safe. We look forward to yore business.
offer1.txt:
Hello name
So Wilberforce you are wondering where your money went. We at Fourteenth National Bank now offer you, Mr Gates a special service for our best customers. For $29.95 a month, we will track all you're account balances. Just send us all your account infomation, you're social security number, and your mother's maiden name and we will keep Gates's money safe. We look forward to yore business.
offer2.txt:
Hello name So Alex you are wondering where your money went. We at Fourteenth National Bank now offer you, Ms Trumbull a special service for our best customers. For $29.95 a month, we will track all you're account balances. Just send us all your account infomation, you're social security number, and your mother's maiden name and we will keep Trumbull's money safe. We look forward to yore business.
offer3.txt:
Hello name So Felix you are wondering where your money went. We at Fourteenth National Bank now offer you, Dr Unger a special service for our best customers. For $29.95 a month, we will track all you're account balances. Just send us all your account infomation, you're social security number, and your mother's maiden name and we will keep Unger's money safe. We look forward to yore business.
offer4.txt:
Hello name So Dre you are wondering where your money went. We at Fourteenth National Bank now offer you, Dr a special service for our best customers. For $29.95 a month, we will track all you're account balances. Just send us all your account infomation,
you're social security number, and your mother's maiden name and we will keep 's money safe. We look forward to yore business.