OC容器——图书馆 .m 文件

//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值