Java Reference
In-Depth Information
Code 4.7
Using Track in the
public void addTrack(Track track)
* Show a list of all the tracks in the collection.
public void listAllTracks()
System.out.println( "Track listing: " );
for (Track track : tracks) {
* Play a track in the collection.
* @param index The index of the track to be played.
public void playTrack( int index)
if (indexValid(index)) {
Track track = tracks.get(index);
System.out.println( "Now playing: " + track.getArtist() +
" - " + track.getTitle());
Other methods omitted.
While we can see that introducing a Track class has made some of the old methods slightly
more complicated, working with specialized Track objects ultimately results in a much better
structure for the program as a whole and allows us to develop the Track class to the most ap-
propriate level of detail for representing more than just music file names.
With a better structuring of track information, we can now do a much better job of searching
for tracks that meet particular criteria. For instance, if we want to find all tracks that contain the
word “love” in their title, we can do so as follows without risking mismatches on artists' names:
* List all tracks containing the given search string.
* @param searchString The search string to be found.
Search WWH ::

Custom Search