// Created by Scott on 14-9-20.
// Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//
#import "Library.h"
@implementation Library
// 初始化方法
- (id)initWithLib:(NSMutableArray *)lib
{
self = [super init];
if (self) {
self.lib = lib;
}
return self;
}
// 增加图书信息(书名、作者、出版社、编号等)。同时添加上书的编号
- (void)addBookWithName:(NSString *)name
andAuthor:(NSString *)author
andPress:(NSString *)press
{
// 创建一个字典
NSNumber *i = [NSNumber numberWithUnsignedLong:[self.lib count] + 1];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"书名",
author, @"作者",
press,@"出版社",
i, @"编号", nil];
[self.lib addObject:dic];
}
// 删除图书
- (void)deleteBookWithName:(NSString *)name
{
int j = 0;
for (int i = 0 ; i < [self.lib count]; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.lib[i]];
if ([dic objectForKey:@"书名"] == name) {
[self.lib removeObject:dic];
NSLog(@"图书《%@》已从我的图书馆删除", name);
j++;
}
}
if (j == 0) {
NSLog(@"对不起,我的图书馆没有《%@》这本书", name);
}
}
// 修改图书馆书籍;
- (void)modifyBookWithName:(NSString *)name
andAuthor:(NSString *)author
andPress:(NSString *)press
{
int j = 0;
for (int i = 0 ; i < [self.lib count]; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.lib[i]];
if ([dic objectForKey:@"书名"] == name) {
[dic setObject:name forKey:@"书名"];
[dic setObject:author forKey:@"作者"];
[dic setObject:press forKey:@"出版社"];
j++;
}
}
if (j == 0) {
NSLog(@"对不起,我的图书馆没有《%@》这本书", name);
}
}
// 根据书名查找书名
- (void)findBook:(NSString *)name
{
int j = 0 ;
for (int i = 0 ; i < [self.lib count]; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.lib[i]];
if ([dic objectForKey:@"书名"] == name) {
for (NSString *key in dic) {
NSLog(@"%@ : %@", key, [dic objectForKey:key]);
j++;
}
}
}
if (j == 0) {
NSLog(@"对不起,我的图书馆没有《%@》这本书", name);
}
}
// 书籍清单(书名)
- (void)listOfBook
{
NSLog(@"我的图书馆共有%lu本藏书", [self.lib count]);
for (int i = 0; i< [self.lib count]; i++) {
NSLog(@"%@", [self.lib[i] valueForKey:@"书名"]);
}
}
// 展示图书馆里面书籍
- (void)listOfLibrary
{
NSLog(@"我的图书馆共有%lu本藏书", [self.lib count]);
for (int i = 0; i< [self.lib count]; i++) {
for (NSString *key in self.lib[i]) {
NSLog(@"%@ : %@", key, [self.lib[i] objectForKey:key]);
}
NSLog(@"**************************");
}
}
@end
OC容器——图书馆 .m 文件
最新推荐文章于 2016-08-11 14:33:59 发布