Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Efficiency! Changing Course

Getting Bummer

I tried with this code but getting error: Hmmm...I found the old value 'Typo Casting' and the new value 'Type Casting'. You should 'get' the video from the map and update the video using 'setTitle'

com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }

  public void addVideo(int index, Video video){
    mVideos.add(index, video);
  }
}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

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

public class QuickFix {
  private Map<String, Video> byTitle  = new HashMap<String, Video>();
  private Map<String, Video> fixTitle = new HashMap<String, Video>();
  public void addForgottenVideo(Course course) {
    Video video = new Video("The Beginning Bits");
    course.addVideo(1, video);
  }

  public Map<String, Video> videosByTitle(Course course){
    for(Video video : course.getVideos()){
      byTitle.put(video.getTitle(), video);
    }
    return byTitle;
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {  
   fixTitle = videosByTitle(course);
    if(fixTitle.containsKey(oldTitle)){
      Video video = fixTitle.get(oldTitle);
       video.setTitle(newTitle);
    }
  }

}

1 Answer

Your code looks good for the most part, if I am not mistaken you only need to create one Map.

This is how I would write it:

 public Map<String, Video> videosByTitle(Course course){
  Map<String, Video> byTitle  = new HashMap<String, Video>();  //create the map locally within the function
  for(Video video : course.getVideos()){
      byTitle.put(video.getTitle(), video);
    }
    return byTitle;
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {  
   Map<String, Video> videoMap = videosByTitle(course);

   if(videoMap.containsKey(oldTitle)){
      Video video = videoMap.get(oldTitle);
       video.setTitle(newTitle);
    }
  }

I would delete theses lines at the top of your QuickFix.java file

private Map<String, Video> byTitle  = new HashMap<String, Video>();
private Map<String, Video> fixTitle = new HashMap<String, Video>();