I've been given a task of creating a MP3 catalogue with all the usual functions. I have a MP3_Track, MP3_UserInterface and Main classes.
public class MP3_Track implements Comparable<MP3_Track>{
private int trackNo;
private String artistName;
private String albumName;
private String trackLength;
MP3_Track(int no, String artist, String album, String length){
this.trackNo = no;
this.artistName = artist;
this.albumName = album;
this.trackLength = length;
// setters & getters
@Override
public String toString(){
return (" Track Number : " + trackNo + "\n Artist Name : " + artistName + "\n Album Name : " + albumName +"\n Track Length : " + trackLength);
}
@Override
public int compareTo(MP3_Track aTrack){
int result;
try{
result = artistName.compareTo(aTrack.artistName);
}
catch(UnsupportedOperationException e){
throw new UnsupportedOperationException("Not supported here.");
}
return result;
}
}
My main class contains:
static void deleteTrack()
static MP3_Track addTrack()
static void moveTrack()
static void exploreTracks()...etc
Main calls on the relevant methods depending what the user enters at the console.
Such as: Collections.sort(myTracks); myTracks is a collection of MP3_Tracks and at the moment mt MP3_Track overrides the compareTo() method but as you can see it only sorts it by artistName!!! I want the user to have the choice of how the tracks are sorted etc. I thought that maybe I could have a data mamber in MP3_Track which could flag what type of sort to implement then some if's in compareTo method to return the required result. This solution seems somewhat cumbersome, another possibility I thought of was maybe implementing a custom interface!!!
Any suggestions as to a slick workaround are much appreciated???
Many thanks i advance, great site BTW..