Reputation: 57
So I have two classes called songs and albums, here they are:
public class Songs {
private String title;
private Double duration;
public Songs(String title, Double duration) {
this.title = title;
this.duration = duration;
}
public Songs(){}
public String getTitle() {
return title;
}
public Double getDuration() {
return duration;
}
public static Songs addSong(String title, Double duration){
return new Songs(title,duration);
}
}
import java.util.ArrayList;
import java.util.LinkedList;
public class Albums {
private ArrayList<Songs> albums;
private String name;
private String title;
public Albums(String name, String title) {
this.albums = new ArrayList<>();
this.name = name;
this.title = title;
}
public boolean addSong(Songs songs){
if(findSong(songs.getTitle())==null){
this.albums.add(songs);
return true;
}else{
System.out.println("Song alredy exist!");
return false;
}
}
private Songs findSong(String songName){
for(int i=0;i<this.albums.size();i++){
Songs currentSong = this.albums.get(i);
if(currentSong.equals(songName)){
return currentSong;
}
}
return null;
}
}
The problem is the main, maybe my logic is not right, if I'm missing something please tell me:
public class Main {
public static void main(String[] args) {
Albums albums = new Albums("Alexandru", "Doi dsadcopii");
Songs songs = new Songs("Patrascu",4.44);
songs.addSong("Dorin",11.11);
albums.addSong(songs);
songs.addSong("Dorin",11.11);
albums.addSong(songs);
songs.addSong("Dorinsads",11.11);
albums.addSong(songs);
songs.addSong("Dorisadsan",11.11);
albums.addSong(songs);
System.out.println(songs.getTitle());
albums.addSong(songs);
albums.printSongs();
}
}
Why I'm getting the same value ? why those values from the .addSong where not added in the list ? This is my code to print the list:
public void printSongs() {
for (int i = 0; i < this.albums.size(); i++) {
System.out.println(i + 1 + "-->" + this.albums.get(i).getTitle() + "--->" + this.albums.get(i).getDuration());
}
}
For sure I'm missing something but I dont know exactly where, any ideas how to fix this ? Thanks! :D The problem is when I'm using the .printSongs() method it prints the first value
Upvotes: 1
Views: 2419
Reputation: 603
Assuming that by
songs.addSong("Dorin",11.11);
, you meant
Songs.addSong("Dorin",11.11);
. This is happening because you are not reassigning songs reference. Replace every statement
songs.addSong("Dorin",11.11);
with
songs=Songs.addSong("Dorin",11.11);
and it will work. Further observation is that your logic in 'findSong()' for equality will always yield false.
Upvotes: 0
Reputation: 186
Please Use modified below Main class.
public class Main {
public static void main(String[] args) {
Albums albums = new Albums("Alexandru", "Doi dsadcopii");
Songs songs = new Songs();
songs=songs.addSong("Patrascu",4.44);
albums.addSong(songs);
songs=songs.addSong("Dorin",11.11);
albums.addSong(songs);
songs=songs.addSong("Dorin",11.11);
albums.addSong(songs);
songs=songs.addSong("Dorinsads",11.11);
albums.addSong(songs);
songs=songs.addSong("Dorisadsan",11.11);
albums.addSong(songs);
System.out.println(songs.getTitle());
albums.addSong(songs);
albums.printSongs();
}
}
Upvotes: 0
Reputation: 2981
From your class Songs:
public static Songs addSong(String title, Double duration){
return new Songs(title,duration);
}
This is just returning a new Song, it is not adding it to the internal List.
You can just add them to the album directly, like
Albums albums = new Albums("Alexandru", "Doi dsadcopii");
albums.addSong(new Songs("Patrascu",4.44));
albums.addSong(new Songs("Dorin",11.11));
Upvotes: 2
Reputation: 37594
Your Song#addSong
method is plain wrong. You are creating new Sound
objects there. In fact you don't need to do that. Remove that method and use it like this
Albums albums = new Albums("Alexandru", "Doi dsadcopii");
albums.addSong(new Songs("Patrascu",4.44));
albums.addSong(new Songs("Dorin",11.11));
etc.
Upvotes: 0