That is, if I put as input in the method duracionTotalCD
: the 2nd title of the CD , it gives me as output: the duration of the 2nd song of the 2nd CD , and it should return as output: the sum of the 2 songs of the 2nd CD .
NOTE: The program does not mark any compilation errors, simply the methods do not return what they have to return.
class CD {
private String titulo, autor;
List<Cancion> listaCanss = new ArrayList<Cancion>();
CD(String titulo, String autor, List<Cancion> listaCans) {
this.autor = autor;
this.listaCanss = listaCans;
this.titulo = titulo;
}
String getTitulo() {
return titulo;
}
void setTitulo(String titulo) {
this.titulo = titulo;
}
List<Cancion> getlistaCans() {
return listaCanss;
}
void setlistaCans(List<Cancion> s) {
listaCanss = s;
}
String getAutor() {
return autor;
}
void setAutor(String autor) {
this.autor = autor;
}
class Cancion {
private String titulo;
private double duracion;
Cancion(String titulo, double duracion) {
this.titulo = titulo;
this.duracion = duracion;
}
double getDuracion() {
return duracion;
}
void setDuracion(double d) {
duracion = d;
}
public String getTituloC() {
return titulo;
}
void setTituloC(String d) {
titulo = d;
}
}
public class Ejercicio {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
List<CD> listaCDs = new ArrayList<CD>(3);
List<Cancion> listaCanss = new ArrayList<Cancion>(2);
for (int i = 0; i < 3; i++) {
System.out.println("Nombre CD:");
String nomCD = leer.nextLine();
System.out.println("Autor CD:");
String aut = leer.nextLine();
leer.nextLine();
for (int j = 0; j < 2; j++) {
System.out.println("Nombre cancion:");
String nomCan = leer.nextLine();
System.out.println("duracion cancion:");
double dur = leer.nextInt();
leer.nextLine();
listaCanss.add(new Cancion(nomCan, dur));
}
listaCDs.add(new CD(nomCD, aut, listaCanss));
}
System.out.println("Introduzca el titulo del CD para devolver su
duracion total");
leer.nextLine();
String stt = leer.nextLine();
System.out.println(duracionTotalCD(stt, listaCDs));
System.out.println("Introduzca el nombre de la cancion a buscar
para devolver el CD al que pertenece y su respectivo autor");
leer.nextLine();
String st = leer.nextLine();
System.out.println(TitAu(listaCDs, st));
}
static double duracionTotalCD(String titulo, List<CD> listaCDs) {
double duracionT = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (0 == (listaCDs.get(i).getTitulo()).compareTo(titulo)) {
duracionT = duracionT +
listaCDs.get(i).listaCanss.get(j).getDuracion();
}
}
}
return duracionT;
}
public static String TitAu(List<CD> c, String n) {
int tam = c.size();
String s1 = null, s2 = null;
for (int i = 0; i < tam; i++) {
for (int j = 0; j < 2; j++) {
if ((c.get(i).listaCanss.get(j).getTituloC()).compareTo(n)
== 0) {
s1 = c.get(i).getTitulo() + " ";
s2 = c.get(i).getAutor();
}
}
}
return s1 + s2;
}
}
Because you are running in that function, who knows what, but when you do this:
You are checking if the song you are looking for is the one you passed... you are not doing anything with the previous ones...
Your method should work in another way, you should pass it the song number, and based on that, add the duration of the songs up to that.
EDITED
Based on the comments, the problem is something else. Let's analyze the CD duration function.
There are two for's in the function, one iterates over the cd's, the other iterates over the themes. So far there is no problem... The problem is that they iterate up to 2. In other words, if there are 4 cds, we will never see them. What is wrong is the whole logic of this method.
What should be done is the following:
Find the cd:
Once we know what the j is, we can go through the cd and add all its songs
Edited 2:
The pseudo code would be:
Iterate over the cd looking for which one we have to look for. With this index, iterate over the tracks on that cd, adding the times of each track.
something like:
if (0 == (listaCDs.get(i).getTitulo()).compareTo(titulo))
This check should go inside the outer loop. If so, you would loop through the songs, adding up the value.
In this way you avoid checking all the songs on all the discs, and you would only add in case the disc is the chosen one.