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

Jonathan Hector
Jonathan Hector
5,225 Points

Asking me to change to SongRequests but still won't work after refactoring as such

I tried doing both Refactoring with SongRequest and SongRequests but still won't work

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.*;

/**
 * @author jonathanhector on 06.01.17.
 */
public class KaraokeMachine {

    private SongBook mSongBook;
    private BufferedReader mReader;
    private Map <String, String> mMenu;
    private Queue <SongRequest> mSongRequestQueue;

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

    }

    private String promptAction() throws IOException {
        System.out.printf("There are %d song available and %d in the queue. Your options are: %n", mSongBook.getSongCount(), mSongRequestQueue.size());
        for(Map.Entry <String, String> option : mMenu.entrySet()) {
            System.out.printf("%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 singerName = promptForSingerName();
                        String artist = promptArtist();
                        Song artistSong = promptSongForArtist(artist);
                        SongRequest SongRequest = new SongRequest(singerName, artistSong);
                        if(mSongRequestQueue.contains(SongRequest)) {
                            System.out.printf("%n%n Whoops %s alread requested %s!", singerName,
                                    artistSong);
                            break;

                        }
                        mSongRequestQueue.add(SongRequest);
                        System.out.printf("You choose: %s %n", artistSong);
                        break;
                    case "play":
                        playNext();
                        break;
                    case "quit":
                        System.out.println("Thanks for playing");
                        break;
                    default:
                        System.out.println("Unknown Choice");

                }
            } catch (IOException ioe) {
                System.out.println("Problem with the input");
                ioe.printStackTrace();
            }

        } while(!choice.equals("quit"));

    }

    private String promptForSingerName() throws IOException {
        System.out.println("Enter the singer 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 url: ");
        String url = mReader.readLine();
        return new Song(artist, title, url);
    }

    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 artists) throws IOException {
        List <Song> songs = mSongBook.getSongsForArtist(artists);
        List <String> songTitle = new ArrayList <>();
        for(Song song : songs) {
            songTitle.add(song.getTitle());
        }
        System.out.printf("Available songs for %s; %n", artists);
        int index = promptForIndex(songTitle);
        return songs.get(index);
    }

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

    public void playNext() {
        SongRequest songRequest = mSongRequestQueue.poll();
        if(songRequest == null) {
            System.out.print("Sorry there are no SongRequest in the queue. Choose choose from the menu to add some");
        } else {

            Song song = songRequest.getSong();
            System.out.printf("%n%n%n Ready %s ? Open %s to hear %s by %s %n%n%n",
                    songRequest.getSingerName(),
                    song.getVideoUrl(),
                    song.getTitle(),
                    song.getArtist());
        }
    }


}

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

It is unbelievable, but if you change this

    private Queue <SongRequest> mSongRequestQueue;

to this

    private Queue<SongRequest> mSongRequestQueue;

I.e. remove SPACE character between Queue and <, it should work ...