Java Reference
In-Depth Information
When listing details of the tracks in listAllTracks , we request the Track object to return
a String containing its details. This shows that we have designed the Track class to be re-
sponsible for formatting the details to be printed, such as artist and title. This is an example of
what is called responsibility-driven design , which we cover in more detail in a later chapter.
In the playTrack method, we now have to retrieve the file name from the selected Track
object before passing it on to the player.
In the music library, we have added code to automatically read from the audio folder and
some print statements to display some information.
Code 4.7
Using Track in the
import java.util.ArrayList;
* A class to hold details of audio tracks.
* Individual tracks may be played.
* @author David J. Barnes and Michael Kölling
* @version 2011.07.31
public class MusicOrganizer
// An ArrayList for storing music tracks.
private ArrayList<Track> tracks;
// A player for the music tracks.
private MusicPlayer player;
// A reader that can read music files and load them as tracks.
private TrackReader reader;
* Create a MusicOrganizer
public MusicOrganizer()
tracks = new ArrayList<Track>();
player = new MusicPlayer();
reader = new TrackReader();
readLibrary( "audio" );
System.out.println( "Music library loaded. " +
getNumberOfTracks() + " tracks." );
* Add a track to the collection.
* @param track The track to be added.
Search WWH ::

Custom Search