Created by Prof. Hu

Last updated: 1/26/2019 2:49 PM

The goals for this part of the assignment are to learn about using Iterators, Sets and Maps. Iterators – iterate through a map’s keySet, Sets – build a set, use map keySet, use set methods. Maps – build a map, iterate and process, use keySet operations to affect map.

There are other Java Collections types not presented in the book such as Map.Entry, and you are not allowed to use them. You may only use material presented up to Chapter 11 in the book.

Remember to add Javadoc class headers, and method headers (JavaDoc if lots of parameters, otherwise a simple comment may suffice). I’ve asked for a few specific comments, but you’re responsible for proper commenting & documentation of anything not obvious and labeling all important code blocks like pseudocode. Put all comments including answers to questions immediately above the relevant code. Don’t just put them all into the method header. It’s usually better to comment before you write code than to comment afterwards when you’ve likely forgotten what was important in figuring it out.

This is a real-life example from my experience as webmaster of a club. We use an Excel spreadsheet to keep our membership database. We also use Evite to send out monthly meeting invites. Unfortunately, Evite gets “out-of-sync” with the Excel sheet. So occasionally I had to scan through and manually mark up new addresses to add, and old addresses to delete. So, I wrote a program to do this comparison for me. The inputs are the 2 CSV files (excel.csv and evite.csv), and the output is a list of actions I need to take. Your program should reconcile these two files to generate evites for the members if they are not already in the evite file, and, remove evites for the members if those members are not in the members file.

1. Create a function called readNamesMap(String filename) that reads the members from a CSV files and generates a sorted Map with <key = email name, value = Person >. The email is used as a key because emails are unique. Make sure to declare any return types or variable types as the abstract form (List, Map) but when you create it you must create a subclass (ArrayList, HashMap).

2. Add a call in the method PartB() to call the readNamesMap (“Excel.csv”) and store what is being returned as “excelMap”. Print “Read %d names from Excel Membership List” and verify 100 names read.

3. Add a call to PartB() to call readNamesMap (“Evite.csv”) and store that as “eviteMap”. Print “Read

%d names from Excel Membership List” and verify 101 names read.

4. Use Iterator & manually generate Evite adds

4.1. You need to generate a list of new members who need to be added to Evite. To do this, use a Java Iterator and iterate every item in excelMap. Review the syntax to iterate over a map’s keySet in the book. Recall that Iterators are parameterized (need <>) and you need to iterate over the keys of the map – email names. Intelligently name all variables.

4.2. You should always store the result of your iterator as a local variable of that parameterized type. In this case, you want to name your variable something like keyExcel. This makes the code equivalent to a for-each loop except the prohibition on modification is removed.

4.3. In the past, we’ve looped through each member of a collection to see if it’s in there. But that’s very inefficient. What method on the Set class can check if a key is in it? Even better, what method on the Map class can check if a key is in its keySet? Use either method to check if the Excel email is in the Evite email keyset. This is where the choice of a TreeSet or HashSet comes into play. A HashSet can lookup a string without having to search at all, whereas a TreeSet requires searching proportional to the depth of the required tree. In this case, we don’t need to iterate all the items in the Set in alphabetical order. A Hash table’s quick lookup is actually better. We’ll learn more about Binary Tree efficiency later in the book. If it isn’t in it then you need to add this name to Evite.

Create a new ArrayList of Persons at the beginning of partB which will hold all the changes that need to be made. This array will be used for both this & the next section – only create one.

Add this person to the changes list. Use the member’s first & last name. But for the email please add“+Evite” to the end.

At the end of partB() print out each item in the changes list on its own line. Verify these two people need to be added to Evite.

Person[firstName=Louisa,lastName=Cronauer,email=+E*[email protected]+Evite]

Person[firstName=Alease,lastName=Buemi,email=+E*[email protected]+Evite]

5. Use Set operations to generate Evite removes

5.1. Using an iterator was a bit of extra work with the while loop and was no advantage since we didn’t need to remove anything. There’s a much simpler way. We can use set operations on the maps to do the work for us.

If you want to know what email names need to be removed from Evite, you can start with Evite’s keyset, and removeAll the keyset of Excel. What’s left must be old members who dropped and need to be removed.

One big caveat is that Set operations (union, intersection, difference) performed on a map’s keySet actually affect the underlying Map. Java refers to this as “the Set is backed by the Map”. If you don’t operate on a copy of the keyset you’ll actually destroy the original map. Note that in this case the Excel keyset won’t be modified, but Evite keyset will.

To prevent from destroying Evite’s keyset, make a copy first. Here’s an example of how to do this using the set constructor which can take a set.

Set<String> newSet = new HashSet<String>(oldMap.keySet());

Add this person to the changes list. Use the member’s first & last name. But for the email please add “- Evite” to the end.

Verify these people need to be removed from Evite.

Person[firstName=Oretha,lastName=Menter,email=E*[email protected]]

Person [firstName=Ty, lastName=Smith, email=-E*[email protected]]

Person [firstName=Kris, lastName=Marrier, email=-E*[email protected]]

6. At the bottom of partB() add a call to testPartB() passing in your new change List.

If you have any errors, double check things

Did you read the right number of items from the source CSV files?

Did you find the removes/adds? If not, use the debugger to see why not.

Did you add the Person to the changes list?

Did you make sure to add the right suffix such as “+Evite”?

7. Submit your project