//******************************************************************** // SortableCD.java Author: Lewis and Loftus // // Classe usata nel progetto di programmazione 6.9. //******************************************************************** import java.text.NumberFormat; public class SortableCD implements Comparable { private String title, artist; private double value; private int tracks; //----------------------------------------------------------------- // Costruttore della classe SortableCD con le informazioni // necessarie per generare un CD specifico. //----------------------------------------------------------------- public SortableCD (String theTitle, String theArtist, double theValue, int theTracks) { title = theTitle; artist = theArtist; value = theValue; tracks = theTracks; } //----------------------------------------------------------------- // Riporta la descrizione di questo CD. //----------------------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String description; description = fmt.format(value) + "\t" + tracks + "\t"; description += artist + "\t" + title; return description; } //----------------------------------------------------------------- // Implementa l'interfaccia Comparable e definisce come si // confrontano CD. Ordina prima per artista, poi per titolo. //----------------------------------------------------------------- public int compareTo (Object obj) { SortableCD other = (SortableCD) obj; int result = artist.compareTo (other.getArtist() ); if (result == 0) result = title.compareTo (other.getTitle()); return result; } //----------------------------------------------------------------- // Riporta il nome dell'artista di questo SortableCD. //----------------------------------------------------------------- public String getArtist() { return artist; } //----------------------------------------------------------------- // Riporta il titolo di questo SortableCD. //----------------------------------------------------------------- public String getTitle() { return title; } }