标签:
Book.h
@interface Book : NSObject
{
NSString *_bookName;
NSString *_bookAuthor;
NSString *_bookPublicHouse;
CGFloat _bookPrice;
}
#pragma mark 初始化方法
-(id)initWithBookName:(NSString *)bookName withBookAuthor:(NSString *)bookAuthor withBookPublicHouse:(NSString *)bookPublicHouse withBookPrice:(CGFloat)bookPrice;
#pragma mark 便利构造器
+(id)bookWithBookName:(NSString *)bookName withBookAuthor:(NSString *)bookAuthor withBookPublicHouse:(NSString *)bookPublicHouse withBookPrice:(CGFloat)bookPrice;
-(void)setBookName:(NSString*)bookName;
-(void)setBookAuthor:(NSString *)bookAuthor;
-(void)setBookPublicHouse:(NSString*)bookPublicHouse;
-(void)setBookPrice:(CGFloat)bookPrice;
-(NSString *)getBookName;
-(NSString *)getBookAuthor;
-(NSString *)getBookPublicHouse;
-(CGFloat)getBookPrice;
@end
Book.m
#import "Book.h"
@implementation Book
- (id)initWithBookName:(NSString *)bookName withBookAuthor:(NSString *)bookAuthor withBookPublicHouse:(NSString *)bookPublicHouse withBookPrice:(CGFloat)bookPrice;
{
self = [super init];
if (self) {
_bookName =bookName;
_bookAuthor = bookAuthor;
_bookPublicHouse = bookPublicHouse;
_bookPrice = bookPrice;
}
return self;
}
#pragma mark 便利构造器
+(id)bookWithBookName:(NSString *)bookName withBookAuthor:(NSString *)bookAuthor withBookPublicHouse:(NSString *)bookPublicHouse withBookPrice:(CGFloat)bookPrice{
Book *b = [[Book alloc ]initWithBookName:bookName withBookAuthor:bookAuthor withBookPublicHouse:bookPublicHouse withBookPrice:bookPrice];
return b;
}
-(void)setBookName:(NSString*)bookName{
_bookName = bookName;
}
-(void)setBookAuthor:(NSString *)bookAuthor{
_bookAuthor = bookAuthor;
}
-(void)setBookPublicHouse:(NSString*)bookPublicHouse{
_bookPublicHouse = bookPublicHouse;
}
-(void)setBookPrice:(CGFloat)bookPrice{
_bookPrice = bookPrice;
}
-(NSString *)getBookName{
return _bookName;
}
-(NSString *)getBookAuthor{
return _bookAuthor;
}
-(NSString *)getBookPublicHouse{
return _bookPublicHouse;
}
-(CGFloat)getBookPrice{
return _bookPrice;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"书名;%@作者:%@出版社:%@ 价钱:%f ",_bookName ,_bookAuthor,_bookPublicHouse,_bookPrice];
}
@end
Library.h
#import <Foundation/Foundation.h>
@class Book;//申明book是一个类
@interface Library : NSObject
{
NSString *_libraryName;
NSMutableArray *_libraryArray;//放书的地方
}
#pragma mark
-(id)initWithLibraryName:(NSString *)libraryName;
# pragma mark
-(void)setLibraryName:(NSString*)libraryName;
-(NSString*)getLibraryName;
#pragma mark 添加图书 思考为什么要有返回值
-(BOOL)addBookToLibrary:(Book*)book;
#pragma mark 删除图书
-(BOOL)deleteBookToLibrary:(Book*)book;
#pragma mark 根据图书的作者来删除图书
-(BOOL)deleteBookFromLibraryByBookAuthor:(NSString *)bookAuthor;
#pragma mark 按照图书名字来查找图书
-(NSArray*)findBookLibraryByName:(NSString *)bookName;
#pragma mark 按照价格来找图书
-(NSArray *)findBookLibraryByPrice:(CGFloat)bookPrice;
#pragma mark 按照作者来找图书,同一作者可以写好多本书
-(NSArray *)findBookLibraryByAuthor:(NSString *)bookAuthor;
#pragma mark 按照出版社来找
-(NSArray *)findBookLibraryByPublicHouse:(NSString *)bookPublicHouse;
#pragma mark 输出所有书
-(void)showAllBooks;
@end
library.m
#import "Library.h"
#import "Book.h"
@implementation Library
- (id)initWithLibraryName:(NSString *)libraryName
{
self = [super init];
if (self) {
_libraryName =libraryName;
_libraryArray = [NSMutableArray arrayWithCapacity:10];//开辟10个空间
}
return self;
}
-(void)setLibraryName:(NSString*)libraryName{
_libraryName = libraryName;
}
-(NSString*)getLibraryName{
return _libraryName;
}
#pragma mark 添加图书 思考为什么要有返回值 里面没有控制,是不是这本图书已经存在?
-(BOOL)addBookToLibrary:(Book*)book{
[_libraryArray addObject:book];
return YES;
}
#pragma mark 删除图书
-(BOOL)deleteBookToLibrary:(Book*)book{
[_libraryArray removeObject:book];
return YES;
}
#pragma mark 根据图书的作者来删除图书
-(BOOL)deleteBookFromLibraryByBookAuthor:(NSString *)bookAuthor{
NSMutableArray *deleteArray = [NSMutableArray array];
//forin 里面不可以直接删除元素
for (Book *temp in _libraryArray) {
if ([[ temp getBookAuthor ] isEqualToString: bookAuthor]) {
// [_libraryArray removeObjects:temp];会造成崩溃
[deleteArray addObject:temp];
}
}
// [_libraryArray removeObjectsInArray:deleteArray];
return YES;
}
#pragma mark 按照图书名字来查找图书
-(NSArray*)findBookLibraryByName:(NSString *)bookName{
NSMutableArray *array = [NSMutableArray array];
for (Book *temp in _libraryArray ) {
if ([[ temp getBookName] isEqualToString:bookName]) {
[array addObject:temp];
}
}
return array;
}
#pragma mark 按照价格来找图书
-(NSArray *)findBookLibraryByPrice:(CGFloat)bookPrice{
NSMutableArray *array = [NSMutableArray array];
for (Book * temp in _libraryArray) {
if ([temp getBookPrice] -bookPrice<0.000001 && bookPrice - [temp getBookPrice]<0.000001) {
[array addObject:temp];
}
}
return array;
}
#pragma mark 按照作者来找图书,同一作者可以写好多本书
-(NSArray *)findBookLibraryByAuthor:(NSString *)bookAuthor{
NSMutableArray *array = [NSMutableArray array];
for (Book *temp in _libraryArray) {
if ([[temp getBookAuthor] isEqualToString:bookAuthor]) {
[array addObject:temp];
}
}
return array;
}
#pragma mark 按照出版社来找
-(NSArray *)findBookLibraryByPublicHouse:(NSString *)bookPublicHouse
NSMutableArray *array = [NSMutableArray array];
for (Book *temp in _libraryArray) {
if ([[temp getBookPublicHouse] isEqualToString: bookPublicHouse ]) {
[array addObject:temp];
}
}
return array;
}
#pragma mark 输出所有书
-(void)showAllBooks{
for (Book *temp in _libraryArray) {
NSLog(@"%@",temp);
}
}
main.m
#import <Foundation/Foundation.h>
#import "Book.h"
#import "Library.h"
int main(int argc, const char * argv[]) {
Book *aBook1 =[[Book alloc]initWithBookName:@"chuanyue" withBookAuthor:@"lha" withBookPublicHouse:@"renminchubanshe" withBookPrice:100];
Book *book2 = [Book bookWithBookName:@"小时代" withBookAuthor:@"guojingming" withBookPublicHouse:@"长江出版社" withBookPrice:36];
Book *book3 = [Book bookWithBookName:@"梦里活落知多少" withBookAuthor:@"guojingming" withBookPublicHouse:@"长江出版社" withBookPrice:2.5];
Book *book4= [Book bookWithBookName:@"左耳" withBookAuthor:@" 饶雪漫" withBookPublicHouse:@"北师大出版社" withBookPrice:80];
Book *book5= [Book bookWithBookName:@"别惹逆世九小姐" withBookAuthor:@"牡丹峰" withBookPublicHouse:@"广州出版社" withBookPrice:49];
Book *book6= [Book bookWithBookName:@"妻子的谎言" withBookAuthor:@"李夏喜" withBookPublicHouse:@"清华出版社" withBookPrice:80];
Book *book7= [Book bookWithBookName:@"绝代双骄" withBookAuthor:@"恶人" withBookPublicHouse:@"台湾出版社" withBookPrice:170];
Book *book8= [Book bookWithBookName:@"快乐大本营" withBookAuthor:@"谢娜,何老师" withBookPublicHouse:@"长沙出版社" withBookPrice:89];
// 创建一个图书馆
Library *library = [[Library alloc]initWithLibraryName:@"北京150407"];
// 往图书馆添加一本书
[library addBookToLibrary:aBook1 ];
[library addBookToLibrary:book2 ];
[library addBookToLibrary:book3 ];
[library addBookToLibrary:book4 ];
[library addBookToLibrary:book5 ];
[library addBookToLibrary:book6 ];
[library addBookToLibrary:book7 ];
[library addBookToLibrary:book8 ];
// 展示所有的图书
[library showAllBooks];
// 删除一本书(根据作者)
[library deleteBookFromLibraryByBookAuthor:@"猫"];
printf("\n");
[library showAllBooks];
printf("\n\n\n");
NSArray *foundBookArray = [library findBookLibraryByPrice:89];
NSLog(@"价格为50元的图书如下:") ;
for (Book *book in foundBookArray) {
NSLog(@"%@",book);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/as5346/p/4469628.html