Amazon.com Widgets Exercise 15.9
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 19, 2014, 11:47:41 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 15
| | | |-+ Exercise 15.9
Pages: [1] Go Down
Print
Author Topic: Exercise 15.9 (Read 1335 times)
tknp
Newbie
*
Posts: 23






on: January 29, 2012, 02:41:48 PM

Here is what I came up with after getting some ideas from another forum.

Song.h
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Song : NSObject

@property (copy, nonatomic) NSString *title, *artist, *album, *playTime;

-(void) setTitle: (NSString *) theTitle setArtist: (NSString *) theArtist setAlbum: (NSString *) theAlbum setPlayTime: (NSString *) thePlayTime;
-(void) print;

@end

Song.m
Code: (Objective-C)
#import "Song.h"

@implementation Song

@synthesize title, artist, album, playTime;

-(void) setTitle: (NSString *) theTitle setArtist: (NSString *) theArtist setAlbum: (NSString *) theAlbum setPlayTime: (NSString *) thePlayTime
{
    self.title = theTitle;
    self.artist = theArtist;
    self.album = theAlbum;
    self.playTime = thePlayTime;
}

-(void) print
{
    NSLog(@"====================================");
NSLog(@"|                                  |");
NSLog(@"|  %-31s |", [title UTF8String]);
NSLog(@"|  %-31s |", [artist UTF8String]);
    NSLog(@"|  %-31s |", [album UTF8String]);
NSLog(@"|  %-31s |", [playTime UTF8String]);
NSLog(@"|                                  |");
NSLog(@"|      O                  O        |");
NSLog(@"====================================");
}
@end

Playlist.h
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Song.h"

@interface Playlist : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSMutableArray *playlist;

-(id) initWithName: (NSString *) name;

-(void) addSong: (Song *) theSong;
-(void) remSong: (Song *) theSong;

-(int) entries;
-(void) list;

@end

Playlist.m
Code: (Objective-C)
#import "Playlist.h"

@implementation Playlist

@synthesize playlist, name;

-(id) initWithName: (NSString *) theName
{
    self = [super init];
   
    if (self) {
        self.name = theName;
        playlist = [[NSMutableArray alloc] init];
    }
    return self;
}

-(id) init
{
    return [self initWithName: @"NoName"];
}

-(void) addSong: (Song *) theSong
{
    if ([playlist containsObject: theSong] == NO)
        [playlist addObject: theSong];
}

-(void) remSong: (Song *) theSong
{
    if ([playlist containsObject: theSong] == YES)
        [playlist removeObjectIdenticalTo: theSong];
}

-(int) entries
{
    return (int) [playlist count];
}

-(void) list
{
    // Creates filler characters for playlist names to match preexisting amount of "=" characters for my info dividers
NSMutableString *equalSignFiller = [[NSMutableString alloc] init];
for (int i = 0; i <= 26 - self.name.length; i++)
[equalSignFiller appendString: @"="];
    // End filler creation
   
    NSLog(@"========= Contents of: %@ %@", self.name, equalSignFiller);
    int i = 1;
    for ( Song *theSong in playlist )
    {
        NSLog(@"%i. %-20s    %-32s", i, [theSong.title UTF8String], [theSong.artist UTF8String]);
        i++;
    }
    NSLog(@"===================================================");
}

@end

MusicCollection.h
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Playlist.h"

@interface MusicCollection : NSObject
 
@property (nonatomic, copy) NSMutableArray *musicCollection;
@property (nonatomic, copy) Playlist *library;

-(id) initWithName: (NSString *) name;

-(void) addSong: (Song *) theSong toPlaylist: (Playlist *) thePlaylist;
-(void) remSong: (Song *) theSong fromPlaylist: (Playlist *) thePlaylist;
-(void) addPlaylist: (Playlist *) thePlaylist;
-(void) remPlaylist: (Playlist *) thePlaylist;
-(void) lookup: (NSString *) searchField;

-(int) entries;
-(void) list;

@end

MusicCollection.m
Code: (Objective-C)
#import "MusicCollection.h"

@implementation MusicCollection

@synthesize musicCollection, library;

-(id) initWithName: (NSString *) theName
{
    self = [super init];
   
    if (self) {
        musicCollection = [[NSMutableArray alloc] init];
        library = [[Playlist alloc] initWithName: @"library"];
        [musicCollection addObject: library];
    }
    return self;
}

