通讯录封装实现

//

//  main.m

//  oc - 作业(封装实现)

//

//  Created by dllo on 15/10/28.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "AddressBook.h"

#import "Contact.h"

int main(int argc, const char * argv[]) {

    //    实现简单通讯录操作。

    //    方法:初始化方法(姓名、电话号码)、显示联系人信息

    //    2、在main.m中定义可变数组,管理所有联系人。可以添加新联系人对象,如果姓名或电话号码为空,打印添加失败。

    //    3、获取某个分组下的所有联系人。

    //    4、根据电话号码搜索联系人。

    //    5、获取所有女性联系人

    //    6、根据姓名删除联系人

    //    7、删除某个分组全部联系人

    //    8、展示通讯录中所有联系人

    //    9、选做:定义AddressBook类,封装上述功能。

    AddressBook *p = [[AddressBook alloc]init];

  

//    Contact *str1 = [Contact contactWithName:@"余浩" sex:@"" phone:@"15802445035" address:@"大连" fenzu:@"friend"] ;

    [p addContact:[Contact contactWithName:@"余浩" sex:@"" phone:@"15802445035" address:@"大连" fenzu:@"friend"]];

    [p addContact:[Contact contactWithName:@"余芮" sex:@"" phone:@"1583425455" address:@"北京" fenzu:@"family"]];

   // Contact *str2 = [Contact contactWithName:@"余芮" sex:@"" phone:@"1583425455" address:@"北京" fenzu:@"family"];

    //[p addContact:str2 :arr];

//    Contact *str3 = [Contact contactWithName:@"张博" sex:@"" phone:@"15834324643" address:@"沈阳" fenzu:@"class"];

    [p addContact:[Contact contactWithName:@"张博" sex:@"" phone:@"15834324643" address:@"沈阳" fenzu:@"class"]];

   // Contact *str4 = [Contact contactWithName:@"袁俊" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"];

    [p addContact:[Contact contactWithName:@"袁俊" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"]];

    //Contact *str6 = [Contact contactWithName:@"王琼" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"];

    [p addContact:[Contact contactWithName:@"王琼" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"]];

    //Contact *str5 = [Contact contactWithName:@"" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"];

    [p addContact:[Contact contactWithName:@"" sex:@"" phone:@"158342343434" address:@"合肥" fenzu:@"girl"]];

   

    return 0;

}

//

//  Contact.h

//  oc - 作业(封装实现)

//

//  Created by dllo on 15/10/28.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

@class AddressBook;

@interface Contact : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *sex;

@property (nonatomic, copy) NSString *phone;

@property (nonatomic, copy) NSString *address;

@property (nonatomic, copy) NSString *fenzu;

- (id)initWithName:(NSString *

)name  phone:(NSString *)phone;

  - (id)initWithName:(NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address fenzu:(NSString *)fenzu;


+ (id)contactWithName:(NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address fenzu:(NSString *)fenzu;

@end


//

//  Contact.m

//  oc - 作业(封装实现)

//

//  Created by dllo on 15/10/28.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Contact.h"

#import "AddressBook.h"

@implementation Contact

- (id)initWithName:(NSString *)name  phone:(NSString *)phone

{

    self = [super init];

    if (self) {

        [self setName:name];

        [self setPhone:phone];

    }

    return self;

}


- (id)initWithName:(NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address fenzu:(NSString *)fenzu

{

    self = [super init];

    if (self) {

        [self setAddress:address];

        [self setName:name];

        [self setPhone:phone];

        [self setFenzu:fenzu];

        [self setSex:sex];

        

    }

    return self;

}

+ (id)contactWithName:(NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address fenzu:(NSString *)fenzu

{

    Contact *p = [[Contact alloc]initWithName:name sex:sex phone:phone address:address fenzu:fenzu];

    return p;

    

}

@end


//

//  AddressBook.h

//  oc - 作业(封装实现)

//

//  Created by dllo on 15/10/28.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

@class Contact;

@interface AddressBook : NSObject

@property (nonatomic, retain)NSMutableArray *arr;

// 添加联系人

- (void)addContact:(Contact *)a ;

//打印数组

- (void)printContact:(NSMutableArray *)arr;

//打印单个联系方式

- (void)print1Contact:(Contact *)a;

//获取某个分组下得所有联系人

- (void)getFenzuContact:(Contact *)fenzu ;

//根据电话号码搜索联系人

- (void)selectPhoneContact:(Contact *)phone ;

//获取所有的女性联系人

- (void)selectSexContact:(Contact *)sex ;

//根据姓名删除联系人

- (void)selectsNameDeleteContact:(Contact *)name ;

  //删除某个分组的全部联系人

- (void)selectsFenzuDeleteContact:(Contact *)fenzu;

@end


//

//  AddressBook.m

//  oc - 作业(封装实现)

//

//  Created by dllo on 15/10/28.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "AddressBook.h"

#import "Contact.h"

@implementation AddressBook

- (id)init

{

    if (self = [super init]) {

        self.arr = [[NSMutableArray alloc]init];

    }

    return self;

}

//添加联系人

- (void)addContact:(Contact *)a

{

    if ([[a  name] isEqualToString: @""] || [[a phone] isEqualToString:@""]) {

                NSLog(@"添加失败");

                NSLog(@"%@%@%@%@%@", [a name], [a sex], [a phone], [a address], [a fenzu] );

            } else {

                 [self.arr addObject:a];

            }

}

//展示所有

- (void)printContact:(NSMutableArray *)arr

{

        for (NSInteger i = 0; i < arr.count ; i++) {

    

            NSLog(@"%@%@%@%@%@", [[arr objectAtIndex:i] name], [[arr objectAtIndex:i] sex], [[arr objectAtIndex:i] phone], [[arr objectAtIndex:i] address], [[arr objectAtIndex:i] fenzu]);

    

        }

}

//打印单个联系方式

- (void)print1Contact:(Contact *)a

{

    NSLog(@"%@%@%@%@%@", [a name], [a sex], [a phone], [a address], [a fenzu] );

}

//获取某个分组下得所有联系人

- (void)getFenzuContact:(NSString *)fenzu

{

    for (NSInteger i = 0; i < [self.arr count]; i++) {

        if ([[[self.arr objectAtIndex:i] fenzu] isEqualToString:fenzu]) {

            Contact *b = [self.arr objectAtIndex:i];

            [self print1Contact:b];

        }

    }

}

//根据电话号码搜索联系人

- (void)selectPhoneContact:(NSString *)phone

{

    for (NSInteger i = 0; i < self.arr.count; i++) {

                if ([[[self.arr objectAtIndex:i] phone] isEqualToString:phone]) {

                    Contact *b = [self.arr objectAtIndex:i];

                    [self print1Contact:b];

                }

            }

}

//获取所有的女性联系人

- (void)selectSexContact:(NSString *)sex

{

    for (NSInteger i = 0; i <self.arr.count  ; i++) {

                if ([[[self.arr objectAtIndex:i] sex] isEqualToString:sex]) {

                    Contact *b = [self.arr objectAtIndex:i];

                    [self print1Contact:b];

        

            }

            }

}

//根据姓名删除联系人

- (void)selectsNameDeleteContact:(NSString *)name

{

    for (NSInteger i = 0; i <_arr.count; i++) {

                if ([[[self.arr objectAtIndex:i] name] isEqualToString:name]) {

                    Contact *b = [self.arr objectAtIndex:i];

                    [self.arr removeObject:b];

                      i = i - 1;

        

                }

            }

}

//删除某个分组的全部联系人

- (void)selectsFenzuDeleteContact:(NSString *)fenzu

{

        for (NSInteger i = 0; i <self.arr.count; i++) {

                if ([[[self.arr objectAtIndex:i] fenzu] isEqualToString:fenzu]) {

//                    Contact *b = [self.arr objectAtIndex:i];

                    [self.arr removeObject:[self.arr objectAtIndex:i]];

                    i = i - 1;

        

                }

            }

    

}


@end



转载于:https://www.cnblogs.com/yuhaojishuboke/p/5043119.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值