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 Local Development Environments Advanced Tooling Refactoring

Refactoring challenge does not recognize the other work we completed during the video lesson

After following along in my local environment during the video about refactoring, I have a working project with the new functionality. The challenge asks me to refactor one more thing in a single file (does not let me see the other classes). I complete this, and it works in my local environment, but when I paste in my refactored code, I get:

./com/teamtreehouse/KaraokeMachine.java:142: error: cannot find symbol songRequest.getSinger(), ^ symbol: method getSinger() location: variable songRequest of type SongRequest 1 error

Huh? What is the challenge trying to accomplish? Why is it looking at songRequest? It seems to be attempting to compile the code without letting me add my other refactored files, which apparently differ from what we accomplished in the video lesson. After he just said "I hope you've been following along because we're going to use these changes going forward..."??? Really!!?

So what do I have to do? Back out the lesson's changes and refactor only the one thing it is asking for?

com/teamtreehouse/KaraokeMachine.java
package com.teamtreehouse;

import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;
import com.teamtreehouse.model.SongRequest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;


public class KaraokeMachine {
    private SongBook mSongBook;
    private BufferedReader mReader;
    private Queue<SongRequest> mSongRequestQueue;

    private Map<String, String> mMenu;

    public KaraokeMachine(SongBook songBook) {
        mSongBook = songBook;
        mReader = new BufferedReader(new InputStreamReader(System.in));
        mSongRequestQueue = new ArrayDeque<SongRequest>();
        mMenu = new HashMap<String, String>();
        mMenu.put("add", "Add a new song to the song book");
        mMenu.put("play", "Play next song from queue");
        mMenu.put("choose", "Choose a song to sing!");
        mMenu.put("quit", "Give up. Exit the program");
    }

    private String promptAction() throws IOException {
        System.out.printf("Songs available: %d\tSongs in Queue: %d%nYour options are:%n",
                mSongBook.getSongCount(),
                mSongRequestQueue.size());
        for (Map.Entry<String, String> option : mMenu.entrySet()) {
            System.out.printf("\t%s - %s%n",
                    option.getKey(),
                    option.getValue());
        }
        System.out.print("What do you want to do? ");
        String choice = mReader.readLine();
        return choice.trim().toLowerCase();
    }

    public void run() {
        String choice = "";
        do {
            try {
                choice = promptAction();
                switch (choice) {
                    case "add":
                        Song song = promptNewSong();
                        mSongBook.addSong(song);
                        System.out.printf("%s added! %n%n", song);
                        break;
                    case "choose":
                        String singer = promptForSinger();
                        String artist = promptArtist();
                        Song artistSong = promptSongForArtist(artist);
                        SongRequest songRequest = new SongRequest(singer, artistSong);
                        if (mSongRequestQueue.contains(songRequest)) {
                            System.out.printf("%n%nWhoops! %s already requested %s%n",
                                    singer,
                                    artistSong);
                        }
                        mSongRequestQueue.add(songRequest);
                        System.out.printf("You chose: %s%n", artistSong);
                        break;
                    case "quit":
                        System.out.println("Thanks for playing!");
                        break;
                    case "play":
                        playNext();
                        break;
                    default:
                        System.out.printf("Unknown choice: '%s'. Try again. %n%n%n", choice);
                }
            } catch (IOException ioe) {
                System.out.println("Problem with input");
                ioe.printStackTrace();
            }
        } while (!choice.equals("quit"));
    }

    private String promptForSinger() throws IOException {
        System.out.print("Enter the singer's name: ");
        return mReader.readLine();
    }

    private Song promptNewSong() throws IOException {
        System.out.print("Enter the artist's name: ");
        String artist = mReader.readLine();
        System.out.print("Enter the title: ");
        String title = mReader.readLine();
        System.out.print("Enter the video URL: ");
        String videoUrl = mReader.readLine();
        return new Song(artist, title, videoUrl);
    }

    private int promptForIndex(List<String> options) throws IOException {
        int counter = 1;
        for (String option : options) {
            System.out.printf("%d.) %s%n", counter, option);
            counter++;
        }
        System.out.print("Your choice: ");
        String optionAsString = mReader.readLine();
        int choice = Integer.parseInt(optionAsString.trim());
        return choice - 1;
    }

    private String promptArtist() throws IOException {
        System.out.println("Available artists:");
        List<String> artists = new ArrayList<>(mSongBook.getArtists());
        int index = promptForIndex(artists);
        return artists.get(index);
    }

    private Song promptSongForArtist(String artist) throws IOException {
        List<Song> songs = mSongBook.getSongsForArtist(artist);
        List<String> songTitles = new ArrayList<>();
        for (Song song : songs) {
            songTitles.add(song.getTitle());
        }
        System.out.printf("Available songs for artist %s:%n", artist);
        int index = promptForIndex(songTitles);
        return songs.get(index);
    }

    public void playNext() {
        SongRequest songRequest = mSongRequestQueue.poll();
        if (songRequest == null) {
            System.out.println("Sorry there are no songs in the queue. Use 'choose' to enqueue something to play...");
        } else {
            Song song = songRequest.getSong();
            System.out.printf("%n%n%n Are you ready %s? Open %s to hear %s by %s %n%n%n",
                    songRequest.getSinger(),
                    song.getVideoUrl(),
                    song.getTitle(),
                    song.getArtist());

        }
    }
}