//实现中级通讯录.
/作业1: 实现通讯录.需求:
//1.定义联系人类Contact. 实例变量:姓名(拼音 首字母大写), 性别, 电话号码, 住址, 分组名称, 年龄. 方法: 自定义初始化方法(姓名 ,电话号码), 显示联系人信息.
//2./2.在main.m中定义字典, 分组管理所有联系人. 分组名为26个大写的英文字母.
//3.可以添加联系人对象, 如果姓名或电话号码为空, 添加失败.添加联系人到匹配的分组.
//4.获取某个分组名称下所有联系人, 并且按照年龄的升序排列.
//5.从通讯录中根据电话号码搜索联系人.
//6. 获取所有女性的联系人, 并且按照年龄的降序排列.
//7. 根据姓名删除某个联系人
//8.删除某个分组的全部联系人
// Contact.h #import <Foundation/Foundation.h> //1.定义联系人类Contact. 实例变量:姓名(拼音 首字母大写), 性别, 电话号码, 住址, 分组名称, 年龄. 方法: 自定义初始化方法(姓名 ,电话号码), 显示联系人信息. @interface Contact : NSObject { NSString *_name; NSString *_gender; NSString *_number; NSString *_place; NSString *_group; NSInteger _age; } //setter姓名 -(void)setName:(NSString *)name; //getter - (NSString *)name; //setter性别 - (void)setGender:(NSString *)gender; //getter - (NSString *)gender; //setter电话号码 - (void)setNumber:(NSString *)number; //getter - (NSString *)number; //setter住址 - (void)setPlace:(NSString *)place; //getter - (NSString *)place; //setter分组 - (void)setGroup:(NSString *)group; //getter - (NSString *)group; //setter年龄 - (void)setAge:(NSInteger)age; //getter - (NSInteger)age; //初始化 - (id)initWithName:(NSString *)name number:(NSString *)number; //便利构造 + (id)ContactWithName:(NSString *)name number:(NSString *)number; - (void)displayContactInfo; //告诉外界,该方法子类已经重写了. - (NSString *)description; //两个联系人对象比较的方法 //按姓名 - (NSComparisonResult)compareByName:(Contact *)addContact; //按年龄 - (NSComparisonResult)compareByAge:(Contact *)addContact; //按联系方式 - (NSComparisonResult)compareByPhoneNumber:(Contact *)addContact; @end
// // Contact.m #import "Contact.h" @implementation Contact //setter姓名 -(void)setName:(NSString *)name{ _name = name; } //getter - (NSString *)name{ return _name; } //setter性别 - (void)setGender:(NSString *)gender{ _gender = gender; } //getter - (NSString *)gender{ return _gender; } //setter电话号码 - (void)setNumber:(NSString *)number{ _number = number; } //getter - (NSString *)number{ return _number; } //setter住址 - (void)setPlace:(NSString *)place{ _place = place; } //getter - (NSString *)place{ return _place; } //setter分组 - (void)setGroup:(NSString *)group{ _group = group; } //getter - (NSString *)group{ return _group; } //setter年龄 - (void)setAge:(NSInteger)age{ _age = age; } //getter - (NSInteger)age{ return _age; } - (id)initWithName:(NSString *)name number:(NSString *)number { self = [super init]; if (self) { _name = name; _number = number; } return self; } //便利构造 + (id)ContactWithName:(NSString *)name number:(NSString *)number { Contact *c = [[Contact alloc] initWithName:name number:number]; return c; } - (void)displayContactInfo { NSLog(@"name:%@,gender:%@,number:%@,age:%ld", _name, _gender, _number, _age); } //重写从父类继承过来的description方法 //当通过%@打印一个当前对象时,就会调用该方法. - (NSString *)description { // return [NSString stringWithFormat:@"<%@: %p>", [self class], self]; return [NSString stringWithFormat:@"name:%@,gender:%@,number:%@,age:%ld", _name, _gender, _number, _age]; } //两个联系人对象比较的方法 //按姓名 - (NSComparisonResult)compareByName:(Contact *)addContact { return [[self name] compare:[addContact name]]; } //按年龄 - (NSComparisonResult)compareByAge:(Contact *)addContact { //1.获取年龄 NSInteger age1 = [self age]; NSInteger age2 = [addContact age]; //2.比较年龄 // if (age1 > age2) { // return NSOrderedDescending; // } else if (age1 == age2) { // return NSOrderedSame; // } else { // return NSOrderedAscending; // } return -[@(age1) compare:@(age2)]; } //按联系方式 - (NSComparisonResult)compareByPhoneNumber:(Contact *)addContact { return -[[self number] compare:[addContact number]]; } @end
// // Contact.m #import "Contact.h" @implementation Contact //setter姓名 -(void)setName:(NSString *)name{ _name = name; } //getter - (NSString *)name{ return _name; } //setter性别 - (void)setGender:(NSString *)gender{ _gender = gender; } //getter - (NSString *)gender{ return _gender; } //setter电话号码 - (void)setNumber:(NSString *)number{ _number = number; } //getter - (NSString *)number{ return _number; } //setter住址 - (void)setPlace:(NSString *)place{ _place = place; } //getter - (NSString *)place{ return _place; } //setter分组 - (void)setGroup:(NSString *)group{ _group = group; } //getter - (NSString *)group{ return _group; } //setter年龄 - (void)setAge:(NSInteger)age{ _age = age; } //getter - (NSInteger)age{ return _age; } - (id)initWithName:(NSString *)name number:(NSString *)number { self = [super init]; if (self) { _name = name; _number = number; } return self; } //便利构造 + (id)ContactWithName:(NSString *)name number:(NSString *)number { Contact *c = [[Contact alloc] initWithName:name number:number]; return c; } - (void)displayContactInfo { NSLog(@"name:%@,gender:%@,number:%@,age:%ld", _name, _gender, _number, _age); } //重写从父类继承过来的description方法 //当通过%@打印一个当前对象时,就会调用该方法. - (NSString *)description { // return [NSString stringWithFormat:@"<%@: %p>", [self class], self]; return [NSString stringWithFormat:@"name:%@,gender:%@,number:%@,age:%ld", _name, _gender, _number, _age]; } //两个联系人对象比较的方法 //按姓名 - (NSComparisonResult)compareByName:(Contact *)addContact { return [[self name] compare:[addContact name]]; } //按年龄 - (NSComparisonResult)compareByAge:(Contact *)addContact { //1.获取年龄 NSInteger age1 = [self age]; NSInteger age2 = [addContact age]; //2.比较年龄 // if (age1 > age2) { // return NSOrderedDescending; // } else if (age1 == age2) { // return NSOrderedSame; // } else { // return NSOrderedAscending; // } return -[@(age1) compare:@(age2)]; } //按联系方式 - (NSComparisonResult)compareByPhoneNumber:(Contact *)addContact { return -[[self number] compare:[addContact number]]; } @end
// // ContactBook.m #import "ContactBook.h" #import "Contact.h" @implementation ContactBook //初始化 -(id)init{ if (self) { //_alloContactDic = [NSMutableDictionary dictionaryWithCapacity:1]; Contact *contact1 = [[Contact alloc] initWithName:@"Wangzhao" number:@"18203662931"]; [contact1 setPlace:@"Xinyang"]; [contact1 setGroup:@"family"]; [contact1 setGender:@"man"]; [contact1 setAge:24]; Contact *contact2 = [[Contact alloc] initWithName:@"Gaoxingxing" number:@"15136423050"]; [contact2 setPlace:@"handan"]; [contact2 setGroup:@"family"]; [contact2 setGender:@"women"]; [contact2 setAge:23]; Contact *contact3 = [[Contact alloc] initWithName:@"Wyy" number:@"17036733150"]; [contact3 setPlace:@"anqing"]; [contact3 setGroup:@"student"]; [contact3 setGender:@"man"]; [contact3 setAge:22]; Contact *contact4 = [[Contact alloc] initWithName:@"Wangxiaolin" number:@"18203658955"]; [contact4 setPlace:@"xinyang"]; [contact4 setGroup:@"student"]; [contact4 setGender:@"man"]; [contact4 setAge:26]; Contact *contact5 = [[Contact alloc] initWithName:@"Xuedongli" number:@"18203659413"]; [contact5 setPlace:@"henan"]; [contact5 setGroup:@"teacher"]; [contact5 setGender:@"man"]; [contact5 setAge:28]; //创建分组, 分别管理改组内的联系人 NSMutableArray *fGroupArr = [NSMutableArray arrayWithObjects:contact1,contact2, nil]; NSMutableArray *sGroupArr = [NSMutableArray arrayWithObjects:contact3,contact4, nil]; NSMutableArray *tGroupArr = [NSMutableArray arrayWithObject:contact5]; //创建可变字典, 管理班级的所有分组, 每个分组管理组内的联系人 _alloContactDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fGroupArr, @"F", sGroupArr, @"S", tGroupArr, @"T", nil]; } return self; } //获取所有分组 - (NSMutableDictionary *)allContactDic{ return _alloContactDic; } //添加联系人 - (BOOL)addContact:(Contact *)addContact{ //1.判断姓名以及联系方式是否为空 NSString *name = [addContact name]; NSString *number = [addContact number]; if (!name || ![name length] || !number || ![number length]) { return NO; } else { //添加到对应的分组中 //(1)获取到姓名首字母 NSString *firstCharac = [name substringToIndex:1]; //(2)根据key 从字典中取出对应的分组 NSMutableArray *groupArr = _alloContactDic[firstCharac]; //(3)判断是否成功从字典中取到key对应的数组 if (!groupArr) { //如果没有数组,就创建数组. groupArr = [NSMutableArray arrayWithCapacity:1]; //将key以及value添加到字典中 [_alloContactDic setObject:groupArr forKey:firstCharac]; } [groupArr addObject:addContact]; return YES; } }//获得指定分组下所有联系人, 按照联系人姓名升序排列 - (NSMutableArray *)allContactWithNameAscendingByGroup:(NSString *)group{ //key对应的数组就存储着该组下所有的联系人. NSMutableArray *fGroup = _alloContactDic[group]; //按照联系人姓名升序排列 [fGroup sortedArrayUsingSelector:@selector(compareByName:)]; return fGroup; } //获取所有女性联系人, 并且按照年龄的降序排列 - (NSMutableArray *)allGrilcontactsAgeDescending{ //获取所有联系人, 并且按照年龄的降序排列 NSMutableArray *girlArr = [[NSMutableArray alloc] initWithCapacity:1]; for (NSString *key in _alloContactDic) { NSMutableArray *groupArr = _alloContactDic[key]; for (Contact *contact in groupArr) { if ([[contact gender] isEqualToString:@"girl"]) { //查找到女性联系人之后,将联系人添加到女性数组中. [girlArr addObject:contact]; } } } [girlArr sortUsingSelector:@selector(compareByAge:)]; return girlArr; } //按照年龄降序排 //从通讯录中根据电话号码搜索联系人 - (Contact *)getContactByNumber:(NSString *)number{ //1.先获取联系人所在的数组 for (NSString *key in _alloContactDic) { //根据key获取对应的数组. NSMutableArray *groupArr = _alloContactDic[key]; //遍历到数组中的联系人 for (Contact *contact in groupArr) { if ([[contact number] isEqualToString:number]) { return contact; } } } return nil; } //根据姓名删除某个联系人 - (void)removeContactByName:(NSString *)name{ //(1)获取所在分组名称 NSString *groupName = [name substringToIndex:1]; //(2)获取key -- 数组 NSMutableArray *group = _alloContactDic[groupName]; for (int i = 0; i < [group count]; i++) { Contact *contact = group[i]; if ([[contact name] isEqualToString:@"ChangChaoQun"]) { //如果和指定名字相同,则从数组中移除对应的元素. [group removeObjectAtIndex:i]; break; } } } //删除某个分组的全部联系人 - (void)removeAllContactsByGroup:(NSString *)group{ [_alloContactDic removeObjectForKey:group]; } //输出所有联系人 - (void)displayAllAddContactInfo{ NSLog(@"allContactInfo = %@", _alloContactDic); } @end
// // main.m #import <Foundation/Foundation.h> #import "Contact.h" #import "ContactBook.h" int main(int argc, const char * argv[]) { @autoreleasepool { //2.在main.m中定义字典, 分组管理所有联系人. 分组名为26个大写的英文字母. //创建通讯录对象 ContactBook *lanou12 = [[ContactBook alloc] init]; //添加联系人 Contact *frank = [[Contact alloc] initWithName:@"Wangluodan" number:@"18839732932"]; BOOL isSuccess = [lanou12 addContact:frank]; NSLog(@"%d", isSuccess); [lanou12 displayAllAddContactInfo]; //获得指定分组下得所有联系人,按照联系人姓名升序排序. NSMutableArray *fGroupArr = [lanou12 allContactWithNameAscendingByGroup:@"W"]; NSLog(@"%@", fGroupArr); //获取所有女性的联系人,并且按照年龄的降序排列。 NSMutableArray *girlArr = [lanou12 allGrilcontactsAgeDescending]; NSLog(@"%@", girlArr); //从通讯录中根据电话号码搜索联系人。 Contact *contact = [lanou12 getContactByNumber:@"13838389438"]; NSLog(@"contact = %@", contact); //删除姓名为Wangzhao的联系人 [lanou12 removeContactByName:@"Wangzhao"]; [lanou12 displayAllAddContactInfo]; //删除f分组下得所有联系人 [lanou12 removeAllContactsByGroup:@"F"]; [lanou12 displayAllAddContactInfo]; } return 0; }
原文:http://www.cnblogs.com/wangzhao2015/p/4621347.html