//定义三个新类:Song、PlayList和MusicCollection。
//Song对象包含着关于特定歌曲的信息,比如歌曲名songName、艺术家artist、专辑album、歌曲长度length等;
//PlayList对象包含播放列表名称和一个歌曲的集合;
//MusicCollection对象包含PlayList集合,它包含一个名为Library的主播放列表,这个列表包含该集合中的所有歌曲。
//
//定义上述三个类,并编写方法实现下列功能:
//a.创建一个Song对象,并设置其信息。
//b.创建一个PlayList对象,并对播放列表添加和删除歌曲。
//添加:如果一首歌曲不在主列表中,那么将其添加进去。
//c.从主播放列表删除歌曲:确保从主播放列表中删除一首歌时,也要从音乐集合中的其他播放列表中删除此歌曲。
//d.创建一个MusicCollection对象,并对该集合添加和删除播放列表。
//e.搜索并显示关于所有歌曲、播放列表和整个音乐集合的信息。
//f.确保所有你定义的类都不产生内存泄漏。
//g.实现排序和模糊查找功能。
#import <Foundation/Foundation.h> #import "PlayList.h" #import "MusicCollection.h" int main(int argc, const char * argv[]) { @autoreleasepool { Song* song1 = [[[Song alloc] initWithName:@"风往北吹" artist:@"孙楠"album:@"拯救" length: 430] autorelease]; Song* song2 = [[[Song alloc] initWithName:@"东京爱情故事" artist:@"匿名" album:@"未知" length:440] autorelease]; Song* song3 = [[[Song alloc] initWithName:@"花田错" artist:@"王力宏" album:@"改变自己" length:380] autorelease]; Song* song4 = [[[Song alloc] initWithName:@"明天你是否依然爱我" artist:@"未知" album:@"未知" length:290] autorelease]; Song* song5 = [[[Song alloc] initWithName:@"捡肥皂" artist:@"某某" album:@"IOS不得了" length:315] autorelease]; Song* song6 = [[[Song alloc] initWithName:@"逆光" artist:@"孙燕姿" album:@"逆光" length:342426430] autorelease]; Song* song7 = [[[Song alloc] initWithName:@"相约98" artist:@"孙楠" album:@"拯救" length:342423430] autorelease]; Song* song8 = [[[Song alloc] initWithName:@"捡肥皂" artist:@"某某" album:@"IOS不得了" length:372423430] autorelease]; PlayList * pl1 = [[[PlayList alloc] initWithName:@"list爱情圣地"] autorelease]; [pl1 addSongtoList:song1]; [pl1 addSongtoList:song2]; [pl1 addSongtoList:song3]; [pl1 addSongtoList:song4]; [pl1 addSongtoList:song5]; PlayList * pl2 = [[[PlayList alloc] initWithName:@"list新不了情"] autorelease]; [pl2 addSongtoList:song6]; [pl2 addSongtoList:song7]; [pl2 addSongtoList:song8]; PlayList * pl3 = [[[PlayList alloc] initWithName:@"listiOS还行"] autorelease]; [pl3 addSongtoList:song5]; [pl3 addSongtoList:song7]; [pl3 addSongtoList:song8]; [pl1 description]; //排序 [pl1 sortList]; [pl2 description]; [pl3 description]; [pl1 removeSongFromList:song2]; MusicCollection *mc = [[[MusicCollection alloc] init] autorelease]; //主列表只有一个,系统会判断如果有主列表则将其清空后再添加,如果列表在副歌列表则将其移动至主列表 [mc addPlaylistToSet2:pl1]; [mc addPlaylistToSet2:pl2]; [mc addPlaylistToSet2:pl3]; [mc addPlaylistToSet:pl2]; [mc addPlaylistToSet:pl1]; //模糊查找 [mc serchForSongName:@"花"]; //c.从主播放列表删除歌曲:确保从主播放列表中删除一首歌时,也要从音乐集合中的其他播放列表中删除此歌曲。 [mc removeSongFromSet:song5]; } return 0; }
#import <Foundation/Foundation.h> #import <Cocoa/Cocoa.h> @interface Song : NSObject @property (nonatomic, copy) NSString* songName; @property (nonatomic, copy) NSString* artist; @property (nonatomic, copy) NSString* album; @property (nonatomic, assign) long length; -(void) SetName:(NSString*)sname artist:(NSString*)art album:(NSString*)alb length:(long)len; -(id) initWithName:(NSString*)sname artist:(NSString*)art album:(NSString*)alb length:(long)len; -(BOOL) isEqualTo:(Song*)thesong; -(NSString*)description; -(NSComparisonResult)sort:(Song*) song2; -(BOOL)hasWord:(NSString*)keyword; @end
#import "Song.h" @implementation Song @synthesize songName,album,artist,length; -(void) SetName:(NSString*)sname artist:(NSString*)art album:(NSString*)alb length:(long)len { songName=sname; album = alb; artist = art; length = len; } -(id) initWithName:(NSString*)sname artist:(NSString*)art album:(NSString*)alb length:(long int)len { if (self=[super init]) { [self SetName:sname artist:art album:alb length:len]; } return self; } - (void)dealloc { songName =nil; artist = nil; album = nil; [super dealloc]; } -(BOOL) isEqualTo:(Song*)theSong { if ([theSong.songName isEqualToString:self.songName ] && [theSong.artist isEqualToString:self.artist ] && [theSong.album isEqualToString:self.album] && theSong.length == self.length) { return true; } return false; } -(NSString*)description { return [NSString stringWithFormat:@"%@ @ %@ from %@ long:%li",songName,artist,album,length]; } -(NSComparisonResult)sort:(Song*) song2 { if (self.length > song2.length) { return NSOrderedDescending; } else if (self.length < song2.length){ return NSOrderedAscending; } return NSOrderedSame; // if ([songName compare:song2.songName]>0) { // return NSOrderedDescending; // } else if ([songName compare:song2.songName]<0){ // return NSOrderedAscending; // } // return NSOrderedSame; } -(BOOL)hasWord:(NSString*)keyword { if ([songName rangeOfString:keyword].length >0 ) { return YES; } return NO; } @end
#import <Foundation/Foundation.h> #import <Cocoa/Cocoa.h> #import "Song.h" @interface PlayList : NSObject @property (nonatomic,retain) Song* song; @property (nonatomic,copy) NSString* name; @property (nonatomic,retain) NSMutableArray* songlist; -(id)initWithName:(NSString*)listname; -(void)addSongtoList:(Song*)theSong; -(void)removeSongFromList:(Song*)theSong; -(void)description; -(void)sortList; -(void)serchForSongName:(NSString*) keyword; @end
#import "PlayList.h" @implementation PlayList @synthesize song,name,songlist; -(id)initWithName:(NSString*)listname { if (self=[super init]) { self.name = listname; self.songlist = [[[NSMutableArray alloc] init] autorelease]; } return self; } -(void)addSongtoList:(Song*)theSong { for (Song * song1 in songlist) { if ([song1 isEqualTo:theSong]) { NSLog(@"歌曲:%@ 已存在!添加失败",theSong.songName); return; } } [songlist addObject:theSong]; NSLog(@"歌曲:%@,添加成功!", theSong.songName); return; } -(void)removeSongFromList:(Song*)theSong { for(Song * song1 in songlist) { if([song1 isEqualTo:theSong]) { [songlist removeObject:song1]; NSLog(@"remooved \"%@\"from %@!",song1.songName ,self.name); return; } } } -(void)description { NSLog(@"列表:%@,所含歌数:%li", name, songlist.count ); NSLog(@"---------------"); for( Song* song1 in songlist) NSLog(@"%@",[song1 description]); } -(void)sortList { [songlist sortUsingSelector:@selector(sort:)]; NSLog(@"排序后:\n"); [self description]; } -(void)serchForSongName:(NSString*) keyword { NSLog(@"在%@中符合查找标准'%@'的歌曲有:\n",name,keyword); for (Song* song1 in songlist) { if ([song1 hasWord:keyword]) NSLog(@"%@",song1.songName); } NSLog(@"--------------"); } @end
#import <Foundation/Foundation.h> #import "PlayList.h" @interface MusicCollection : NSObject @property (nonatomic, copy) PlayList* playlist; @property (nonatomic, retain) NSMutableSet* playSet; @property (nonatomic, retain) NSMutableSet* playSet2; -(void)addPlaylistToSet:(PlayList* )theList; -(void)addPlaylistToSet2:(PlayList* )theList; -(void)removePlaylist; -(void)removePlaylist2; -(void)removePlaylist2:(PlayList*)thelist; -(void)removeSongFromSet:(Song* )song1; -(void)serchForSongName:(NSString*) keyword; @end
#import "MusicCollection.h" @implementation MusicCollection @synthesize playlist,playSet,playSet2; -(void)addPlaylistToSet:(PlayList* )theList { if (playSet.count == 0) { [playSet addObject:theList]; playlist = theList; NSLog(@"%@添加至主播放列表!",theList.name); } else { [self removePlaylist]; [self addPlaylistToSet:theList]; playlist = theList; [self removePlaylist2:theList]; } return; } -(void)addPlaylistToSet2:(PlayList* )theList { [playSet2 addObject:theList]; NSLog(@"%@添加至副播放列表!",theList.name); return; } -(void)removePlaylist { [playSet removeAllObjects]; NSLog(@"已清空播放列表。"); return; } -(void)removePlaylist2 { [playSet2 removeAllObjects]; NSLog(@"已清空播放列表。"); return; } -(void)removePlaylist2:(PlayList*)thelist { if ([playSet2 containsObject:thelist]) { [playSet2 removeObject:thelist]; NSLog(@"移除%@从副歌列表",thelist.name); } } -(void)removeSongFromSet:(Song* )song1 { [playlist removeSongFromList:song1]; for (PlayList* playlist2 in playSet2) { [playlist2 removeSongFromList:song1]; } } -(void)serchForSongName:(NSString*) keyword { [playlist serchForSongName:keyword]; for (PlayList* list2 in playSet2) [list2 serchForSongName:keyword]; } -(id)init { if (self = [super init]) { playSet = [[[NSMutableSet alloc] init] autorelease]; playSet2 = [[[NSMutableSet alloc] init] autorelease]; NSLog(@"添加Set成功!"); } return self; } @end