CPSC 170 PostLab 2: 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 CDs by the given artist (you may need to add a getArtist method to the CD class), add them to the new collection, then return it. But note that the existing addCD method creates and adds a new CD object, which is not necessary here -- the CD object already exists, you just want to include it in your new collection. To do this, overload the addCD method with a version that takes a CD object instead of the info to create a new object. The CD object passed in can be added directly to the collection, and the rest of the new method will look very much like the existing addCD method.

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 In

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