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;
    }
  • Probably you're learning Java: http://www.cs.cmu.edu/~mrmiller/15-110/Handouts/PlayList.java, also is a duplication of: https://stackoverflow.com/questions/33162098/playlist-and-song-and-driver – azbarcea Oct 29 '18 at 23:07
  • you are probably trying to do something like https://stackoverflow.com/questions/11073539/creating-instance-list-of-different-objects – Usama Oct 29 '18 at 23:09

2 Answers2

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 {

}

Oktfolio
  • 176
  • 3
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());
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35