图书馆
//
// Book.h
// lanou
//
// Created by lanou3g on 15/4/29.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
{
NSString * _bookName;//图书名字
NSString * _bookAuther;//图书作者
NSString * _bookPublicHouse;//图书出版社
CGFloat _bookPrice;//图书价格
}
//初始化方法
-(id)initWithBookName:(NSString *)bookName WithBookAuther:(NSString *)bookAuther WithBookPublicHouse:(NSString *)bookPublicHouse WithBookPrice:(CGFloat)bookPrice;
//便利构造器
+(id)bookWithBookName:(NSString *)bookName WithBookAuther:(NSString *)bookAuther WithBookPublicHouse:(NSString *)bookPublicHouse WithBookPrice:(CGFloat)bookPrice;
//set赋值
-(void)setBookName:(NSString *)bookName;
-(void)setBookAuther:(NSString *)bookAuther;
-(void)setBookPublicHouse:(NSString *)bookPublicHouse;
-(void)setBookPrice:(CGFloat)bookPrice;
//get取值
-(NSString *)getBookName;
-(NSString *)getBookAuther;
-(NSString *)getBookPublicHouse;
-(CGFloat)getBookPrice;
@end
//
// Book.m
// lanou
//
// Created by lanou3g on 15/4/29.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "Book.h"
@implementation Book
-(id)initWithBookName:(NSString *)bookName WithBookAuther:(NSString *)bookAuther WithBookPublicHouse:(NSString *)bookPublicHouse WithBookPrice:(CGFloat)bookPrice
{
self = [super init];
if (self) {
_bookName = bookName;
_bookAuther = bookAuther;
_bookPublicHouse = bookPublicHouse;
_bookPrice = bookPrice;
}
return self;
}
+(id)bookWithBookName:(NSString *)bookName WithBookAuther:(NSString *)bookAuther WithBookPublicHouse:(NSString *)bookPublicHouse WithBookPrice:(CGFloat)bookPrice
{
Book * book = [[Book alloc]initWithBookName:bookName WithBookAuther:bookAuther WithBookPublicHouse:bookPublicHouse WithBookPrice:bookPrice];
return book;
}
-(void)setBookName:(NSString *)bookName
{
_bookName = bookName;
}
-(void)setBookAuther:(NSString *)bookAuther
{
_bookAuther = bookAuther;
}
-(void)setBookPublicHouse:(NSString *)bookPublicHouse
{
_bookPublicHouse = bookPublicHouse;
}
-(void)setBookPrice:(CGFloat)bookPrice
{
_bookPrice = bookPrice;
}
-(NSString *)getBookName
{
return _bookName;
}
-(NSString *)getBookAuther
{
return _bookAuther;
}
-(NSString *)getBookPublicHouse
{
return _bookPublicHouse;
}
-(CGFloat)getBookPrice
{
return _bookPrice;
}
#pragma make 重写打印需要展示内容的方法
-(NSString *)description
{
return [NSString stringWithFormat:@"%@ %@ %@ %.2f",_bookName,_bookAuther,_bookPublicHouse,_bookPrice];
}
@end
//
// Library.h
// lanou
//
// Created by lanou3g on 15/4/29.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#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;
#pragma mark 访问器(图书馆的名字)
-(NSString *)getLibraryName;
#pragma make 添加图书
-(BOOL)addBookToLibrary:(Book *)book;
#pragma make 删除图书
-(BOOL)deleteBookFromLibrary:(Book *)book;
#pragma make 根据作者删除图书
-(BOOL)deleteBookFromLiByBookAuther:(Book *)bookAuther;
#pragma make 按照图书名字查找图书
-(NSArray *)findBookFromLibraryByName:(NSString *)bookName;
#pragma make 按照价格找图书
-(NSArray *)findBookFromLibraryByPrice:(CGFloat)bookPrice;
#pragma make 按照作者找
-(NSArray *)findBookFromLibraryByAuther:(NSString *)bookAuther;
#pragma make 按照出版社找
-(NSArray *)findBookFromLibraryByHouse:(NSString *)bookPublicHouse;
#pragma make 展示所有书籍
-(void)showAllBooks;
@end
//
// Library.m
// lanou
//
// Created by lanou3g on 15/4/29.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "Library.h"
#import "Book.h"
@implementation Library
#pragma mark 初始化方法
-(id)initWithLibraryName:(NSString *)libraryName
{
self = [super init];
if (self) {
_libraryName = libraryName;
_libraryArray = [NSMutableArray array];
}
return self;
}
#pragma mark 设置器(图书馆的名字)
-(void)setLibraryName:(NSString *)libraryName
{
_libraryName = libraryName;
}
#pragma mark 设置器(图书馆的名字)
-(NSString *)getLibraryName
{
return _libraryName;
}
#pragma make 添加图书
-(BOOL)addBookToLibrary:(Book *)book
{
[_libraryArray addObject:book];
return YES;
}
#pragma make 删除图书
-(BOOL)deleteBookFromLibrary:(Book *)book
{
[_libraryArray removeObject:book];
return YES;
}
#pragma make 根据作者删除图书
-(BOOL)deleteBookFromLiByBookAuther:(NSString *)bookAuther
{
NSMutableArray * deleteArray = [NSMutableArray array];
// forin里面不可以直接删除元素 .要删就创建一个数组,把要删的元素存到里面,在一起删
for (Book * temp in _libraryArray) {
if ([[temp getBookAuther]isEqualToString:bookAuther]) {
[deleteArray addObject:temp];
// [_libraryArray removeObject:temp];这种做法不可取。。。会找成系统崩溃
}
}
[_libraryArray removeObject:deleteArray];
return YES;
}
#pragma make 按照图书名字查找图书
-(NSArray *)findBookFromLibraryByName:(NSString *)bookName
{
NSMutableArray * array = [NSMutableArray array];
for (Book * temp in _libraryArray) {
if ([[temp getBookName]isEqualToString:bookName]) {
[array addObject:temp];
}
}
return array;
}
#pragma make 按照价格找图书
-(NSArray *)findBookFromLibraryByPrice:(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 make 按照作者找
-(NSArray *)findBookFromLibraryByAuther:(NSString *)bookAuther
{
NSMutableArray * array = [NSMutableArray array];
for (Book * temp in _libraryArray) {
if ([[temp getBookAuther]isEqualToString:bookAuther]) {
[array addObject:temp];
}
}
return array;
}
#pragma make 按照出版社找
-(NSArray *)findBookFromLibraryByHouse:(NSString *)bookPublicHouse
{
NSMutableArray * array = [NSMutableArray array];
for (Book * temp in _libraryArray) {
if ([[temp getBookPublicHouse]isEqualToString:bookPublicHouse]) {
[array addObject:temp];
}
}
return array;
}
#pragma make 展示所有书籍
-(void)showAllBooks
{
for (Book * temp in _libraryArray) {
NSLog(@"%@ %@ %@ %.2f",[temp getBookName],[temp getBookAuther],[temp getBookPublicHouse],[temp getBookPrice]);
}
}
@end
//
// main.m
// lanou
//
// Created by lanou3g on 15/4/29.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Book.h"
#import "Library.h"
int main(int argc, const char * argv[])
{
Book * book = [Book bookWithBookName:@"坏蛋是怎样炼成的" WithBookAuther:@"六道" WithBookPublicHouse:@"中国出版" WithBookPrice:10];
// NSLog(@"%@ %@ %@ %f",[book getBookName],[book getBookAuther],[book getBookPublicHouse],[book getBookPrice]);
Book * book1 = [Book bookWithBookName:@"大宋的智慧" WithBookAuther:@"子鱼" WithBookPublicHouse:@"长江出版社" WithBookPrice:50];
Book * book2 = [Book bookWithBookName:@"钢铁是怎样炼成的" WithBookAuther:@
"司机"WithBookPublicHouse:@"人民出版社" WithBookPrice:120];
Book * book3 = [Book bookWithBookName:@"平凡的世界" WithBookAuther:@"路遥" WithBookPublicHouse:@"盗版" WithBookPrice:250];
Book * book4 = [Book bookWithBookName:@"小时代" WithBookAuther:@"郭敬明" WithBookPublicHouse:@"长江出版社" WithBookPrice:36];
Book * book5 = [Book bookWithBookName:@"梦里花落知多少" WithBookAuther:@"郭敬明" WithBookPublicHouse:@"长江出版社" WithBookPrice:2.5];
// 创建一个图书馆
Library * library = [[Library alloc]initWithLibraryName:@"北京150407"];
// 往图书馆添加一本书
[library addBookToLibrary:book];
[library addBookToLibrary:book1];
[library addBookToLibrary:book2];
[library addBookToLibrary:book3];
[library addBookToLibrary:book4];
[library addBookToLibrary:book5];
//// 展示所有图书
// [library showAllBooks];
// printf(" \n");
//// 删除一本书
//
// [library deleteBookFromLiByBookAuther:@"司机"];
// [library showAllBooks];
//
NSArray * foundBookArray = [library findBookFromLibraryByPrice:2.5];
for (Book * books in foundBookArray) {
NSLog(@"%@",books);
}
return 0;
}