-(void) addSong: (Song *) theSong toPlaylist: (Playlist *) thePlaylist
{
    if ([thePlaylist.playlist containsObject: theSong] == NO)
        [thePlaylist addSong: theSong];
    if ([library.playlist containsObject: theSong] == NO)
        [library addSong: theSong];
}

-(void) remSong: (Song *) theSong fromPlaylist: (Playlist *) thePlaylist
{
    if ([thePlaylist isEqualTo: library])
    {
        [library remSong: theSong];
       
        // also look through all the playlists and remove it if it's there
        for ( Playlist *thePlaylist in musicCollection )
            [thePlaylist remSong: theSong];
    }
    else {
    if ([thePlaylist.playlist containsObject: theSong] == YES)
        [thePlaylist remSong: theSong];
    }
}

-(void) addPlaylist: (Playlist *) thePlaylist
{
    if ([musicCollection containsObject: thePlaylist] == NO)
        [musicCollection addObject: thePlaylist];
}

-(void) remPlaylist: (Playlist *) thePlaylist
{
    if ([musicCollection containsObject: thePlaylist] == YES)
        [musicCollection removeObjectIdenticalTo: thePlaylist];
}

-(void) lookup: (NSString *) searchField
{
    NSMutableArray *results = [[NSMutableArray alloc] init];
   
    for ( Song *theSong in library.playlist )
    {
        if ( ([theSong.title rangeOfString: searchField options: NSCaseInsensitiveSearch]).length ||
            ([theSong.artist rangeOfString: searchField options: NSCaseInsensitiveSearch]).length ||
            ([theSong.album rangeOfString: searchField options: NSCaseInsensitiveSearch]).length )
            [results addObject: theSong];
    }
   
    NSLog(@"\n");
    NSLog(@" << Results are in for \"%s\"! >>", [searchField UTF8String]);
    for ( Song *theSong in results )
        [theSong print];
    NSLog(@" << End results for \"%s\". >>", [searchField UTF8String]);

}

-(int) entries
{
    return (int) [musicCollection count];
}

-(void) list
{
    NSLog(@"========== Contents of: Music Collection ==========");
    int i = 1;
    for ( Playlist *thePlaylist in musicCollection )
    {
        NSLog(@"%i. %@", i, thePlaylist.name);
        i++;
    }
    NSLog(@"===================================================");
}

