Following classes are created, no interface/abstract class for now:
- Test – for creating test data
- Music – should store all CDs
- CD – should store Artist and CD title + all Songs
- Artist – only contains String name
- Song – contains default-constructor with (Artist anArtist, String songtitle, String Duration)
- Duration – for the song duration and later calculations
i tried an ArrayList, but it gets overwritten when i add another CD. I need a data type that stores the data with a link to another array. for example: store artist and title of CD1 and link to another array that contains all songs from CD1.
thanks
code from class Test:
Artist aArtist = new Artist("artist1");
CD aCD = myMusic.addCD(aArtist, "CD1");
aCD.addSong(aArtist, "song1", "1:00");
aArtist = new Artist("artist2");
CD aCD = myMusic.addCD(aArtist, "CD2"); //Overwrites arraylist from CD1
aCD.addSong(aArtist, "song2", "2:00");
i changed my ArrayLists in class Music and class CD… i think there is an easier way to solve that tutorial, but here is the code: (outputs all saved songs and CD title)
class Music:
import java.util.ArrayList;
public class Music {
private ArrayList<CD> myCDs = new ArrayList<CD>();
public CD addCD(Artist anArtist, String cdTitle) {
CD aCD = new CD(anArtist, cdTitle);
myCDs.add(aCD);
return aCD;
}
public void showData() {
for (int i = 0; i < myCDs.size(); i++) {
for (int j = 0; j < myCDs.get(i).getMyCD().size(); j++) {
System.out.println(myCDs.get(i).getMyCD().get(j).getSong());
}
}
}
}
class CD:
import java.util.ArrayList;
public class CD {
private String cdTitle;
private ArrayList<Song> myCD = new ArrayList<Song>();
public CD(Artist aArtist, String cdTitle) {
setMyCD(aArtist, cdTitle);
}
public ArrayList<Song> getMyCD() {
return myCD;
}
public String getMyCDtitle() {
return cdTitle;
}
public void setMyCD(Artist anArtist, String cdTitle) {
this.cdTitle = anArtist.getName() + ": " + cdTitle;
}
public void addSong(Artist anArtist, String aTitle, String aDuration) {
myCD.add(new Song(anArtist, aTitle, aDuration));
}
}
class Song:
public class Song {
private String song;
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Song(Artist anArtist, String songtitle, String aDuration) {
Duration songDuration = new Duration(aDuration);
setSong(anArtist.getName() + ": " + songtitle + " ("
+ songDuration.getSongDuration() + ")");
}
}
in general: how would you solve such a project ?
i would appreciate every advice or uml diagram.
This may be a problem with
aArtist = new Artist("artist2");
CD aCD = myMusic.addCD(aArtist, "CD2"); //Overwrites arraylist from CD1
aCD.addSong(aArtist, "song2", "2:00");
You use the same Artist object. If you post your addCD method implementation it will be more open for us.