cs106a – Assignment #7 – Task #4

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

Implement functionality for Change Status, Change Picture, and Add Friend buttons

The next step in the process is to complete more of the implementation of the FacePamphlet class, namely the functionality for the Change Status, Change Picture, and Add Friend buttons. The main issue to remember here is that these buttons effect the current profile, if there is one. As a result, one of the first things you should think about in implementing this milestone is how you will keep track of the current profile in the application. To help introduce the notion of the current profile, you might want to update the code for the Add, Delete, and Lookup button handlers so that:

  • Whenever a new profile is added, the current profile is set to be the newly added profile. If the user tried to add a profile with the name of an existing profile, then the existing profile with that name is set to be the current profile (this is similar to the case below where the users simply looks up an existing profile).
  • Whenever a profile is deleted (whether or not the profile to be deleted exists in the database), there is no longer a current profile (regardless of what the current profile previously was).
  • Whenever the user lookups up a profile by name, the current profile is set to be the profile that the user looked up, if it exists in the database. If a profile with that name does not exist in the database, then there is no longer a current profile (regardless of what the current profile previously was).

Create a new instance variable to hold the current profile, and reset it:

	private FacePamphletProfile profile;

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

When adding a new profile set the current profile to the old one if it already existed, otherwise to the new one:

    			String name = nameField.getText();
    			if (data.containsProfile(name)) {
    				profile = data.getProfile(name);
    				println("Add: profile for " + name + " already exists: " + profile);
    			} else {
    				profile = new FacePamphletProfile(name);
    				data.addProfile(profile);
    				println("Add: new profile: " + profile);
    			}

When deleting a profile, reset the current profile:

    			profile = null;

For a lookup set the current profile to the existing profile, or reset it if such a profile does not exist:

    			String name = nameField.getText();
    			if (data.containsProfile(name)) {
    				profile = data.getProfile(name);
    				println("Lookup: " + profile);
    			} else {
    				println("Lookup: profile with name " + name + " does not exist");
    				profile = null;
    			}

Implementing Change Status

If the user enters some text in the text field associated with the Change Status button andeitherpressestheChange StatusbuttonorhitsEnter,theapplicationshouldupdate as follows:

  • If there is a current profile, then the status for that profile should be updated to the text entered, and you can just print out a message to that effect.
  • If there is no current profile, then you should simply prompt the user to select a profile to change the status of (and there should be no changes to any of the profiles in the database).

If a profile is available change the status otherwise do “nothing”:

    			if (profile != null) {
    				String status = statusField.getText();
    				profile.setStatus(status);
    				println("Status updated to " + status);
    				println("--> Current profile: " + profile);
    			} else {
    				println("Please select profile to change status");
    				println("--> No current profile");
    			}    			

Implementing Change Picture

If the user enters some text in the text field associated with the Change Picture button and either presses the Change Picture button or hits Enter, the application should update as follows:

  • If there is a current profile, then we need to see if we can create a GImage with the filename of the text entered in the text field. Checking to see if a valid image file exists can be accomplished using the code fragment below (where filename is a String containing the name of the image file we are trying to open):
            GImage image = null;
            try {
                image = new GImage(filename);
            } catch (ErrorException ex) {
                // Code that is executed if the filename cannot be opened. 
            }
    

    Note in the code fragment above that the variable image will still have the value null if we were unable to open the image file with the given filename. Otherwise, the value of the variable image will be a valid GImage object (whose value will not be null).
    If we obtained a valid GImage, then the image for the current profile should be updated to this image, and you can print out a message to that effect (although you won’t be able to display the actual image for now).

  • If there is no current profile, then you should simply prompt the user to select a profile to change the image of (and there should be no changes to any of the profiles in the database).

If there is a current profile and a valid picture add it otherwise prompt accordingly:

				if (profile != null) {
					String filename = pictureField.getText();    			
					GImage image = null;
					try {
						image = new GImage(filename);
					} catch (ErrorException ex) {
						// Code that is executed if the filename cannot be opened. 
					}
					if (image != null) {
						profile.setImage(image);
						println("Picture updated to " + filename);
						println("--> Current profile: " + profile);
					} else {
						println("Please use a valid picture");
						println("--> Current profile: " + profile);
					}
				} else {
					println("Please select profile to change picture");
					println("--> No current profile");
				}    			

Implementing Add Friend

If the user enters some text in the text field associated with the Add Friend button and either presses the Add Friend button or hits Enter, the application should update as follows:

  • If there is a current profile, then we need to see if the name entered in the text field is the name of a valid profile in the database. If it is, then we try to add the named friend to the list of friends for the current profile. If the named friend already exists in the list of friends for the current profile, then we simply write out a message that such a friend already exists. If that named friend does not previously exist in the list of friends (i.e., it was successfully added to the list of friends for the current profile), then (recalling that friendships are reciprocal) we also need to update the profile of the named friend to add the name of the current profile to its list of friends. For example, if the current profile was “Mehran” and we tried to add as a friend “Julie” (which, say, is the name of valid profile in the database, which is not already a friend of Mehran), then we should add Julie as a friend of Mehran and also add Mehran as a friend of Julie.
  • If the name entered in the Add Friend text field is not a valid profile in the system, we should just print out a message to that effect.
  • If there is no current profile, then you should simply prompt the user to select a profile to add a friend to (and there should be no changes to any of the profiles in the database).

If there is a current profile and a the entered friend has a valid profile of her/his own, add the friend. If the friend did not already exist add the name of the current profile as friend to the friends profile:

				if (profile != null) {
					String friend = friendField.getText();
					if (data.containsProfile(friend)) {
						if (profile.addFriend(friend)) {
							data.getProfile(friend).addFriend(profile.getName());
							println(friend + " added as friend");
						} else {
							println("Friend with name " + friend + " already exists");
						}
					} else {
						println("profile with name " + friend + " does not exist");
					}
					println("--> Current profile: " + profile);
				} else {
					println("Please select profile to add friend");
					println("--> No current profile");					
				}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.