@end

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "MusicCollection.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
       
        NSString *aTitle =      @"The Funeral";
        NSString *aArtist =     @"Band of Horses";
        NSString *aAlbum =      @"Everything All The Time";
        NSString *aPlayTime =   @"5:22";
        NSString *bTitle =      @"Run";
        NSString *bArtist =     @"Snow Patrol";
        NSString *bAlbum =      @"Up To Now - The Best Of";
        NSString *bPlayTime =   @"5:04";
        NSString *cTitle =      @"Kamera";
        NSString *cArtist =     @"Wilco";
        NSString *cAlbum =      @"Yankee Hotel Foxtrot";
        NSString *cPlayTime =   @"3:30";
        NSString *dTitle =      @"Zuton Fever";
        NSString *dArtist =     @"The Zutons";
        NSString *dAlbum =      @"Who Killed... The Zutons";
        NSString *dPlayTime =   @"3:08";
        NSString *eTitle =      @"Grapevine Fires";
        NSString *eArtist =     @"Death Cab For Cutie";
        NSString *eAlbum =      @"Narrow Stairs";
        NSString *ePlayTime =   @"4:11";
        NSString *fTitle =      @"El Pico";
        NSString *fArtist =     @"Ratatat";
        NSString *fAlbum =      @"Ratatat";
        NSString *fPlayTime =   @"4:42";
        NSString *gTitle =      @"Old Shoes";
        NSString *gArtist =     @"Tom Waits";
        NSString *gAlbum =      @"Closing Time";
        NSString *gPlayTime =   @"3:41";
       
        Song *song1 = [[Song alloc] init];
        Song *song2 = [[Song alloc] init];
        Song *song3 = [[Song alloc] init];
        Song *song4 = [[Song alloc] init];
        Song *song5 = [[Song alloc] init];
        Song *song6 = [[Song alloc] init];
        Song *song7 = [[Song alloc] init];


        // Add the song details to the song card
       
        [song1 setTitle: aTitle setArtist: aArtist setAlbum: aAlbum setPlayTime: aPlayTime];
        [song2 setTitle: bTitle setArtist: bArtist setAlbum: bAlbum setPlayTime: bPlayTime];
        [song3 setTitle: cTitle setArtist: cArtist setAlbum: cAlbum setPlayTime: cPlayTime];
        [song4 setTitle: dTitle setArtist: dArtist setAlbum: dAlbum setPlayTime: dPlayTime];
        [song5 setTitle: eTitle setArtist: eArtist setAlbum: eAlbum setPlayTime: ePlayTime];
        [song6 setTitle: fTitle setArtist: fArtist setAlbum: fAlbum setPlayTime: fPlayTime];
        [song7 setTitle: gTitle setArtist: gArtist setAlbum: gAlbum setPlayTime: gPlayTime];

       
        // Set up a new playlist
       
        Playlist *myPlaylist1 = [[Playlist alloc] initWithName: @"Marks's Playlist 1"];
        Playlist *myPlaylist2 = [[Playlist alloc] initWithName: @"Marks's Playlist 2"];
       
       
        // Set up a new music collection
        MusicCollection *myMusic = [[MusicCollection alloc] initWithName: @"music"];
       

        // Add the songs to the playlist
       
        [myMusic addSong: song1 toPlaylist: myPlaylist2];
        [myMusic addSong: song1 toPlaylist: myPlaylist2];   // dupe, shouldn't be added
        [myMusic addSong: song2 toPlaylist: myPlaylist2];
        [myMusic addSong: song3 toPlaylist: myPlaylist1];
        [myMusic addSong: song4 toPlaylist: myPlaylist1];
        [myMusic addSong: song5 toPlaylist: myPlaylist2];
       

        // Add playlists to music collection
       
        [myMusic addPlaylist: myPlaylist1];
        [myMusic addPlaylist: myPlaylist2];


        // Print songs
       
        NSLog(@"<< Printing song details for all current songs >>");
        NSLog(@"\n");
        [song1 print];[song2 print];[song3 print];[song4 print];[song5 print];

       
        // Print the playlists
       
        [myPlaylist1 list];
        [myPlaylist2 list];
       
       
        // Add more songs to a playlist
       
        NSLog(@"\n");
        NSLog(@"<< Add 2 new songs to playlist1 >>");
        NSLog(@"\n");
       
        [myMusic addSong: song6 toPlaylist: myPlaylist1];
        [myMusic addSong: song7 toPlaylist: myPlaylist1];
        [myPlaylist1 list];     // list playlist1
        [myPlaylist2 list];     // list playlist2
        [myMusic.library list]; // list library
       
       
        // Remove a song from a playlist
       
        NSLog(@"\n");
        NSLog(@"<< Remove a song from playlist2, but leave it in library >>");
        NSLog(@"\n");
       
        [myMusic remSong: song5 fromPlaylist: myPlaylist2];
        [myPlaylist1 list];     // list playlist1
        [myPlaylist2 list];     // list playlist2
        [myMusic.library list]; // list library
       
       
        // Reset to default
       
        NSLog(@"\n");
        NSLog(@"<< Reset to default, add song back to playlist >>");
        NSLog(@"\n");

        [myMusic addSong: song5 toPlaylist: myPlaylist2];
        [myPlaylist1 list];     // list playlist1
        [myPlaylist2 list];     // list playlist2
        [myMusic.library list]; // list library

       
        // Remove a song from the library which removes it from all playlists
       
        NSLog(@"\n");
        NSLog(@"<< Remove a song from library >>");
        NSLog(@"\n");
       
        [myMusic remSong: song4 fromPlaylist: myMusic.library];
        [myPlaylist1 list];     // list playlist1
        [myPlaylist2 list];     // list playlist2
        [myMusic.library list]; // list library
       
       
        // Lookup some songs
       
        NSLog(@"\n");
        NSLog(@"<< Searching based on differing criteria... >>");
       
        [myMusic lookup: @"run"];
        [myMusic lookup: @"closing"];
        [myMusic lookup: @"of"];

       
        // Print the music collection
       
        NSLog(@"\n");
        NSLog(@"<< Print the playlists >>");
        NSLog(@"\n");
       
        [myMusic list];
       
       
        // Remove a playlist from the music collection
       
        NSLog(@"\n");
        NSLog(@"<< Remove a playlist and print the list >>");
        NSLog(@"\n");
       
        [myMusic remPlaylist: myPlaylist2];
        [myMusic list];

    }
    return 0;
}


