0

If I hava a class PlayList a class song and a class advertisement and a I create a Listplaylist(in a different class called Index) How can I add songs (or adds) to playlist? It doesn't allow me to do it. I am not allow to use .add in there (index)

public static void PrintPlayList() {
        int songsNumber = songs.size();
        int addsNumber = adds.size();
        for(int i=0; i<songsNumber;i++) {
            playlist.add(songs.get(i));

        } 

After Declaring the list in there

 public static List<PlayListStuff> playlist = new ArrayList<PlayListStuff>();

And having the fields song and add in the class PlayListStuff

public class PlayListStuff {
    private Song song;
    private Add  add;
}
public PlayListStuff(Song song) {
        super();
        this.song = song;

    }
    public PlayListStuff( Add add) {
        super();

        this.add = add;
    }
2

2 Answers 2

0

You can only add PlayListStuff object into playlist.

Song and Add are supposed to extends PlayListStuff.

like

public class PlayListStuff {

}

public class Song extends PlayListStuff {

}

public class Add extends PlayListStuff {

}

Sign up to request clarification or add additional context in comments.

Comments

0
public final class PlayList {

    private final List<Stuff> stuffs = new ArrayList<>();

    public void addStuff(Stuff stuff) {
        stuffs.add(stuff);
    }

    public interface Stuff {}
}

public class Song implements PlayList.Stuff {}

public class Add implements PlayList.Stuff {}

And your client code could look like this:

PlayList playList = new PlayList();
playList.addStuff(new Song());
playList.addStuff(new Add());

Comments