简单通讯录的实现 main..h .m文件全部

//

//  main.m

//  OC - 数组作业

//

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

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

//


#import <Foundation/Foundation.h>

#import "Contact.h"


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

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

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

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

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

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

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

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

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

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

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

    


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

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

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

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

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

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

    

//    NSMutableArray *arr = [NSMutableArray arrayWithObjects:str1, str2, str3, nil];

    NSMutableArray *arr = [NSMutableArray array];

    //添加新的联系人

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

//        NSLog(@"添加失败");

//    } else {

//         [arr addObject:str1];

//    }

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

//        NSLog(@"添加失败");

//    } else {

//        [arr addObject:str2];

//    }

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

//        NSLog(@"添加失败");

//    } else {

//        [arr addObject:str3];

//    }

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

//        NSLog(@"添加失败");

//    } else {

//        [arr addObject:str4];

//    }

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

//        NSLog(@"添加失败");

//    } else {

//        [arr addObject:str5];

//    }

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

//        NSLog(@"添加失败");t

//    } else {

//        [arr addObject:str6];

//    }

//    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]);

//    }

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

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

//        if ([[[arr objectAtIndex:i] fenzu] isEqualToString:@"family"]) {

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

//        }

//    }

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

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

//        if ([[[arr objectAtIndex:i] phone] isEqualToString:@"15802445035"]) {

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

//        }

//    }

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

    

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

//        if ([[[arr objectAtIndex:i] sex] isEqualToString:@""]) {

//            

//            [[arr objectAtIndex:i] show];

//        

//    }

//    }

    //根据姓名删除联系人

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

//        if ([[[arr objectAtIndex:i] name] isEqualToString:@"张博"]) {

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

//            [arr removeObject:b];

//              i = i - 1;

//

//        }

//    }

//    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]);

//            }

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

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

//        if ([[[arr objectAtIndex:i] fenzu] isEqualToString:@"girl"]) {

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

//            [arr removeObject:b];

//            i = i - 1;

//            

//        }

//    }

    //展示通讯录的所有联系人


//    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]);

//       

//    }


    

    

    

    

    

    return 0;

}

//

//  Contact.h

//  OC - 数组作业

//

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

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

//


#import <Foundation/Foundation.h>


@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;

- (void)show;

- (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;

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

//    1、定义联系人类Contact。实例变量:姓名、性别、电话号码、住址、分组名称。方法:初始化方法(姓名、电话号码)、显示联系人信息

@end


//

//  Contact.m

//  OC - 数组作业

//

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

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

//


#import "Contact.h"


@implementation Contact

- (void)show

{

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

}

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




posted @ 2015-11-08 13:46  挽月细数风流  阅读(217)  评论(0编辑  收藏  举报