0

I am writing a basic music app for my school. What our teacher asked from us is that wee need to create three classes; Song, PlayList and MusicCollection. A play list object contains songs and music collection contains play lists.I write my Song class and also a big part of my PlayList class. But there are two methods that I don't know very well. One is sortByName and the other is sortByPlayingTime. I need to use collections and I write something until now but it is not working. Can you have a look?

public class PlayList {

private String playListName;
private ArrayList<Song> songsPlayList = new ArrayList<>();

public PlayList(String playListName) {
    this.playListName = playListName;
}

public void addSong(Song songObject){
    songsPlayList.add(songObject);
}

public void removeSong(Song songObject) {
    songsPlayList.remove(songObject);
}

public void countOfPlaylist(){
    System.out.println("You have " + songsPlayList.size() + " songs on your list!");
}

public void displayPlayList(){
    for (int i =0;i<songsPlayList.size();i++){
        System.out.println((i+1) + ". " + songsPlayList.get(i));
    }
}

public void sortByName(){
    System.out.println("You can see your list as sorted by name.\n");
    Collections.sort(song); //this code give an error!
    for (Song temp : songsPlayList){
        System.out.println(temp);
    }
}

public void sortByPlayingTime(){
    System.out.println("Your playlist sorted by playing time.");
    //I stock here.
}

Also with my MusicCollection class I need to do the same thing. It was easy to add a song to a play list but now I need to choose (or say) which play list I need to add a song. I write my methods but not define yet. I need your help :)

Here is my MusicCollection class:

public class MusicCollection {

private String musicCollectionName;
private ArrayList<PlayList> collection = new ArrayList<>();

public MusicCollection(String musicCollectionName) {
    this.musicCollectionName = musicCollectionName;
}

public void addSongToPlayList(String playListName,Song songObject){


}

public void removeSongFormPlaylist (String playListName, Song songObject){

}

public void addPlayList (PlayList  playlistObject){

}

public void removePlayList (PlayList  playlistObject){

}

public int countOfPlaylistsInCollection(){

    return 0;
}

public int countsOfSongsInPlayList (String playlistName){

    return 0;
}

public void displayPlayListInCollection(){

}

public void displaySongsFromPlaylist (String playlistName){

}

}

  • 2
    Collections.sort(song); Where is your song variable? You have to sort songsPlayList I suppose. – Sergei Podlipaev Jun 19 '18 at 09:36
  • ..and what is the error? Either your `Song` class needs to implement `Comparable` or you can use `Collections.sort(list, Comparator)` – Jack Flamp Jun 19 '18 at 09:39
  • @SergeiPodlipaev I didn't put a song variable. I missed that variable and because of it it is not working good.Right? – Eren Arican Jun 19 '18 at 09:53
  • @JackFlamp Error was: reason: no instance(s) of type variable(s) T exist so that Song conforms to Comparable super T> But when I received that error, this was like Collections.sort(songsPlayList); – Eren Arican Jun 19 '18 at 09:53
  • @ErenArican no, this wasnt the full root cause. Please refer to my answer. – Sergei Podlipaev Jun 19 '18 at 09:55
  • @ErenArican `Collections.sort(songsPlayList)` means that the list is sorted according to its natural sorting order. An object has no natural order so you have to implement that yourself. See Maurice's answer or use `Comparable` – Jack Flamp Jun 19 '18 at 09:55

2 Answers2

0

Collections.sort(song);

Where is your song variable? You have to sort songsPlayList I suppose. And you have to write comparators for your Song class. For example implement Comparable interface. Please, refer to this article or this question for more information about comparators.

Sergei Podlipaev
  • 1,331
  • 1
  • 14
  • 34
0

You need a Comparator<Song> that compares two songs' playing time:

public static final Comparator<Song> BY_PLAYINGTIME = new Comparator<Song>() {
    @Override
    public int compare(Song a, Song b) {
        return a.getPlayingTime().compareTo(b.getPlayingTime());
    }
};

You can then sort your list with:

Collections.sort(songsPlayList, BY_PLAYINGTIME);
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24