OC----通讯录实现

OC----通讯录实现_第1张图片

OC----通讯录实现_第2张图片

OC----通讯录实现_第3张图片

OC----通讯录实现_第4张图片

OC----通讯录实现_第5张图片

OC----通讯录实现_第6张图片

OC----通讯录实现_第7张图片

OC----通讯录实现_第8张图片

OC----通讯录实现_第9张图片

OC----通讯录实现_第10张图片

main.m

#import
#import "Person.h"
#import "AddressBook.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 创建按Person 对象
        Person *per1 = [Person personWithName:@"kkk" sex:@"男" phoneNumber:@"123" age:18];
        Person *per2 = [Person personWithName:@"xxx" sex:@"女" phoneNumber:@"111" age:16];
        Person *per3 = [Person personWithName:@"ddd" sex:@"男" phoneNumber:@"222" age:17];
        
        // 创建 通讯录
        AddressBook *addressBook = [[[AddressBook alloc] init] autorelease];
        NSLog(@"------------------添加联系人----------------");
        // 添加联系人
        BOOL a = [addressBook addPerson:per1];
        BOOL b = [addressBook addPerson:per2];
        BOOL c = [addressBook addPerson:per3];
        NSLog(@"是否成功添加: %hhd %hhd %hhd", a, b, c);
        
        NSLog(@"--------------根据号码查找联系人--------------");
        // 根据电话号码查询联系人
        Person *str = [addressBook findPersonWithPhineNum:@"123"];
        NSLog(@"%@", str);
        
        NSLog(@"---------根据性别查找联系人,并按年龄排序--------");
        // 根据性别查找联系人 并按年龄排序
        NSArray *array = [addressBook findPersonsWithSex:@"男"];
        for (Person *str in array) {
            NSLog(@"%@", str);
        }
       
        NSLog(@"------------根据姓名更改联系人信息-------------");
        // 根据姓名更改联系人信息
        BOOL d = [addressBook changePersonWithName:@"kkk" phoneNum:@"333" sex:@"女" age:18];
        NSLog(@"%hhd", d);
       
        NSLog(@"--------------按照姓名删除联系人---------------");
        // 根据姓名删除联系人
        BOOL e = [addressBook deletePersonWithName:@"kkk"];
        NSLog(@"%hhd", e);
        
        NSLog(@"----------------删除一个分组------------------");
        BOOL f = [addressBook deleteGroup:@"X"];
        NSLog(@"%hhd", f);
        
        
        NSLog(@"-------------展示通讯录所有联系人---------------");
        [addressBook showAll];
        
    }
    return 0;
}

AddressBook.h

#import
#import "Person.h"

@interface AddressBook : NSObject

@property (nonatomic, retain) NSMutableDictionary *allPersonDic; // 管理联系人字典


// 初始化
- (instancetype)init;


// 添加联系人
- (BOOL)addPerson:(Person *)aPerson;

// 根据电话查找某个联系人
- (Person *)findPersonWithPhineNum:(NSString *)aPhoneNum;

// 根据性别查找联系人,并将查到的联系人按照年龄升序排序
- (NSArray *)findPersonsWithSex:(NSString *)aSex;

// 根据姓名,更改联系人信息
- (BOOL)changePersonWithName:(NSString *)name
                    phoneNum:(NSString *)aNum
                         sex:(NSString *)aSex
                         age:(NSInteger)aAge;



// 按照姓名删除联系人
- (BOOL)deletePersonWithName:(NSString *)aPersonName;



// 删除一个分组
- (BOOL)deleteGroup:(NSString *)aGroupName;

// 展示通讯录中所有联系人信息
- (void)showAll;


@end

AddressBook.m

#import "AddressBook.h"

@implementation AddressBook


- (void)dealloc {
    NSLog(@"AddressBook dealloc");
    
    [self.allPersonDic dealloc];
    [super dealloc];
}

- (instancetype)init {
    if (self = [super init]) {
    self.allPersonDic = [[NSMutableDictionary alloc] initWithCapacity:1];
    }
    return self;
}

// 添加联系人
- (BOOL)addPerson:(Person *)aPerson {
    if (aPerson.name == nil || aPerson.phoneNumber == nil) {
        NSLog(@"添加失败");
        return NO;
    }
    NSMutableArray *groupArray = [self.allPersonDic objectForKey:aPerson.groupName];
    if (groupArray == nil) {
        groupArray = [NSMutableArray arrayWithCapacity:1];
        [groupArray addObject:aPerson];
        [self.allPersonDic setObject:groupArray forKey:aPerson.groupName];
    }
    else {
        [groupArray addObject:aPerson];
    }
    return YES;
}

// 根据电话查找某个联系人
- (Person *)findPersonWithPhineNum:(NSString *)aPhoneNum {
    for (NSString *key in self.allPersonDic) {
         NSMutableArray *array = [self.allPersonDic objectForKey:key];
        for (Person *str in array) {
            if ([str.phoneNumber isEqualToString:aPhoneNum]) {
                return str;
            }
        }
    }return nil;
}

