cs106a – Assignment #7 – Task #3

The complete specification of assignment #7 can be found as part of the stream at iTunes.

Implement the FacePamphletDatabase class

The FacePamphletDatabase class is used to keep track of all the profiles in the social network. The class which contains five public entries:

  • A constructor that has no parameters. You can use this to perform any initialization you may need for the database. Note: depending on how you implement the database, it is entirely possible that your constructor may not need to do anything. It’s perfectly fine if that’s the case.
  • An addProfile method that is passed a FacePamphletProfile, and is responsible for adding that profile to the database. Note that profile names are unique identifiers for profiles in the database. In other words, no two profiles in the database should have the same name and the name associated with a profile will never change. So, when a new profile is being added, if there is already an existing profile with the same name, the existing profile should be replaced by the new profile. Note: depending on what data structure you use to keep track of the database, this behavior may actually be quite easy to implement.
  • A getProfile method that takes a name, looks it up in the database of profiles, and returns the FacePamphletProfile with that name, or null if there is no profile with that name.
  • A deleteProfile method that takes a profile name, and deletes the profile with that name from the profile database. Note that when we delete a profile from the database, we not only delete the profile itself, but we also update all other profiles in the database so as to remove the deleted profile’s name from any friends lists in other profiles. In this way, we ensure that someone cannot be friends with a person who does not have a profile in the database.
  • A containsProfile method that takes a profile name, and returns true if there is a profile with that name in the database. Otherwise, it returns false.

To test this part of the program, you can add code to the FacePamphlet program so that it creates the FacePamphletDatabase and then change the code for the Add, Delete, and Lookup button handlers as follows:

  • Entering a name in the Name text field and clicking the Add button looks up the current name in the database to see if a profile with that name already exists. If the name does not exist, then it adds a new profile to the database and prints out “Add: new profile: ” followed by the string version of the profile (using the toString method of the FacePamphletProfile). If the profile name already exists in the database, then it prints out the fact that the profile with that name already exists followed by the string representation of the profile.
  • Entering a name in the Name text field and clicking the Delete button looks up the current name in the database to see if it exists. If the name does exist, then it deletes the profile with that name from the database and prints out that the profile was deleted. If the profile name does not exist in the database, then it simply prints out that a profile with the given name does not exist.
  • Entering a name in the Name text field and clicking the Lookup button looks up the current name in the database to see if it exists. If the name does exist, then prints out “Lookup: ” followed by the string version of the profile. If the name does not exist, then it prints out that a profile with the given name does not exist.

Add a map to hold the data – where the key equals the lower case name and the value equals the specific profile – and initialize it in the constructor.

        private HashMap<String,FacePamphletProfile> data;

	public FacePamphletDatabase() {
		data = new HashMap<String,FacePamphletProfile>();
	}

… adding just adds the given profile to the data map using the lower case name as key:

	public void addProfile(FacePamphletProfile profile) {
		data.put(profile.getName().toLowerCase(), profile);
	}

.. return the profile if available:

	public FacePamphletProfile getProfile(String name) {
		if (!containsProfile(name)) return null;
		return data.get(name.toLowerCase());
	}

… removing the profile itself is only a single line. To remove a name from all other profiles it is necessary to loop over their friend lists. To simplify the code its better to change the previous list to a map to be able to find all names even if the user different types of lower and upper case versions:

	public void deleteProfile(String name) {
		if (!containsProfile(name)) return;
		data.remove(name.toLowerCase());
		Iterator<String> it = data.keySet().iterator();
		while (it.hasNext()) {
			data.get(it.next()).removeFriend(name);
		}		
	}

// FacePamphletProfile
	private HashMap<String,String> friends;

	public FacePamphletProfile(String name) {
		...
		friends = new HashMap<String,String>();
	}

	public boolean addFriend(String friend) {
		String key = friend.toLowerCase();
		if (friends.containsKey(key)) return false;
		friends.put(key, friend);
		return true;
	}

	public boolean removeFriend(String friend) {
		String key = friend.toLowerCase();
		if (!friends.containsKey(key)) return false;
		friends.remove(key);
		return true;
	}

	public Iterator<String> getFriends() {
		return friends.keySet().iterator();
	}

	public String toString() {
		...
			result += next + friends.get(it.next());
		...
	}

… finding if a profile exisists is again a single line:

	public boolean containsProfile(String name) {
		return data.containsKey(name.toLowerCase());
	}

In the main class add a new instance variable for the database and initialize it:

	private FacePamphletDatabase data;

	public void init() {
		data = new FacePamphletDatabase();
	        ...
	}

… and add your test code:

    public void actionPerformed(ActionEvent e) {
    	Object source = e.getSource();
    	if (source == addButton) {
    		if (!emptyTextField(nameField)) {
    			String name = nameField.getText();
    			if (data.containsProfile(name)) {
    				println("Add: profile for " + name 
                                + " already exists: " + data.getProfile(name));
    			} else {
    				data.addProfile(new FacePamphletProfile(name));
    				println("Add: new profile: " + data.getProfile(name));
    			}
    		}    		
    	} else if (source == deleteButton) {
    		if (!emptyTextField(nameField)) {
    			String name = nameField.getText();
    			if (data.containsProfile(name)) {
    				data.deleteProfile(name);
    				println("Delete: profile of " + name + " deleted");
    			} else {
    				println("Delete: profile with name " + name + " does not exist");
    			}
    		}    		    		
    	} else if (source == lookupButton) {
    		if (!emptyTextField(nameField)) {
    			String name = nameField.getText();
    			if (data.containsProfile(name)) {
    				println("Lookup: " + data.getProfile(name));
    			} else {
    				println("Lookup: profile with name " + name + " does not exist");
    			}
    		}    		    		
    	} ...
    }

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *