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

How do I do this Challenge??

Hi there, I thought I had this challenge beat, but it ended up not working, could someone give me a hand. Thanks!

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;
  }

}
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;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video vid = new Video("theBeginningBits");
    // TODO(2):  Add the newly created video to the course videos as the second video.

  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

  }

  public Map<String, Video> videosByTitle(Course course) {
    return null;
  }

}

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Gabriel,

I'll break this challenge down for you. (1) so you successfully created a vid object but now you have to add it to a Course. If you look at the Course.java file, you can see that it just contains a string as a title and a list of Videos. Basically what we want to do is add our vid object to that list of Videos which is in a Course object.

Craig wrote that I'm going to pass in this course to the addForgottenVideo method. in the addForgottenVideo you can see that it has a Course object as a parameter. that means we can simply use a method of that course object that will allow us to get the list of the videos and then add our video. It has a method called getVideos() which returns a list of the videos. Then we can chain it with the method .add(1, video). there is a 1 (index based) there because in the todo it says ** TODO(2): Add the newly created video to the course videos as the second video.**. The solution is below if your still confused.

public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
Video vids= new Video("The Beginning Bits");
    // TODO(2):  Add the newly created video to the course videos as the second video.
course.getVideos().add(1, vids);
  }

(2) For this, we want to loop through the video list in the course object that Craig will pass in. First instantiate a new Map. Using the same method as above, we want to get all the videos from the course object. We then loop through that and save it as a Video object. Then inside the loop, get the title of each video and store it as a string. Finally place that title as the fire parameter of the map and the current video we're looping as the second parameter of the map. In case it's still confusing there is the solution below.

public Map<String, Video> videosByTitle(Course course) {
    Map<String, Video> vidMap = new TreeMap<>();
    for (Video currentVids : course.getVideos()) {
        String title = currentVids.getTitle();
         vidMap.put(title, currentVids);
    } 

    return vidMap;
  }

(3) for this, we just simply loop through each video object. we call the getTitle() method each time it loops and check if it equals our "oldTitle" string that we pass in. If they equal each other, then use the setTitle() method to change the title to the "newTitle" string.

for (Video videos : course.getVideos()) {
      if (videos.getTitle() == oldTitle) {
       videos.setTitle(newTitle); 
      }
    }

I hope that helped and dont forget to mark as best answer so others can see this as well.

Happy coding,

Kevin

BENJAMIN UWIMANA
PLUS
BENJAMIN UWIMANA
Courses Plus Student 22,636 Points

For the challenge number 3, the following also works:

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

given:

public Map<String, Video> videosByTitle(Course course) { Map<String, Video> videosMap = new TreeMap<>(); for (Video video : course.getVideos()) { String title = video.getTitle(); videosMap.put(title, video); } return videosMap; }