Java Java Data Structures Efficiency! Using a Map to store Contact Methods

stuck on Java Data structures using a map to store contact methods task 3 of 3

i'm stuck on on this code challenge but i don't know what to do for that last part it's asking to return the value of the passed in methodName so i tried this

public String getContactInfo(String methodName) {
    // FIXME: return the value for the passed in *methodName*
    return methodName;
  }

but when i clicked check my work it is saying Bummer! I called addContactMethod with "pager" and "(222) 222-2222", and expected it to be added to mContactMethods, but it was not.

so how would i go about doing this task ? any help would be nice.

package com.example.model;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;

public class Contact {
  private String mFirstName;
  private String mLastName;
  private Map<String, String> mContactMethods;

  public Contact(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
    /* This stores contact methods by name
     * eg:  "phone" => "(555) 555-1234"
     */
    mContactMethods = new HashMap<String, String>();
  }

  public void addContactMethod(String method, String value) {
    mContactMethods.put(method, value);
  }

  /**
   * Returns the available contact methods.  eg: phone, pager,
   *
   * @return The name of the contact methods that are available
   */
  public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set keys = mContactMethods.keySet();
    return keys;
  }

  /**
   * Returns the value for the contact method if it exists, 
   *
   * @param methodName  The name of the contact method to look up.
   * @return The name of the contact methods that are available
   */
  public String getContactInfo(String methodName) {
    return null; // What would i need to do for this part ? 
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}

3 Answers

Dan Johnson
Dan Johnson
38,543 Points

The methodName parameter represents a key in the mContactMethods map. You can get values with a key with the get method.

so could you give me an example in code so i can get a better understanding.

Dan Johnson
Dan Johnson
38,543 Points

This is all there is to it:

mContactMethods.get(methodName);

That will get you your return value.

Brody Ricketts
Brody Ricketts
15,612 Points

I wrote this up a bit ago in response to another thread about this challenge.

I hope this helps if you're still a bit confused.

// Task 3 "For this task, let's fix getContactInfo method."
public String getContactInfo(String methodName) {
    /*  
     *  FIXME: return the value for the passed in *methodName*
     *  
     *  The task here is the pull the value from a stored key(contact method).
     *  Remember when pulling from a HashMap we can use the method .get();
     *  We know that the name of the HashMap is mContactMethods(check line 10 and 18)
     *  We also know that the syntax looks much like mMapName.get();
     *  Maps 4:05
     *
     *  A simple analogy that I hope helps you complete this task:
     *  You have a bowl from fruit on a table(mFruitBowl), you want to GET a piece of fruit(getFruit();). 
     *  You decide to GET an apple. Apple has become your parameter,
     *  Remember you have to pass apple into mFruitBowl.get();, because that is where its stored, much like a HashMap.
     */ 
    return null;
}

Thanks for your help I have passed the code challenge and thank u for posting that code now I understand it a bit more.☺

Shadd Anderson
Shadd Anderson
Treehouse Degree Mentor

Points to Brody for the excellent analogy! :)

mongst
mongst
10,344 Points

This is what I used to solve:

html
package com.example.model;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;

public class Contact {
  private String mFirstName;
  private String mLastName;
  private Map<String, String> mContactMethods;

  public Contact(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
    /* This stores contact methods by name
     * eg:  "phone" => "(555) 555-1234"
     */
    mContactMethods = new HashMap<String, String>();
  }

  public void addContactMethod(String method, String value) {
    mContactMethods.put(method, value);
  }

  /**
   * Returns the available contact methods.  eg: phone, pager,
   *
   * @return The name of the contact methods that are available
   */
  public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set keys = mContactMethods.keySet();
    return keys;
  }

  /**
   * Returns the value for the contact method if it exists, 
   *
   * @param methodName  The name of the contact method to look up.
   * @return The name of the contact methods that are available
   */
  public String getContactInfo(String methodName) {
    return mContactMethods.get(methodName);
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}