1.创建AddressBook类.
1)使用字典作为容器,字典的Key值为分组名(姓名首字母,大写),value值为数组,数组中存放联系人(Person实例).
2)添加联系人.要求
a.判断姓名和电话不能为空否则添加失败.
b.判断是否已经存在分组,存在直接存储.不存在创建分组后再存储.(注:联系人不能有重名).
c.添加成功返回YES,姓名为空或者有重名的添加失败返回NO.
d.数据使用Person存储.
e.方法:- (BOOL)addPerson:(Person *)aPerson;
2.获取某个分组名称下所有联系人,并且按照姓名升序排列。
3.从通讯录中根据电话号码搜索联系⼈人。
4.获取所有⼥女性的联系⼈,并且按照年龄的降序排列。
5.根据姓名删除某个联系⼈。
6.删除某个分组的全部联系⼈。
Person的声明部分
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic,retain)NSString *name ; @property (nonatomic,retain)NSString *phoneNumber ; @property (nonatomic,copy)NSString *address ; @property (nonatomic,assign)int age ; @property (nonatomic,retain)NSString *gender ;
//便利构造器的声明
+ (instancetype)personWithName : (NSString *)name phoneNumber : (NSString *)phoneNumber address : (NSString *)address age : (int)age gender : (NSString *)gender ;
// 按年龄降序排序
- (NSComparisonResult)compareByAge : (Person *)person ; // 按姓名升序排列
- (NSComparisonResult)compareByName : (Person *)person ; @end
Person的实现部分
#import "Person.h"
@implementation Person
// 初始化方法
- (instancetype)initWithName : (NSString *)name phoneNumber : (NSString *)phoneNumber address : (NSString *)address age : (int)age gender : (NSString *)gender { if (self = [super init]) { self.name = name ; self.phoneNumber = phoneNumber ; self.address = address ; self.age = age ; self.gender = gender ; } return self ; }
// 便利构造器
+ (instancetype)personWithName : (NSString *)name phoneNumber : (NSString *)phoneNumber address : (NSString *)address age : (int)age gender : (NSString *)gender { Person *person = [[Person alloc] initWithName:name phoneNumber:phoneNumber address:address age:age gender:gender] ; return [person autorelease] ; }
// 联系人的自描述方法
- (NSString *)description { NSString *string = [[NSString alloc] initWithFormat:@"name :%@,phoneNumber :%@,address :%@,age :%d,gender :%@",_name,_phoneNumber,_address,_age,_gender] ; return [string autorelease] ; }
// 按年龄降序排序
- (NSComparisonResult)compareByAge : (Person *)person { if (_age < [person age]) { return NSOrderedDescending ; } else if (_age > [person age]) { return NSOrderedAscending ; } else return NSOrderedSame ; }
// 按姓名升序
- (NSComparisonResult)compareByName : (Person *)person { if ([_name compare:person.name] == -1) { return NSOrderedAscending ; } else if ([_name compare:person.name] == 1) { return NSOrderedDescending ; } else return NSOrderedSame ; }
- (void)dealloc { [_name release]; [_phoneNumber release]; [_address release] ; [_gender release] ; [super dealloc] ; } @end
AdressBook的声明部分
#import <Foundation/Foundation.h>
#import "Person.h"
@interface AddressBook : NSObject
@property (nonatomic,retain)NSMutableDictionary *mDictionary ; // 字典中存放的是联系人的键值对,值是动态变化的,因此使用可变字典
// 便利构造器
+ (instancetype)addressBookWithDictionary : (NSMutableDictionary *)mDictionary ;
// 添加联系人
- (BOOL)addPerson : (Person *)person ;
// 获取某个分组下名称下所有联系人,并按升序排列
- (void)personByGroupName : (NSString *)groupName ;
// 根据电话号码搜索
- (void)searchByPhoneNumber : (NSString *)phoneNumber ;
// 获取女性联系人,并按年龄排序
- (void)getFemalePersonSortByAgeDown ;
// 根据姓名删除某联系人
- (void)removeByName : (NSString *)name ;
// 删除某个分组的全部联系人
- (void)removeAll : (NSString *)groupName ; @end
AdressBook的实现部分
#import "AddressBook.h"
@implementation AddressBook
//初始化方法
- (instancetype)initWithDictionary : (NSMutableDictionary *)mDictionary { if (self = [super init]) { self.mDictionary = mDictionary ; } return self ; }
//便利构造器
+ (instancetype)addressBookWithDictionary : (NSMutableDictionary *)mDictionary { AddressBook *addressBook = [[AddressBook alloc] initWithDictionary:mDictionary] ; return [addressBook autorelease] ; }
// 添加联系人
- (BOOL)addPerson : (Person *)person { BOOL res = YES ;
// 先判断姓名和电话是否为空
if ([person name] != nil && [person phoneNumber] != nil) { // 如果不为空,则先判断字典中是否存在该分组 char ch = [[[person name] capitalizedString] characterAtIndex:0] ; // 取第一个字母 NSString *string = [NSString stringWithFormat:@"%c",ch] ; // 将字符转换为字符串对象 if ([_mDictionary objectForKey:string] != nil) // 判断该键值对应的value是否为空,如果不为空说明已经存在,则判断是否重名,如果不重名可以直接加进去 { // 重名判断 for (id obj in [_mDictionary objectForKey:string]) { if ([person.name isEqualToString:[obj name]]) { res = NO ; break ; } }
if (res == NO) // 重名 { NSLog(@"该联系人已存在") ; }
else // 没有重名,就添加进去 { [[_mDictionary objectForKey:string] addObject:person] ; //res = YES ; } } else // 创建一个新的数组用来存放该键值的value { NSMutableArray *mArray = [[NSMutableArray alloc] init] ; [mArray addObject:person] ; // 对象加到数组里 [_mDictionary setObject:mArray forKey:string] ; // 给字典添加键值对 [mArray autorelease] ; //res = YES ; } } else { NSLog(@"添加失败,姓名和电话不能为空!") ; res = NO ; } return res ; }
// 获取某个分组下的联系人
- (void)personByGroupName : (NSString *)groupName { if ([groupName length] != 1) { NSLog(@"分组名错误") ; } else { [[_mDictionary valueForKey:groupName] sortUsingSelector:@selector(compareByName:)] ; // 获取时是排好序的联系人(升序) for (id obj in [_mDictionary valueForKey:groupName]) { NSLog(@"%@",obj) ; } } }
// 根据电话号码搜索联系人
- (void)searchByPhoneNumber : (NSString *)phoneNumber { for (id obj in [_mDictionary allValues]) // 获取所有value值,遍历查找 { for (id obj1 in obj) { if ([[obj1 phoneNumber] isEqualToString:phoneNumber]) { NSLog(@"%@",obj1) ; } } } }
// 获取女性联系人并按照年龄降序输出
- (void)getFemalePersonSortByAgeDown { NSMutableArray *mArray = [[NSMutableArray alloc] init] ; for (id obj in [_mDictionary allValues]) { for (id obj1 in obj) { if ([[obj1 gender] isEqualToString:@"女"] || [[obj1 gender] isEqualToString :@"woman"] || [[obj1 gender] isEqualToString :@"female"]) { [mArray addObject:obj1] ; } } }
// 排序输出
[mArray sortUsingSelector:@selector(compareByAge:)] ; for (id obj2 in mArray) { NSLog(@"%@",obj2) ; } [mArray release] ; }
// 根据姓名删除某联系人
- (void)removeByName : (NSString *)name { NSString *string = [[name substringToIndex:1] capitalizedString] ; // 取子串在转为大写 for (int i = 0; i < [[_mDictionary valueForKey:string] count]; i++) { if ([[[[_mDictionary valueForKey:string] objectAtIndex:i] name] isEqualToString:name]) // { [[_mDictionary valueForKey:string] removeObjectAtIndex:i] ;// 删除坐标为i,姓名为name的对象 } } }
// 删除某个分组的全部联系人
- (void)removeAll : (NSString *)groupName { // 判断分组名是否输入正确 if ([groupName length] != 1) { NSLog(@"没有此分组") ; } else { [[_mDictionary valueForKey:groupName] removeAllObjects] ; } }
- (void)dealloc { [_mDictionary release] ; [super dealloc] ; } @end
主函数测试部分
#import <Foundation/Foundation.h>
#import "Person.h"
#import "AddressBook.h"
int main(int argc, const char * argv[]) { @autoreleasepool { // 创建多个Person对象 Person *person1 = [Person personWithName:@"song江" phoneNumber:@"11111111" address:@"郓城" age:32 gender:@"男"]; Person *person2 = [Person personWithName:@"lu俊义" phoneNumber:@"22222222" address:@"河北大名府" age:35 gender:@"男"]; Person *person3 = [Person personWithName:@"lin冲" phoneNumber:@"66666666" address:@"开封" age:28 gender:@"男"]; Person *person4 = [Person personWithName:@"hu三娘" phoneNumber:@"56565656" address:@"扈家庄" age:20 gender:@"女"]; Person *person5 = [Person personWithName:@"gu大嫂" phoneNumber:@"10810808" address:@"不知道" age:30 gender:@"woman"]; Person *person6 = [Person personWithName:@"likui" phoneNumber:@"77777777" address:@"sss" age:25 gender:@"男"]; Person *person7 = [Person personWithName:@"hua荣" phoneNumber:@"99999999" address:@"清风寨" age:26 gender:@"男"]; Person *person8 = [Person personWithName:@"sunerniang" phoneNumber:@"33333333" address:@"十字坡" age:27 gender:@"female"]; // 创建通讯录对象 AddressBook *addressBook = [AddressBook addressBookWithDictionary:[[NSMutableDictionary alloc] init]]; // 添加联系人 [addressBook addPerson:person1]; [addressBook addPerson:person2]; [addressBook addPerson:person3]; [addressBook addPerson:person4]; [addressBook addPerson:person5]; [addressBook addPerson:person6]; [addressBook addPerson:person7]; [addressBook addPerson:person8]; //创建一个测试用的对象 Person *personTest = [Person personWithName:@"likui" phoneNumber:@"" address:@"hhh" age:20 gender:@"男"]; [addressBook addPerson:personTest]; // 删除某分组下的联系人 //[addressBook removeAll:@"L"]; // 获取某分组下的联系人 [addressBook personByGroupName:@"L"] ; // 根据电话号码查询 [addressBook searchByPhoneNumber:@"99999999"]; // 获取女性联系人并按年龄排序 [addressBook getFemalePersonSortByAgeDown]; } return 0; }