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

Challenge changing course 3 of 3

Can't figure how to update the oldtitle with the new title in this problem.

Can anyone help me with this problem?

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Ethan these challenges are not easy are they? I struggled with this one for a while, I think it's hard as you're aren't really sure what you should be doing (I still don't know if I did coding i wasn't suppose to) but I passed it with the below code

COURSE CLASS

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 setVideos(List<Video> list) {

    mVideos = list;

  }

}

And then the QUICKFIX CLASS

import com.example.model.Course;
import com.example.model.Video;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video video = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.
    course.getVideos().add(1,video);

  }

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

    Map<String, Video> myMap = videosByTitle(course);
    Video video = myMap.get(oldTitle);
    video.setTitle(newTitle);
    myMap.put(newTitle, myMap.remove(oldTitle));
    List<Video> videoList = new ArrayList<Video>(myMap.values());
    course.setVideos(videoList);

  }

  public Map<String, Video> videosByTitle(Course course) {

    Map<String, Video> myMap = new HashMap<String, Video>();

    for(Video video:course.getVideos()){

     myMap.put(video.getTitle(),video);


    }
    return myMap;
  }

}

As you're towards the end of the Java course I assume you know a fair bit, so I won't go over the top explaing what I did but basically I use the method in challenge 2 to obtain a full Map containing all video's then get() the video in question. I then change the value of the title using setTitle() method then reassign this into the map replacing the old one. I then created a new method in the Course class to assign this Map back with the changes (although as the method returns nothing this would be pointless but I didn't know how they were checking what had been changed!!!)

Hopefully this helps

Daniel

Hi Daniel, It is all clear to me now. Wow thanks for your detailed and kind explanation!

Best, Ethan