/
KaraokeMachine.java
130 lines (112 loc) · 4.9 KB
/
KaraokeMachine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.teamtreehouse;
import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class KaraokeMachine {
private SongBook mSongBook;
private BufferedReader mReader;
private Map<String, String> mMenu;
public KaraokeMachine(SongBook songBook) {
mSongBook = songBook;
mReader = new BufferedReader(new InputStreamReader(System.in));
mMenu = new HashMap<String, String>();
mMenu.put("add", "Add a new song to the song book");
mMenu.put("choose", "Choose a song to sing!");
mMenu.put("quit", "Give up. Exit the program");
}
private String promptAction() throws IOException {
System.out.printf("There are %d songs available. Your options are: %n",
mSongBook.getSongCount());
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 artist = promptArtist();
Song artistSong = promptSongForArtist(artist);
// TODO: Add to a song queue
System.out.printf("You chose: %s %n", artistSong);
break;
case "quit":
System.out.println("Thanks for playing!");
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 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);
}
// en esta parte del código estamos buscando canciones por artistas // on this code stage, we are looking for a particular artist
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 or %s: %n", artist);
int index = promptForIndex(songTitles);
return songs.get(index);
}
// este método o función, es creadp para recibir una lista de cannciones y luego asignar un número a cada item o canción
// this function has being created to get a list of songs and then we assign a number to each item on the list
private int promptForIndex(List<String> options) throws IOException {
int counter = 1;
// con este for se muestran todas las opciones y se le asigna el numero de posición por asi decirlo a cada ítem.
// whith this for loop. we can show all the options (wich are songs) and it is gived a number of position.
for (String option : options) {
System.out.printf("%d.) %s %n", counter, option);
counter++;
}
// en este punto se lee desde la consola la opción ingresada por el cliente
// At this stage the code it read from the console the input option selected by the client.
System.out.print("Your choice: ");
String optionAsString = mReader.readLine();
// la funcion trim() permite validar los espacios en blanco, asi no tengo problemas de validación de la clave por espacios en blanco
// the trim() function allow you to validate the blank left by the client, some issues can arise from this, so tims() helps to avoid it
int choice = Integer.parseInt(optionAsString.trim());
// la unica razon para comenzar con la opocióm desde menos 1 es por que la lista comiemza en base cero, por lo que debemos dejar un espacio menjos para poder hacer nuestro trabajo.
// we are gonna return choice menus one, bacause we know it´s zero based, but we started at one, so we need to take one off of that.
return choice - 1;
}
}