//******************************************************************** // SortableCD.java Author: Lewis/Loftus/Cocking // // Solution to Programming Project 6.11 //******************************************************************** import java.text.NumberFormat; public class SortableCD implements Comparable { private String title, artist; private double value; private int tracks; //----------------------------------------------------------------- // Creates a new SortableCD with the specified information. //----------------------------------------------------------------- public SortableCD (String theTitle, String theArtist, double theValue, int theTracks) { title = theTitle; artist = theArtist; value = theValue; tracks = theTracks; } //----------------------------------------------------------------- // Returns a description of this SortableCD. //----------------------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String description; description = fmt.format(value) + "\t" + tracks + "\t"; description += artist + "\t" + title; return description; } //----------------------------------------------------------------- // Determines the relationship to another SortableCD, satisfying // the Comparable interface. Sorts first by artist name, then // by title. //----------------------------------------------------------------- public int compareTo (Object obj) { SortableCD other = (SortableCD) obj; int result = title.compareTo (other.getTitle()); if (result == 0) result = artist.compareTo (other.getArtist()); return result; } //----------------------------------------------------------------- // Returns the artist name for this SortableCD. //----------------------------------------------------------------- public String getArtist() { return artist; } //----------------------------------------------------------------- // Returns the title of this SortableCD. //----------------------------------------------------------------- public String getTitle() { return title; } }