And the output:
Quote
2012-01-29 13:36:18.488 exercise15[5071:903] << Printing song details for all current songs >>
2012-01-29 13:36:18.634 exercise15[5071:903]
2012-01-29 13:36:18.634 exercise15[5071:903] ====================================
2012-01-29 13:36:18.635 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.636 exercise15[5071:903] |  The Funeral                     |
2012-01-29 13:36:18.637 exercise15[5071:903] |  Band of Horses                  |
2012-01-29 13:36:18.637 exercise15[5071:903] |  Everything All The Time         |
2012-01-29 13:36:18.638 exercise15[5071:903] |  5:22                            |
2012-01-29 13:36:18.639 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.640 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:18.640 exercise15[5071:903] ====================================
2012-01-29 13:36:18.641 exercise15[5071:903] ====================================
2012-01-29 13:36:18.642 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.642 exercise15[5071:903] |  Run                             |
2012-01-29 13:36:18.665 exercise15[5071:903] |  Snow Patrol                     |
2012-01-29 13:36:18.666 exercise15[5071:903] |  Up To Now - The Best Of         |
2012-01-29 13:36:18.667 exercise15[5071:903] |  5:04                            |
2012-01-29 13:36:18.668 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.668 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:18.669 exercise15[5071:903] ====================================
2012-01-29 13:36:18.670 exercise15[5071:903] ====================================
2012-01-29 13:36:18.671 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.672 exercise15[5071:903] |  Kamera                          |
2012-01-29 13:36:18.673 exercise15[5071:903] |  Wilco                           |
2012-01-29 13:36:18.674 exercise15[5071:903] |  Yankee Hotel Foxtrot            |
2012-01-29 13:36:18.674 exercise15[5071:903] |  3:30                            |
2012-01-29 13:36:18.675 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.676 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:18.677 exercise15[5071:903] ====================================
2012-01-29 13:36:18.678 exercise15[5071:903] ====================================
2012-01-29 13:36:18.678 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.679 exercise15[5071:903] |  Zuton Fever                     |
2012-01-29 13:36:18.680 exercise15[5071:903] |  The Zutons                      |
2012-01-29 13:36:18.680 exercise15[5071:903] |  Who Killed... The Zutons        |
2012-01-29 13:36:18.681 exercise15[5071:903] |  3:08                            |
2012-01-29 13:36:18.682 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.682 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:18.684 exercise15[5071:903] ====================================
2012-01-29 13:36:18.684 exercise15[5071:903] ====================================
2012-01-29 13:36:18.685 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.685 exercise15[5071:903] |  Grapevine Fires                 |
2012-01-29 13:36:18.686 exercise15[5071:903] |  Death Cab For Cutie             |
2012-01-29 13:36:18.687 exercise15[5071:903] |  Narrow Stairs                   |
2012-01-29 13:36:18.687 exercise15[5071:903] |  4:11                            |
2012-01-29 13:36:18.688 exercise15[5071:903] |                                  |
2012-01-29 13:36:18.688 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:18.689 exercise15[5071:903] ====================================
2012-01-29 13:36:18.689 exercise15[5071:903] ========= Contents of: Marks's Playlist 1 =========
2012-01-29 13:36:18.690 exercise15[5071:903] 1. Kamera                  Wilco                           
2012-01-29 13:36:18.691 exercise15[5071:903] 2. Zuton Fever             The Zutons                     
2012-01-29 13:36:18.691 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.692 exercise15[5071:903] ========= Contents of: Marks's Playlist 2 =========
2012-01-29 13:36:18.692 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:18.693 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:18.694 exercise15[5071:903] 3. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:18.694 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.695 exercise15[5071:903]
2012-01-29 13:36:18.696 exercise15[5071:903] << Add 2 new songs to playlist1 >>
2012-01-29 13:36:18.696 exercise15[5071:903]
2012-01-29 13:36:18.697 exercise15[5071:903] ========= Contents of: Marks's Playlist 1 =========
2012-01-29 13:36:18.698 exercise15[5071:903] 1. Kamera                  Wilco                           
2012-01-29 13:36:18.699 exercise15[5071:903] 2. Zuton Fever             The Zutons                     
2012-01-29 13:36:18.700 exercise15[5071:903] 3. El Pico                 Ratatat                         
2012-01-29 13:36:18.701 exercise15[5071:903] 4. Old Shoes               Tom Waits                       
2012-01-29 13:36:18.702 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.702 exercise15[5071:903] ========= Contents of: Marks's Playlist 2 =========
2012-01-29 13:36:18.703 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:18.704 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:18.704 exercise15[5071:903] 3. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:18.705 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.707 exercise15[5071:903] ========= Contents of: library ====================
2012-01-29 13:36:18.707 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:18.708 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:18.709 exercise15[5071:903] 3. Kamera                  Wilco                           
2012-01-29 13:36:18.709 exercise15[5071:903] 4. Zuton Fever             The Zutons                     
2012-01-29 13:36:18.710 exercise15[5071:903] 5. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:18.711 exercise15[5071:903] 6. El Pico                 Ratatat                         
2012-01-29 13:36:18.712 exercise15[5071:903] 7. Old Shoes               Tom Waits                       
2012-01-29 13:36:18.712 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.713 exercise15[5071:903]
2012-01-29 13:36:18.713 exercise15[5071:903] << Remove a song from playlist2, but leave it in library >>
2012-01-29 13:36:18.714 exercise15[5071:903]
2012-01-29 13:36:18.715 exercise15[5071:903] ========= Contents of: Marks's Playlist 1 =========
2012-01-29 13:36:18.715 exercise15[5071:903] 1. Kamera                  Wilco                           
2012-01-29 13:36:18.716 exercise15[5071:903] 2. Zuton Fever             The Zutons                     
2012-01-29 13:36:18.717 exercise15[5071:903] 3. El Pico                 Ratatat                         
2012-01-29 13:36:18.719 exercise15[5071:903] 4. Old Shoes               Tom Waits                       
2012-01-29 13:36:18.720 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.721 exercise15[5071:903] ========= Contents of: Marks's Playlist 2 =========
2012-01-29 13:36:18.722 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:18.722 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:18.723 exercise15[5071:903] ===================================================
2012-01-29 13:36:18.724 exercise15[5071:903] ========= Contents of: library ====================
2012-01-29 13:36:18.724 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:18.725 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:18.726 exercise15[5071:903] 3. Kamera                  Wilco                           
2012-01-29 13:36:19.748 exercise15[5071:903] 4. Zuton Fever             The Zutons                     
2012-01-29 13:36:19.751 exercise15[5071:903] 5. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:19.754 exercise15[5071:903] 6. El Pico                 Ratatat                         
2012-01-29 13:36:19.755 exercise15[5071:903] 7. Old Shoes               Tom Waits                       
2012-01-29 13:36:19.757 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.759 exercise15[5071:903]
2012-01-29 13:36:19.760 exercise15[5071:903] << Reset to default, add song back to playlist >>
2012-01-29 13:36:19.761 exercise15[5071:903]
2012-01-29 13:36:19.762 exercise15[5071:903] ========= Contents of: Marks's Playlist 1 =========
2012-01-29 13:36:19.762 exercise15[5071:903] 1. Kamera                  Wilco                           
2012-01-29 13:36:19.764 exercise15[5071:903] 2. Zuton Fever             The Zutons                     
2012-01-29 13:36:19.765 exercise15[5071:903] 3. El Pico                 Ratatat                         
2012-01-29 13:36:19.766 exercise15[5071:903] 4. Old Shoes               Tom Waits                       
2012-01-29 13:36:19.767 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.768 exercise15[5071:903] ========= Contents of: Marks's Playlist 2 =========
2012-01-29 13:36:19.768 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:19.769 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:19.770 exercise15[5071:903] 3. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:19.770 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.771 exercise15[5071:903] ========= Contents of: library ====================
2012-01-29 13:36:19.772 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:19.772 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:19.773 exercise15[5071:903] 3. Kamera                  Wilco                           
2012-01-29 13:36:19.774 exercise15[5071:903] 4. Zuton Fever             The Zutons                     
2012-01-29 13:36:19.774 exercise15[5071:903] 5. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:19.775 exercise15[5071:903] 6. El Pico                 Ratatat                         
2012-01-29 13:36:19.776 exercise15[5071:903] 7. Old Shoes               Tom Waits                       
2012-01-29 13:36:19.776 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.777 exercise15[5071:903]
2012-01-29 13:36:19.778 exercise15[5071:903] << Remove a song from library >>
2012-01-29 13:36:19.778 exercise15[5071:903]
2012-01-29 13:36:19.779 exercise15[5071:903] ========= Contents of: Marks's Playlist 1 =========
2012-01-29 13:36:19.780 exercise15[5071:903] 1. Kamera                  Wilco                           
2012-01-29 13:36:19.780 exercise15[5071:903] 2. El Pico                 Ratatat                         
2012-01-29 13:36:19.781 exercise15[5071:903] 3. Old Shoes               Tom Waits                       
2012-01-29 13:36:19.782 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.782 exercise15[5071:903] ========= Contents of: Marks's Playlist 2 =========
2012-01-29 13:36:19.783 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:19.784 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:19.784 exercise15[5071:903] 3. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:19.785 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.786 exercise15[5071:903] ========= Contents of: library ====================
2012-01-29 13:36:19.786 exercise15[5071:903] 1. The Funeral             Band of Horses                 
2012-01-29 13:36:19.787 exercise15[5071:903] 2. Run                     Snow Patrol                     
2012-01-29 13:36:19.788 exercise15[5071:903] 3. Kamera                  Wilco                           
2012-01-29 13:36:19.788 exercise15[5071:903] 4. Grapevine Fires         Death Cab For Cutie             
2012-01-29 13:36:19.789 exercise15[5071:903] 5. El Pico                 Ratatat                         
2012-01-29 13:36:19.790 exercise15[5071:903] 6. Old Shoes               Tom Waits                       
2012-01-29 13:36:19.790 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.791 exercise15[5071:903]
2012-01-29 13:36:19.792 exercise15[5071:903] << Searching based on differing criteria... >>
2012-01-29 13:36:19.792 exercise15[5071:903]
2012-01-29 13:36:19.793 exercise15[5071:903]  << Results are in for "run"! >>
2012-01-29 13:36:19.794 exercise15[5071:903] ====================================
2012-01-29 13:36:19.794 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.795 exercise15[5071:903] |  Run                             |
2012-01-29 13:36:19.796 exercise15[5071:903] |  Snow Patrol                     |
2012-01-29 13:36:19.796 exercise15[5071:903] |  Up To Now - The Best Of         |
2012-01-29 13:36:19.797 exercise15[5071:903] |  5:04                            |
2012-01-29 13:36:19.808 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.809 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:19.811 exercise15[5071:903] ====================================
2012-01-29 13:36:19.812 exercise15[5071:903]  << End results for "run". >>
2012-01-29 13:36:19.821 exercise15[5071:903]
2012-01-29 13:36:19.822 exercise15[5071:903]  << Results are in for "closing"! >>
2012-01-29 13:36:19.822 exercise15[5071:903] ====================================
2012-01-29 13:36:19.823 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.823 exercise15[5071:903] |  Old Shoes                       |
2012-01-29 13:36:19.824 exercise15[5071:903] |  Tom Waits                       |
2012-01-29 13:36:19.825 exercise15[5071:903] |  Closing Time                    |
2012-01-29 13:36:19.825 exercise15[5071:903] |  3:41                            |
2012-01-29 13:36:19.826 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.831 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:19.832 exercise15[5071:903] ====================================
2012-01-29 13:36:19.832 exercise15[5071:903]  << End results for "closing". >>
2012-01-29 13:36:19.833 exercise15[5071:903]
2012-01-29 13:36:19.834 exercise15[5071:903]  << Results are in for "of"! >>
2012-01-29 13:36:19.835 exercise15[5071:903] ====================================
2012-01-29 13:36:19.835 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.836 exercise15[5071:903] |  The Funeral                     |
2012-01-29 13:36:19.837 exercise15[5071:903] |  Band of Horses                  |
2012-01-29 13:36:19.837 exercise15[5071:903] |  Everything All The Time         |
2012-01-29 13:36:19.838 exercise15[5071:903] |  5:22                            |
2012-01-29 13:36:19.839 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.839 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:19.840 exercise15[5071:903] ====================================
2012-01-29 13:36:19.841 exercise15[5071:903] ====================================
2012-01-29 13:36:19.842 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.842 exercise15[5071:903] |  Run                             |
2012-01-29 13:36:19.843 exercise15[5071:903] |  Snow Patrol                     |
2012-01-29 13:36:19.846 exercise15[5071:903] |  Up To Now - The Best Of         |
2012-01-29 13:36:19.846 exercise15[5071:903] |  5:04                            |
2012-01-29 13:36:19.847 exercise15[5071:903] |                                  |
2012-01-29 13:36:19.848 exercise15[5071:903] |      O                  O        |
2012-01-29 13:36:19.848 exercise15[5071:903] ====================================
2012-01-29 13:36:19.849 exercise15[5071:903]  << End results for "of". >>
2012-01-29 13:36:19.850 exercise15[5071:903]
2012-01-29 13:36:19.850 exercise15[5071:903] << Print the playlists >>
2012-01-29 13:36:19.852 exercise15[5071:903]
2012-01-29 13:36:19.853 exercise15[5071:903] ========== Contents of: Music Collection ==========
2012-01-29 13:36:19.854 exercise15[5071:903] 1. library
2012-01-29 13:36:19.855 exercise15[5071:903] 2. Marks's Playlist 1
2012-01-29 13:36:19.855 exercise15[5071:903] 3. Marks's Playlist 2
2012-01-29 13:36:19.856 exercise15[5071:903] ===================================================
2012-01-29 13:36:19.857 exercise15[5071:903]
2012-01-29 13:36:19.858 exercise15[5071:903] << Remove a playlist and print the list >>
2012-01-29 13:36:19.859 exercise15[5071:903]
2012-01-29 13:36:19.859 exercise15[5071:903] ========== Contents of: Music Collection ==========
2012-01-29 13:36:19.860 exercise15[5071:903] 1. library
2012-01-29 13:36:19.861 exercise15[5071:903] 2. Marks's Playlist 1
2012-01-29 13:36:19.862 exercise15[5071:903] ===================================================
Logged
Sergion
Newbie
*
Posts: 4



