0

I have a Song class(field: String name) and an Album class (initialize an Arraylist fill with Song objects), I am adding a method (addSongAlbum(String name parameter))that first check if the song is already in the album. Everything works fine but now I want to check too if the song exist outside the album. How can I do this taking in consideration that the input of the method is a String?

public void addSongToAlbum(String name){
    if(checkExist(name) == null){
        album.add(checkExist(name));
        System.out.println("Song "+name+" was successfully added");
    } else {
        System.out.println("This song is already in the album");
    }
}

private Song checkExist(String name){
    for(int i=0; i<album.size(); i++){
        if(name.equals(album.get(i).getName())){
            return album.get(i);
        }
    }
    return null;
}

2 Answers2

1

I'd create a central Songmanager object that is available to all objects working with Songs, like so

public class SongManager {

   private static SongManager sMan;

   private Map<String, Song> songs = new HashMap<String, Song>();

   public static SongManager getInstance() {
       if (sMan == null) {
           sMan = new SongManager();
       }
       return sMan;
   }

   public void addSong(Song s) {
       songs.put(s.getName, s);
   }

   public Song getSong(String name) {
       return songs.get(name);
   }

}

your songs can be simple container classes, like so

public class Song {

private String name;

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

}

You can access this manager everywhere by calling SongManager.getInstance(). Create a simple container class for songs all existing Songs to this Manager and in youraddSongMethod, simply call thegetSong` method to see if the song exists.

P.s. I know that the Singleton pattern I'm using here is very controversal (see e.g. What is so bad about singletons?, but to me it is the easiest working alternative here.

Community
  • 1
  • 1
user3792852
  • 311
  • 4
  • 14
0

You would need to have a List of all the Song objects somewhere. Either add them to a static List when constructing (you would need to remember to remove them when no longer used to let the memory be garbage collected), or have a similar List of all Album objects and then search through those. The first one is useful if you want to check all Song objects that were created, the second one if you only want to check those that are in albums.

Filip Smola
  • 166
  • 2
  • 7
  • This makes sense, so I have to group them inside something and then go thought this something, there is no way to check all the classes that have been created? (sorry if the question is dumb im new in programming, just want to be sure) –  Jul 24 '16 at 08:49
  • Yes, you need a way to get to them. Unless you have a connection to them (filed of an object you have access to, static field of a class, singleton class) you can't use them and they will get garbage collected eventualy. – Filip Smola Jul 24 '16 at 10:07