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 trialGarvit Kashyap
2,918 PointsError Message in Refactoring Code Problem
Why is this error being displayed?
package com.teamtreehouse.model;
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 Map<String, String> mMenu;
private Queue<SongRequest> mSongRequestQueue;
public KaraokeMachine(SongBook songbook) {
mSongbook = songbook;
mReader = new BufferedReader(new InputStreamReader(System.in));
mMenu = new HashMap<String, String>();
mSongRequestQueue = new ArrayDeque<SongRequest>();
mMenu.put("Add", "Add a new song to the song book\n");
mMenu.put("Play", "Play next song in queue\n");
mMenu.put("Choose", "Choose a song to sing\n");
mMenu.put("Quit", "Not in the mood to sing\n");
}
private String promptAction() throws IOException {
System.out.printf("There are %d songs in the song book and %d in the queue. Your options are: \n", mSongbook.songCount(), mSongRequestQueue.size());
for (Map.Entry<String, String> entry : mMenu.entrySet()) {
System.out.format("%s-%s", entry.getKey(), entry.getValue());
}
System.out.println("What do you want to do ?\n");
String choice = mReader.readLine();
return choice.trim().toLowerCase();
}
public void run() {
String choice = "add";
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);
//TODO: Add song to queue
SongRequest songRequest = new SongRequest(singerName, artistSong);
mSongRequestQueue.add(songRequest);
if (mSongRequestQueue.contains(songRequest)) {
System.out.printf("\n\n %s Already Requested %s\n\n",
singerName,
artistSong);
break;
}
System.out.printf("You chose: %s\n", artistSong);
case "play":
playNext();
break;
case "quit":
System.out.println("Thank you for playing...");
break;
default:
System.out.printf("Invalid Choice %s, Try Again", choice);
}
} catch (IOException ioe) {
System.out.println("Problem getting input...");
ioe.printStackTrace();
}
} while (!choice.equals("quit"));
}
private String promptForSingerName() throws IOException {
System.out.print("Enter the singer's name: ");
return mReader.readLine();
}
private Song promptNewSong() throws IOException {
System.out.print("Enter artist's name: \n");
String artist = mReader.readLine();
System.out.print("Enter song title: \n");
String title = mReader.readLine();
System.out.print("Enter video URL: \n");
String videoURL = mReader.readLine();
return new Song(artist, title, videoURL);
}
private String promptArtist() throws IOException {
System.out.println("Available artists: ");
List<String> artists = new ArrayList<String>(mSongbook.getArtists());
int index = promptForIndex(artists);
return artists.get(index);
}
private Song promptSongForArtist(String artist) throws IOException {
List<Song> songs = mSongbook.getSongForArtists(artist);
List<String> songTitles = new ArrayList<>();
for (Song song : songs) {
songTitles.add(song.getTitle());
}
System.out.printf("Available songs for %s\n", artist);
int index = promptForIndex(songTitles);
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: \n");
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 songs in the queue.");
} else {
Song song = songRequest.getMsong();
System.out.printf("\n\nReady %s? Open %s to hear %s by %s\n",
songRequest.getSingerName(),
song.getVideoUrl(),
song.getTitle(),
song.getArtist());
}
}
}
Garvit Kashyap
2,918 PointsNo need. I solved it. Thanks anyway!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsWhat error message are you getting?