PostLab 1: More Tunes

Add a method CDCollection findAll(String artist) to your CDCollection class. This method should return a CDCollection containing all of the CDs in the current collection that are by the given artist. This is easy; findAll will just create a new CDCollection, look through the current collection for matches, add them to the new collection, then return it. Note that to use the existing addCD method you need to know the title, artist, cost, and tracks of the CD. You can add methods to the CD class to get this information, OR you can write a new (private) method addExistingCD for the CDCollection class that instead of creating a new CD, takes a reference to an existing CD and just puts it in the collection. In the first case you have separate CD objects in the new and old collections, while in the second case they share a single CD object. I can imagine situations where either of this might be useful, so you can do it either way.

File MoreTunes.java contains most of a driver to test your program. Fill in the missing code and run the program. You'll notice that both the original and the new collection are called My CD Collection, which is a little misleading. To fix this, add a new constructor to the CDCollection class that takes a title for the collection, and store the title in an instance variable. Then in the toString method of CDCollection, use the title of the collection if one exists; if there is no title (i.e., if the original constructor was used), just say "My CD Collection" as before. Note that your findAll method should use the constructor that takes a title, passing the artists name. A run of your program might look like this:

[bloss@riddler lab1]$ java MoreTunes
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
My CD Collection

Number of CDs: 8
Total cost: $137.67
Average cost: $17.21

CD List:

$14.95  10      Storm Front     Billy Joel
$14.95  16      Come On Over    Shania Twain
$17.95  33      Soundtrack      Les Miserables
$13.90  11      Graceland       Paul Simon
$19.99  26      Double Live     Garth Brooks
$15.95  13      Greatest Hits   Jimmy Buffet
$19.99  26      Garth II        Garth Brooks
$19.99  26      Garth III       Garth Brooks

Enter artist to search for: Garth Brooks

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
My Garth Brooks CD Collection

Number of CDs: 3
Total cost: $59.97
Average cost: $19.99

CD List:

$19.99  26      Double Live     Garth Brooks
$19.99  26      Garth II        Garth Brooks
$19.99  26      Garth III       Garth Brooks

What To Turn Iin

Turn in hardcopies of CDCollection.java and MoreTunes.java. Tar your post1 directory and e-mail it to me with cpsc170 post1 in the Subject line.