Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired 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>();