package examples; import java.text.DateFormat; import java.text.ParseException; import java.util.*; /** * CS401 Example CompilationCD * * A simple class to represent a music CD where each track belongs to an * different artist. Like MusicCD, this class is very simple, providing only a * single constructor, toString() method, and compareTo() method. Notice that * much of the implementation is exactly like that of MusicCD. * * @author PJ Dillon */ public class CompilationCD { public static String [] music_genres = {"Popular", "Rock", "Jazz", "Country", "R&B", "Classical", "Other"}; private String title; private String[] artists; private int genre; private String[] trackTitles; private int length; private Date releaseDate; public CompilationCD(String t, String[] a, int g, String[] tt, int l, String d) { title = new String(t); /* * Unlike MusicCD, we're given an array of Strings for the artist names, * each of which we expect to correspond to the names of the tracks in the * trackTitles array. */ artists = new String[a.length]; for(int i = 0; i < a.length; i++) artists[i] = new String(a[i]); if (g >= 0 && g < music_genres.length) genre = g; else genre = music_genres.length-1; trackTitles = new String[tt.length]; for(int i = 0; i < tt.length; i++) trackTitles[i] = tt[i]; DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); try { releaseDate = df.parse(d); } catch (ParseException e) { releaseDate = null; } length = l; } public String toString() { StringBuffer s = new StringBuffer(); s.append("CD: " + title + '\n'); s.append("Artists: "); for(int i = 0; i < artists.length; i++) if(i < artists.length -1) s.append(artists[i] + ", "); else s.append(artists[i]); s.append('\n'); s.append("Genre: " + music_genres[genre] + '\n'); if (releaseDate != null) s.append("Release Date: " + releaseDate.toString() + "\n"); else s.append("No release date \n"); int min = length / 60; int sec = length % 60; s.append("Number of tracks: " + trackTitles.length + "\n"); s.append("Length: " + min + " min. " + sec + " sec. \n"); return s.toString(); } public int compareTo(Object r) { CompilationCD rhs = (CompilationCD) r; return this.title.compareTo(rhs.title); } }