@interface Book : NSObject
@property (nonatomic, copy) NSString *bookName;
@property (nonatomic) NSInteger price;
- (id)initWithBookName:(NSString *)bookName price:(NSInteger)price;
@end
#import "Book.h"
@implementation Book
- (id)initWithBookName:(NSString *)bookName price:(NSInteger)price{
self = [super init];
if (self) {
self.bookName = bookName;
self.price = price;
}
return self;
}
- (void)setBookName:(NSString *)bookName{
if (_bookName != bookName) {
[_bookName release];
_bookName = [bookName retain];
}
}
//重写NSLog方法
- (NSString *)description{
return [NSString stringWithFormat:@"书名是:%@, 价格是:%ld", _bookName, _price];
}
- (void)dealloc{
[_bookName release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
@class Book;
@interface Library : NSObject
//2添加某本书
- (BOOL)addBook:(Book *)book;
//3删除某本书
- (void)removeBook:(NSString *)name;
//4查找某本书并且修改价格
- (void)lookForBook:(NSString *)name;
//5展示所有书籍
- (void)showAllBooks;
//6将书本按照价格从高到低进行排序
- (void)sortBookByPrice;
//7对书籍根据书名升序排序
- (void)sortBookByName;
//8记录图书馆中书名一样的书籍个数
- (void)repeatBook:(NSString *)book;
@end
#import "Library.h"
#import "Book.h"
@interface Library ()
@property (nonatomic, retain) NSMutableArray *array;
@end
@implementation Library
- (void)dealloc{
[_array release];
[super dealloc];
}
//初始化
- (instancetype)init {
if(self = [super init]) {
self.array = [NSMutableArray arrayWithCapacity:0];
}
return self;
}
//添加某本书
- (BOOL)addBook:(Book *)book {
if (book.bookName.length == 0) {
return NO;
} else {
[_array addObject:book];
return YES;
}
}
//删除某本书
- (void)removeBook:(NSString *)name {
if (name.length == 0) {
NSLog(@"请输入正确的书名");
return;
}
for (NSInteger i = 0; i < _array.count; i++) {
Book *book = [_array objectAtIndex:i];
if ([book.bookName isEqualToString:name]) {
NSLog(@"成功删除了%@", name);
[_array removeObjectAtIndex:i];
}
}
}
//查找某本书且修改价格
- (void)lookForBook:(NSString *)name {
if (name.length == 0) {
NSLog(@"请输入正确的书名");
return;
}
for (int i = 0; i < [_array count]; i++) {
Book *book = [_array objectAtIndex:i];
if ([book.bookName isEqualToString:name]) {
NSLog(@"找到了这本书%@", [_array objectAtIndex:i]);
book.price = 1000;
NSLog(@"更改之后的书:%@", [_array objectAtIndex:i]);
}
}
}
//展示所有书籍
- (void)showAllBooks {
for (Book *book in _array) {
NSLog(@"%@", book);
}
}
//对书本排序
- (void)sortBookByPrice {
BOOL flag = YES;
for (NSInteger i = 0; i < _array.count - 1 || flag; i++) {
flag = NO;
for (NSInteger j = 0; j < _array.count - 1 - i; j++) {
if ([_array[j] price] > [_array[j + 1] price]) {
[_array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
flag = YES;
}
}
}
}
- (void)sortBookByName {
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"bookName" ascending:YES];
[_array sortUsingDescriptors:@[descriptor]];
}
- (void)repeatBook:(NSString *)bookName{
int count = 0;
for (Book *book in _array) {
if ([book.bookName isEqualToString:bookName]) {
count++;
}
}
NSLog(@"%d", count);
}
@end
#import "Book.h"
#import "Library.h"
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
Library *libary = [[Library alloc]init];
Book *book = [[Book alloc]initWithBookName:@"C语言基础" price:100];
Book *book1 = [[Book alloc]initWithBookName:@"OC之如何与水杯相处" price:1000];
Book *book2 = [[Book alloc]initWithBookName:@"UI之东游记白骨精的爱情故事" price:400];
Book *book3 = [[Book alloc]initWithBookName:@"Java之为为何你还是单身" price:30];
//1添加某本书
[libary addBook:book];
[libary addBook:book1];
[libary addBook:book2];
[libary addBook:book3];
[libary showAllBooks];
NSLog(@"----------华丽的分割线------------");
/*
//2删除某本书
char str[100];
NSLog(@"请输入书名:");
scanf("%s",str);
NSString *str1 = [[NSString alloc]initWithUTF8String:str];
[libary removeBook:str1];
[libary showAllBooks];
NSLog(@"----------华丽的分割线------------");
//3查找某本书并且修改价格
NSLog(@"请输入书名:");
scanf("%s",str);
NSString *str2 = [[NSString alloc]initWithUTF8String:str];
[libary lookForBook:str2];
NSLog(@"----------华丽的分割线------------");
*/
//4展示所有书籍
[libary showAllBooks];
NSLog(@"----------华丽的分割线------------");
//5对书籍根据价格排序
[libary sortBookByPrice];
[libary showAllBooks];
NSLog(@"----------华丽的分割线------------");
//6 对书籍根据名字升序排序
[libary sortBookByName];
[libary showAllBooks];
//7 记录同名书籍数
[libary repeatBook:@"C语言基础"];
return 0;
}