IS 313: Data Structures in Java
Autumn 2002
Individual Assignment 2
Using collection classes and iterators
Due Friday October 11 before 11:30pm
Sometimes not-for-profit institutions need to know more about their
donors than simply who are the largest donors. For example, it may be
useful to know where the biggest donors live or why they made
donations. For this next assignment, you will work on an expanded
version of the program from the last assignment. First, you will
write an expanded Donations class that reads and updates the donations
file. Second, you will work on a new class that reads in an expanded
names file that includes the donor's city and what plays he or she
attended. This information will allow your program to present
donation totals by city or by play. Finally, you will write a simple
interface that allows a user to interact with the program.
Part 1
Create a new class called SavedDonations that extends the Donations class. This class should provide two new methods:
- read -- This method takes a file name (a string) and reads in the specified donations file. It should also save the file name for the write method.
- write -- Without an argument, this method saves the updated donations list to the same file that the donations list was read from. With a string argument, it writes the donations list to the file named by the argument.
Because the Donations class does not keep a record of each individual
donation, your extended class must store each donation (i.e. the ID
and the amount) as it is read in or added to the list. This way, it
can then write all donations back to the file when the write method is
called. In order to store all donations, you need to override the
addDonation method so that it saves all donations.
Additional requirements (really hints)
- Use List objects to store the IDs and donation amounts as they are
being added to the donations collection.
- Your addDonations method should call the addDonations method of
the Donations class (
super.addDonations
) after it has
stored the donation.
- The write method without the argument should call the
write method with the filename argument.
- Write test code for this class and place it in its main method.
Part 2
The names file has been expanded to include the donor's city and which plays the donor attended. Here's an example of the names file using the new format:
Jones, Sam
2
Chicago
The Miser&Tartuffe
Smith, Alfred
3
Oak Park
The Miser&The Imaginary Invalid
Miller, Andrew
5
Evanston
Tartuffe
Note that the play titles are separated by the '&' character since play titles may have spaces in them.
Using the newly formatted names file, create a class called
DonorCollection that reads in this file and provides the following
methods for accessing useful information:
- read -- This method takes a string specifying the file
name. It reads in all of the information in the newly formatted names
file.
- getName -- This method takes an integer that specifies a
donor's ID. It returns the name that corresponds to the ID or returns
null if the donor is not listed in the names file.
- getCities -- This method returns a Set that includes
all of the cities the donors are from.
- getPlays -- This method returns a Set that includes
all of the plays the donors attended.
- getTotalByCity -- This method takes a Donations object and
a string specifying a city. It returns the total amount of the
donations from donors living in the specified city.
- getTotalByPlay -- This method takes a Donations object and
a string specifying a play. It returns the total amount of the
donations from donors who attended the specified play.
Additional requirements (really hints)
- The Donor class constructs objects that hold a donor's
name, id, city and plays attended. Use its objects to store the
contents of the names file.
- So that the donor's name can be referenced efficiently, use a Map
that links a donor's ID (represented as an Integer object) to each
Donor object.
- A skeleton of the DonorCollection class is created for you. It
already has some methods written for you. You need to finish the
remaining methods and complete the comments.
Part 3
Write a simple driver class called DonorAdmin that uses the
SavedDonations class and the DonorCollection class. This
class should provide simple user interaction through command-line
menus that allow a user to do the following items:
- Update the donations list
- Obtain donation totals by city and by play
- Obtain donor lists sorted by number and total amount (like assignment 1)
- Obtain a donor's name given the donor's ID.
Example data files
Here are example data files for donations
and names. You should find that the larger
cities (e.g. Chicago) generally have larger donation totals. You will
also find that one of the plays has produced larger donation totals
too.
Submission
Before the due date and time, you should submit the following files
through the submission
Web page:
- SavedDonations.java
- DonorCollection.java
- DonorAdmin.java
If for some reason you are unable to submit the files, you may email me
them as an attachment.