码迷,mamicode.com
首页 > 其他好文 > 详细

OC 通讯录 中等难度

时间:2015-10-16 20:43:58      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Contact.h

 

 

 

#import <Foundation/Foundation.h>

 

 

 

@interface Contact : NSObject

 

 

 

{

 

    NSString *_name;

 

    NSString *_gender;

 

    NSInteger _age;

 

    NSString *_address;

 

    NSString *_phoneNumber;

 

    NSString *_group;

 

}

 

 

 

- (void)setName:(NSString *)name;

 

- (NSString *)name;

 

 

 

- (void)setGender:(NSString *)gender;

 

- (NSString *)gender;

 

 

 

- (void)setAge:(NSInteger)age;

 

- (NSInteger)age;

 

 

 

- (void)setAddress:(NSString *)address;

 

- (NSString *)address;

 

 

 

- (void)setPhoneNumber:(NSString *)phoneNumber;

 

- (NSString *)phoneNumber;

 

 

 

- (void)setGroup:(NSString *)group;

 

- (NSString *)group;

 

 

 

- (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age address:(NSString *)address phoneNumber:(NSString *)phoneNumber;

 

 

 

- (void)introduce;

 

 

 

- (NSString *)description;

 

 

 

- (NSComparisonResult)compareByName:(Contact *)contact;

 

 

 

- (NSComparisonResult)compareByAge:(Contact *)contact;

 

 

 

@end

 

 

Contact.m

 

 

 

#import "Contact.h"

 

 

 

@implementation Contact

 

 

 

- (void)setName:(NSString *)name {

 

    _name = name;

 

}

 

- (NSString *)name {

 

    return _name;

 

}

 

 

 

- (void)setGender:(NSString *)gender {

 

    _gender = gender;

 

}

 

- (NSString *)gender {

 

    return _gender;

 

}

 

 

 

- (void)setAge:(NSInteger)age {

 

    _age = age;

 

}

 

- (NSInteger)age {

 

    return _age;

 

}

 

 

 

- (void)setAddress:(NSString *)address {

 

    _address = address;

 

}

 

- (NSString *)address {

 

    return _address;

 

}

 

 

 

- (void)setPhoneNumber:(NSString *)phoneNumber {

 

    _phoneNumber = phoneNumber;

 

}

 

- (NSString *)phoneNumber {

 

    return _phoneNumber;

 

}

 

 

 

- (void)setGroup:(NSString *)group {

 

    _group = group;

 

}

 

- (NSString *)group {

 

    return _group;

 

}

 

 

 

- (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age address:(NSString *)address phoneNumber:(NSString *)phoneNumber  {

 

    self = [super init];

 

    if (self) {

 

        [self setName:name];

 

        [self setGender:gender];

 

        [self setAge:age];

 

        [self setAddress:address];

 

        [self setPhoneNumber:phoneNumber];

 

        _group = [_name substringToIndex:1];

 

    }

 

    return self;

 

}

 

 

 

- (void)introduce {

 

    NSLog(@"%@ %@ %ld %@ %@ %@",_name, _gender, _age, _address, _phoneNumber, _group);

 

}

 

 

 

- (NSString *)description {

 

    return [[NSString alloc] initWithFormat:@"%@ %@ %ld %@ %@ %@",_name, _gender, _age, _address, _phoneNumber, _group];

 

}

 

 

 

- (NSComparisonResult)compareByName:(Contact *)contact {

 

    NSString *selfName = [self name];

 

    NSString *contactName = [contact name];

 

    NSComparisonResult result = [selfName compare:contactName];

 

    return result;

 

}

 

 

 

- (NSComparisonResult)compareByAge:(Contact *)contact {

 

    NSInteger selfAge = [self age];

 

    NSInteger contactAge = [contact age];

 

    

 

    if (selfAge > contactAge) {

 

        return NSOrderedAscending;

 

    }

 

    else if (selfAge < contactAge) {

 

        return NSOrderedDescending;

 

    }

 

    else

 

    {

 

        return NSOrderedSame;

 

    }

 

}

 

@end

 

 

 

ContactManger.h

 

 

 

#import <Foundation/Foundation.h>

 

#import "Contact.h"

 

 

 

@interface ContactManager : NSObject

 

 

 

{

 

    NSString *_name;

 

    NSMutableDictionary *_contactList;

 

}

 

 

 

- (void)setName:(NSString *)name;

 

- (NSString *)name;

 

 

 

- (instancetype)initWithName:(NSString *)name;

 

 

 

//添加联系人

 

- (void)addNewContact:(Contact *)contact;

 

 

 

//获取分组下所有的联系人,并且按照姓名升序排列

 

- (NSArray *)getAllContactsByGroupName:(NSString *)groupName;

 

 

 

//根据号码搜索对应的联系人

 

- (Contact *)findContactByPhoneNumber:(NSString *)phoneNumber;

 

 

 

//获取所有女性联系人 按照年龄降序排列

 

- (NSArray *)getAllFemalContactsByGender:(NSString *)gender;

 

 

 

//根据姓名删除某个联系人

 

- (void)removeContactByName:(NSString *)name;

 

 

 

//删除某个分组下的所有联系人

 

- (void)removeAllContactsByGroupName:(NSString *)groupName;

 

 

 

//展示所有联系人

 

- (void)showAllContacts;

 

 @end

 

 

 

ContactManger.m

 

 

 

#import "ContactManager.h"

 

 

 

@implementation ContactManager

 

 

 

- (void)setName:(NSString *)name {

 

    _name = name;

 

}

 

- (NSString *)name {

 

    return _name;

 

}

 

 

 

- (instancetype)initWithName:(NSString *)name {

 

    self = [super init];

 

    if (self) {

 

        [self setName:name];

 

        _contactList = [[NSMutableDictionary alloc] initWithCapacity:0];

 

    }

 

    return self;

 

}

 

 

 

//添加联系人

 

- (void)addNewContact:(Contact *)contact {

 

    if (nil == [contact name] || nil == [contact phoneNumber]) {

 

        NSLog(@"你脑子坏了吧?");

 

    } else {

 

        NSMutableArray *array = [_contactList objectForKey:[contact group]];

 

        if (nil == array) {

 

            //要初始化数组

 

            array = [[NSMutableArray alloc] initWithCapacity:0];

 

            [array addObject:contact];

 

            [_contactList setObject:array forKey:[contact group]];

 

        }

 

        else {

 

            [array addObject:contact];

 

        }

 

    }

 

    

 

    

 

}

 

 

 

//获取分组下所有的联系人,并且按照姓名升序排列

 

- (NSArray *)getAllContactsByGroupName:(NSString *)groupName {

 

    NSArray *contacts = [_contactList objectForKey:groupName];

 

    NSArray *newArray = [contacts sortedArrayUsingSelector:@selector(compareByName:)];

 

    return newArray;

 

}

 

 

 

//根据号码搜索对应的联系人

 

- (Contact *)findContactByPhoneNumber:(NSString *)phoneNumber {

 

    for (NSString *key in _contactList) {

 

        NSMutableArray *array = [_contactList objectForKey:key];

 

        for (Contact *contact in array) {

 

            if ([phoneNumber isEqualToString:[contact phoneNumber]]) {

 

                return contact;

 

            }

 

        }

 

    }

 

    return nil;

 

}

 

 

 

//获取所有女性联系人 按照年龄降序排列

 

- (NSArray *)getAllFemalContactsByGender:(NSString *)gender {

 

    NSMutableArray *femalArray = [[NSMutableArray alloc] initWithCapacity:0];

 

    for (NSString *key in _contactList) {

 

        NSMutableArray *array = [_contactList objectForKey:key];

 

        for (Contact *contact in array) {

 

            if ([gender isEqualToString:[contact gender]]) {

 

                [femalArray addObject:contact];

 

            }

 

        }

 

    }

 

    [femalArray sortUsingSelector:@selector(compareByAge:)];

 

    return femalArray;

 

}

 

 

 

//根据姓名删除某个联系人

 

- (void)removeContactByName:(NSString *)name {

 

    Contact *c;

 

    NSString *newKey;

 

    for (NSString *key in _contactList) {

 

        NSMutableArray *array = [_contactList objectForKey:key];

 

        for (Contact *contact in array) {

 

            if ([name isEqualToString:[contact name]]) {

 

                [array removeObject:contact];

 

                return;

 

//                c = contact;

 

//                newKey = key;

 

            }

 

        }

 

    }

 

    [[_contactList objectForKey:newKey] removeObject:c];

 

}

 

 

 

//删除某个分组下的所有联系人

 

- (void)removeAllContactsByGroupName:(NSString *)groupName {

 

    [_contactList removeObjectForKey:groupName];

 

}

 

 

 

//展示所有联系人

 

- (void)showAllContacts {

 

    NSArray *allKeys = [_contactList allKeys];

 

    NSArray *newArray = [allKeys sortedArrayUsingSelector:@selector(compare:)];

 

    

 

    for (NSString *key in newArray) {

 

        NSMutableArray *array = [_contactList objectForKey:key];

 

        NSLog(@"-----%@-----",key);

 

        

 

        for (Contact *contact in array) {

 

            [contact introduce];

 

        }

 

    }

 

}

 

@end

 

 

main.m 

 

 

 

#import <Foundation/Foundation.h>

 

#import "ContactManager.h"

 

 

 

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

 

    

 

    ContactManager *cm = [[ContactManager alloc] initWithName:@"SHS150913通讯录(中等难度)"];

 

    

 

    Contact *c1 = [[Contact alloc] initWithName:@"wangzhiwen" gender:@"男" age:18 address:@"丽德" phoneNumber:@"15156056193"];

 

    Contact *c2 = [[Contact alloc] initWithName:@"xumin" gender:@"女" age:19 address:@"丽德酒店" phoneNumber:@"15951758095"];

 

    Contact *c3 = [[Contact alloc] initWithName:@"zhanglu" gender:@"女" age:20 address:@"丽德酒店" phoneNumber:@"15951758639"];

 

    Contact *c4 = [[Contact alloc] initWithName:@"xuzizhong" gender:@"男" age:21 address:@"丽德" phoneNumber:@"18862162030"];

 

    Contact *c5 = [[Contact alloc] initWithName:@"wangtao" gender:@"男" age:22 address:@"丽德" phoneNumber:@"18329032011"];

 

    Contact *c6 = [[Contact alloc] initWithName:@"qiuse" gender:@"男" age:23 address:@"丽德" phoneNumber:@"18321978135"];

 

    Contact *c7 = [[Contact alloc] initWithName:@"juju" gender:@"女" age:24 address:@"丽德" phoneNumber:@"15156697015"];

 

 

 

    [cm addNewContact:c1];

 

    [cm addNewContact:c2];

 

    [cm addNewContact:c3];

 

    [cm addNewContact:c4];

 

    [cm addNewContact:c5];

 

    [cm addNewContact:c6];

 

    [cm addNewContact:c7];

 

 

 

    [cm showAllContacts];

 

    NSLog(@"-----华丽的分割线-----");

 

    

 

    NSArray *array = [cm getAllContactsByGroupName:@"w"];

 

    NSLog(@"%@",array);

 

    NSLog(@"-----华丽的分割线-----");

 

 

 

    Contact *resultContact = [cm findContactByPhoneNumber:@"15156697015"];

 

    NSLog(@"%@",resultContact);

 

    NSLog(@"-----华丽的分割线-----");

 

 

 

    NSArray *femalArray = [cm getAllFemalContactsByGender:@"女"];

 

    NSLog(@"%@",femalArray);

 

    NSLog(@"-----华丽的分割线-----");

 

 

 

    [cm removeContactByName:@"juju"];

 

    [cm showAllContacts];

 

    NSLog(@"-----华丽的分割线-----");

 

 

 

    [cm removeAllContactsByGroupName:@"w"];

 

    [cm showAllContacts];

 

    return 0;

 

}

 

OC 通讯录 中等难度

标签:

原文地址:http://www.cnblogs.com/xlsn0w/p/4886156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!