// 根据性别查找联系人,并将查到的联系人按照年龄升序排序
- (NSArray *)findPersonsWithSex:(NSString *)aSex {
    NSMutableArray *sexArray = [NSMutableArray arrayWithCapacity:1];
    for (NSString *key in self.allPersonDic) {
        NSMutableArray *array = [self.allPersonDic objectForKey:key];
        for (Person *str in array) {
            if ([str.sex isEqualToString:aSex]) {
                [sexArray addObject:str];
            }
        }
    }
    [sexArray sortUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2) {
        return [@([obj1 age]) compare:@([obj2 age])];
    }];
    return sexArray;
}


// 根据姓名,更改联系人信息
- (BOOL)changePersonWithName:(NSString *)name
                    phoneNum:(NSString *)aNum
                         sex:(NSString *)aSex
                         age:(NSInteger)aAge
{
    for (NSString *key in self.allPersonDic) {
        NSMutableArray *array = [self.allPersonDic objectForKey:key];
        for (Person *str in array) {
            if ([str.name isEqualToString:name]) {
                str.sex = aSex;
                str.phoneNumber = aNum;
                str.age = aAge;
            }
            return YES;
        }
    }return NO;
}

// 按照姓名删除联系人
- (BOOL)deletePersonWithName:(NSString *)aPersonName {
    for (NSString *key in self.allPersonDic) {
        NSMutableArray *array = [self.allPersonDic objectForKey:key];
        for(int i = 0; i < array.count; i++) {
            if([[[array objectAtIndex:i] name] isEqualToString:aPersonName]) {
                [array removeObjectAtIndex:i];
                return YES;
            }
        }
    }
    return NO;
}


// 删除一个分组
- (BOOL)deleteGroup:(NSString *)aGroupName {
//    for (NSString *key in self.allPersonDic) {
//        if(![key isEqualToString:aGroupName]) {
//            return NO;
//        }
//    }
    [self.allPersonDic removeObjectForKey:aGroupName];
    return YES;
}


// 展示通讯录中所有联系人信息
- (void)showAll {
    for (NSString *key in self.allPersonDic) {
        NSMutableArray *array = [self.allPersonDic objectForKey:key];
        for (Person *str in array) {
            NSLog(@"%@", str);
        }
    }
}


@end

person.h

#import
#import "NSString+Characters.h"
@interface Person : NSObject

@property (nonatomic, retain) NSString *name; // 姓名
@property (nonatomic, retain) NSString *sex; // 性别
@property (nonatomic, assign) NSInteger age; // 年龄
@property (nonatomic, retain) NSString *phoneNumber; // 电话
@property (nonatomic, retain) NSString *groupName; // 分组


// 初始化
- (instancetype)initWithName:(NSString *)name
                         sex:(NSString *)sex
                 phoneNumber:(NSString *)phoneNumber
                         age:(NSInteger)age;


// 便利构造器
+ (instancetype)personWithName:(NSString *)name
                         sex:(NSString *)sex
                 phoneNumber:(NSString *)phoneNumber
                         age:(NSInteger)age;

@end

person.m

#import "Person.h"

@implementation Person

- (void)dealloc {
    
    NSLog(@"Person dealloc");
    
    [_name release];
    [_sex release];
    [_phoneNumber release];
    [super dealloc];
}

// 初始化
- (instancetype)initWithName:(NSString *)name
                         sex:(NSString *)sex
                 phoneNumber:(NSString *)phoneNumber
                         age:(NSInteger)age
{
    if (self = [super init]) {
        self.name = name;
        self.sex = sex;
        self.phoneNumber = phoneNumber;
        self.age = age;
        self.groupName = [self.name getUpperFirestLetterWithName];
    }
    return self;
}


// 便利构造器
+ (instancetype)personWithName:(NSString *)name
                           sex:(NSString *)sex
                   phoneNumber:(NSString *)phoneNumber
                           age:(NSInteger)age
{
    Person *per = [[[Person alloc] initWithName:name sex:sex phoneNumber:phoneNumber age:age] autorelease];
    return per;
}


// 输出
- (NSString *)description
{
    return [NSString stringWithFormat:@"姓名:%@ 性别:%@ 年龄:%lu 电话:%@", self.name, self.sex, self.age, self.phoneNumber];
}


@end

NSString+Characters.h

#import

@interface NSString (Characters)


// 获得姓名首字母并转为大写
- (NSString *)getUpperFirestLetterWithName;

@end

NSString+Characters.m

@implementation NSString (Characters)

// 获得姓名首字母并转为大写
- (NSString *)getUpperFirestLetterWithName {
    return [[self uppercaseString] substringToIndex:1];
}

@end

你可能感兴趣的:(OC)