16.2 Running Example: The CD Record Class

We will use the CD record class and the Genre enum type from Example 16.1 in many of the examples throughout this chapter. The classes are intentionally kept simple for the purposes of exposition, but they will suffice in illustrating the vast number of topics covered in this chapter.

The CD record class declares fields for the following information about a CD: an artist name, a title, a fixed number of tracks, the year the CD was released, and a musical genre. The compiler provides the relevant get methods to access the fields in a CD record, but boolean methods are explicitly defined to determine its musical genre. The compiler also provides implementations to override the toString(), hashCode(), and equals() methods from the Object class. However, the record class provides its own implementation of the toString() method. The record class implements the Comparable<CD> interface with the following comparison order for the fields: artist name, title, number of tracks, year released, and musical genre.

It is important to note the static field declarations in the CD record class, as they will be used in subsequent examples. These include the static references cd0, cd1, cd2, cd3, and cd4 that refer to five different instances of the CD record class. In addition, the fixed-size list cdList contains these five ready-made CDs, as does the array cdArray. The output from Example 16.1 shows the state of the CDs in the cdList. We recommend consulting this information in order to verify the results from examples presented in this chapter.

The simple enum type Genre is used to indicate the style of music on a CD.

Example 16.1 The CD Example Classes

Click here to view code image

// The different genres in music.
public enum Genre {POP, JAZZ, OTHER}

Click here to view code image

import java.time.Year;
import java.util.Comparator;
import java.util.List;
/** A record class that represents a CD. */
public record CD(String artist, String title, int noOfTracks,
                 Year year, Genre genre) implements Comparable<CD> {
  // Additional get methods:
  public boolean isPop()   { return this.genre == Genre.POP; }
  public boolean isJazz()  { return this.genre == Genre.JAZZ; }
  public boolean isOther() { return this.genre == Genre.OTHER; }
  // Provide own implementation of the toString() method.
  @Override public String toString() {
    return String.format(“<%s, \”%s\”, %d, %s, %s>”,
        this.artist, this.title, this.noOfTracks, this.year, this.genre);
  }
  /** Compare by artist, by title, by number of tracks, by year, and by genre. */
  @Override public int compareTo(CD other) {
    return Comparator.comparing(CD::artist)
                     .thenComparing(CD::title)
                     .thenComparing(CD::noOfTracks)
                     .thenComparing(CD::year)
                     .thenComparing(CD::genre)
                     .compare(this, other);
  }
  // Some ready-made CDs.
  public static final CD cd0
      = new CD(“Jaav”,      “Java Jive”,       8,  Year.of(2017), Genre.POP);
  public static final CD cd1
      = new CD(“Jaav”,      “Java Jam”,        6,  Year.of(2017), Genre.JAZZ);
  public static final CD cd2
      = new CD(“Funkies”,   “Lambda Dancing”,  10, Year.of(2018), Genre.POP);
  public static final CD cd3
      = new CD(“Genericos”, “Keep on Erasing”, 8,  Year.of(2018), Genre.JAZZ);
  public static final CD cd4
      = new CD(“Genericos”, “Hot Generics”,    10, Year.of(2018), Genre.JAZZ);
  // A fixed-size list of CDs.
  public static final List<CD> cdList = List.of(cd0, cd1, cd2, cd3, cd4);

  // An array of CDs.
  public static final CD[] cdArray = {cd0, cd1, cd2, cd3, cd4};
}

Click here to view code image

import java.util.List;

public final class CDAdmin {
  public static void main(String[] args) {
    List<CD> cdList = CD.cdList;
    System.out.println(”     Artist    Title           No. Year Genre”);
    for(int i = 0; i < cdList.size(); ++i) {
      CD cd = cdList.get(i);
      String cdToString = String.format(“%-10s%-16s%-4d%-5s%-5s”,
          cd.artist(), cd.title(), cd.noOfTracks(),
          cd.year(), cd.genre());
      System.out.printf(“cd%d: %s%n”, i, cdToString);
    }
  }
}

Output from the program:

Click here to view code image

     Artist    Title           No. Year Genre
cd0: Jaav      Java Jive       8   2017 POP
cd1: Jaav      Java Jam        6   2017 JAZZ
cd2: Funkies   Lambda Dancing  10  2018 POP
cd3: Genericos Keep on Erasing 8   2018 JAZZ
cd4: Genericos Hot Generics    10  2018 JAZZ

Leave a Reply

Your email address will not be published. Required fields are marked *