Email




Reply #1 on: February 18, 2012, 03:47:58 PM

Great job!
Logged

Illusion!
thecheops
Newbie
*
Posts: 3


Email




Reply #2 on: March 01, 2012, 12:33:15 PM

So then what do the addSong and remSong methods in Playlist.m do?  They are never used.
Logged
tknp
Newbie
*
Posts: 23






Reply #3 on: March 01, 2012, 02:16:24 PM

They do get called in MusicCollection but in a superfluous way. Mainly did it this way so I could keep backwards compatibility going with older exercises and not having different versions of Song.m and Playlist.m all over the place
Logged
Hesadanza
Newbie
*
Posts: 28






Reply #4 on: March 03, 2012, 07:22:52 PM

Great work.  I'm working on this exercise right now myself, and am running into a wall.

Maybe I'm overthinking this, but I see two issues with your program:
  • The addSong method in Playlist can never be used directly from main (i.e. [myPlaylist1 addSong:song1]).  If it were, then a song may be added to a playlist that is not also added to the master library.
  • The remSong method in Playlist can never be called directly from main on the master library (i.e. [myMusic.library remSong song1]).  If it were, then a song would be removed from the master, but not all other playlists.

How would you take these two scenarios into account in your program?  How would you prevent another programmer who is using your classes from doing this?

These are a couple of the issues I've run into while working on this exercise, and wondered if anyone else had some insight?  How is the Playlist class supposed to know if a song is already part of the master library in the collection to which the playlist belongs, so if it's not, it can add it?  How does the Playlist class remove a song from all other playlists in a collection, if a song is removed from it, and it is the master library?

It seems, from the way the exercise is written, that these features should be part of the Playlist class, not the MusicCollection class.  But maybe it's just a poorly written exercise?
Logged
starryVere
Newbie
*
Posts: 6






Reply #5 on: March 25, 2013, 12:40:26 PM

wow great program tknp, thanks for sharing!

this was very helpful for me to look over, and props to your musical selection as well!
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.