Ok, I must be losing it. I have been working on this project for over 20 hrs.
total. It should be easy as heck to do, but for some reason I cannot get it. I
think it is partly to do with the instructions, they seem real vague, I don't
understand exactly what I am to do. They read:
"Create a class called MusicCollection. This class must have member inner
classes called Artist and Recording. The Recording class must have an inner
class called Track that represents a single piece of music within a Recording
object. A MusicCollection object must have an array of Artist objects and an
array of Recording objects. Recording objects must have a single Artist object,
for simplicity, and an array of Track objects. Add a main method to
MusicCollection that tests the creation of all these objects."
This is what I have so far, any help would be greatly appreciated, or even
letting me know if I am on the right track, or if I am completely off.
|
Code:
|
/**
* @author twiggy62
*/
public class MusicCollection {
private int totalArtists = 0;
private int totalRecordings = 0;
private static final int ARRAY_LENGTH = 3;
private Artist[] artistObjects = new Artist[ARRAY_LENGTH];
private Recording[] recordingObjects = new Recording[ARRAY_LENGTH];
/**
* Test the creation of all objects
* @param args The command line arguments
* @exception Exception if a failure occurs while adding a object
*/
public static void main(String[] args) throws Exception {
MusicCollection mc = new MusicCollection();
Artist art1 = mc.new Artist("twig");
Artist art2 = mc.new Artist("twiggy");
Artist art3 = mc.new Artist("tiggy62");
Recording rec1 = mc.new Recording("1");
Recording rec2 = mc.new Recording("2");
Recording rec3 = mc.new Recording("3");
System.out.println(mc);
}
/**
* Provide a string representing the MusicCollection
* @return string Representation of the object
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < artistObjects.length; i++) {
sb.append("Artist" + (i + 1) + ":" + artistObjects[i].artist
+ " Recording: " + recordingObjects[i].record + " ");
}
return sb.toString();
}
public class Artist {
private String artist;
public Artist(String name) {
artist = name;
if (totalArtists < ARRAY_LENGTH) {
artistObjects[totalArtists++] = this;
}
}
}
public class Recording {
private String record;
public Recording(String name) {
record = name;
if (totalRecordings < ARRAY_LENGTH) {
recordingObjects[totalRecordings++] = this;
}
}
public class Track {
private String track;
public Track(String name) {
track = name;
}
}
}
} |