1

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

4
  • addSong doesn't add a Songs, it only creates a new object, and you just ignore it. Commented Aug 19, 2017 at 10:04
  • words are important. Change to singular Song and Album. Then thing about multiply (Album has many Song) ... so album.add(Song) etc Commented Aug 19, 2017 at 10:06
  • Since addSong creates a new object, which is not saved anywhere, your 'songs' variable remains unchanged and you are basically adding the same object 5 times. Commented Aug 19, 2017 at 10:11
  • What does the term "null constructor" in your title have to do with your question? Commented Aug 19, 2017 at 10:20

4 Answers 4

2

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.