int main(int argc, const char * argv[]) {
@autoreleasepool {
Contact *con1 = [Contact contactWithName:@”Zhangsan” gender:@”man” number:@”123456” address:@”zhengzhou” groupName:@”Z” age:18];
Contact *con2 = [Contact contactWithName:@"Lisi" gender:@"man" number:@"456789" address:@"zhengzhou" groupName:@"L" age:20];
Contact *con3 = [Contact contactWithName:@"Wanger" gender:@"man" number:@"147852" address:@"kaifeng" groupName:@"W" age:21];
Contact *con4 = [Contact contactWithName:@"Chengxu" gender:@"women" number:@"369852" address:@"luoyang" groupName:@"C" age:20];
Contact *con5 = [Contact contactWithName:@"Xiaoxiao" gender:@"women" number:@"951753" address:@"zhengzhou" groupName:@"X" age:18];
Contact *con6 = [Contact contactWithName:@"Bilaoye" gender:@"man" number:@"753869" address:@"zhengzhou" groupName:@"B" age:20];
AddressBook *book = [[AddressBook alloc] init];
//3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。
[book addContact:con1];
[book addContact:con2];
[book addContact:con3];
[book addContact:con4];
[book addContact:con5];
[book addContact:con6];
[book displayAllContactInfo];
//4、获取某个分组名称下所有联系人,并且按照姓名升序排列。
NSArray *array = [book getPersonForGroupName:@”X”];
NSLog(@”%@”, array);
//5、从通讯录中根据电话号码搜索联系人。
Contact *nArray = [book getContactForNumber:@”123456”];
NSLog(@”%@”, nArray);
//6、获取所有女性的联系人,并且按照年龄的降序排列。
NSArray *girl = [book getPersonForGender:@”women”];
NSLog(@”%@”, girl);
//7、根据姓名删除某个联系人。
[book removeContactForName:@”Zhangsan”];
[book displayAllContactInfo];
// 8、删除某个分组的全部联系人。
[book removeAllContactsForGroupName:@”C”];
[book displayAllContactInfo];
}
return 0;
}
@implementation AddressBook
//重写父类继承过来的初始化方法,在初始化的同时,为_allContactDic完成初始化操作.
- (id)init {
self = [super init];
if (self) {
_allContactDic = [NSMutableDictionary dictionaryWithCapacity:1];
}
return self;
}
//添加联系人
- (void)addContact:(Contact *)con {
//获取姓名和电话
NSString *name = [con name];
NSString *number = [con number];
//判断姓名和电话都为空时
if (name == nil || number == nil) {
NSLog(@”添加失败”);
return;
}
//姓名电话都不为空时
//首先获取分组姓名
NSString *gruop = [con groupName];
//获取分组姓名之后,从字典中取出数组
NSMutableArray *conArray = [_allContactDic objectForKey:gruop];
//此时分两种情况,能不能取到conArray
if (conArray == nil) {
//创建数组
conArray = [NSMutableArray arrayWithCapacity:1];
//然后将联系人对象添加到数组中
[conArray addObject:con];
//最后在字典中添加该键值对
[_allContactDic setObject:conArray forKey:gruop];
}else {
//如果键值对已经存在,直接添加
[conArray addObject:con];
}
}
//获得指定分组下得所有联系人,按照联系人姓名升序排序.
- (NSArray )getPersonForGroupName:(NSString )groupName{
//获取指定分组的组名
NSMutableArray *mGroup = _allContactDic[groupName];
//然后按照姓名升序排序
[mGroup sortUsingSelector:@selector(compareByName:)];
//返回该数组
return mGroup;
}
//获取所有女性的联系人,并且按照年龄的降序排列。
- (NSArray )getPersonForGender:(NSString )gender{
NSMutableArray *genderArr = [[NSMutableArray alloc] initWithCapacity:1];
for (NSString *key in _allContactDic) {
NSMutableArray *groupArray = _allContactDic[key];
for (Contact *con in groupArray) {
if ([[con gender] isEqualToString:gender]) {
[genderArr addObject:con];
}
}
}
[genderArr sortUsingSelector:@selector(compareByAge:)];
return genderArr;
}
//从通讯录中根据电话号码搜索联系人。
- (Contact )getContactForNumber:(NSString )number{
for (NSString *key in _allContactDic) {
NSMutableArray *groupNum = _allContactDic[key];
for (Contact *con in groupNum) {
if ([[con number] isEqualToString:number]){
return con;
}
}
}
return nil;
}
//根据姓名删除某个联系人。
- (void)removeContactForName:(NSString *)name{
for (NSString *key in _allContactDic) {
NSMutableArray *nameGroup = _allContactDic[key];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:nameGroup];
for (Contact *con in tempArray) {
if ([[con name] isEqualToString:name]) {
[nameGroup removeObject:con];
}
}
}
}
//删除某个分组的全部联系人.
- (void)removeAllContactsForGroupName:(NSString *)groupName {
[_allContactDic removeObjectForKey:groupName];
}
//展示所有联系人信息
- (void)displayAllContactInfo {
for (NSString *key in _allContactDic) {
NSArray *contcatArray = _allContactDic[key];
for (Contact *con in contcatArray) {
[con printContactInfo];
}
}
}
@end
@implementation Contact
//setter 和 getter 方法
(NSString *)name {
return _name;
}
(NSString *)gender {
return _gender;
}
(NSString *)number {
return _number;
}
(NSString *)address {
return _address;
}
(NSString *)groupName {
return _groupName;
}
(NSInteger)age {
return _age;
}
//自定义初始化
- (id)initWithName:(NSString *)name
gender:(NSString *)gender
number:(NSString *)number
address:(NSString *)address
groupName:(NSString *)groupName
age:(NSInteger)age {
self = [super init];
if (self) {
_name = name;
_gender = gender;
_number = number;
_address = address;
_groupName = groupName;
_age = age;
}
return self;
}
//便利结构器
+ (id)contactWithName:(NSString *)name
gender:(NSString *)gender
number:(NSString *)number
address:(NSString *)address
groupName:(NSString *)groupName
age:(NSInteger)age {
Contact *contact = [[Contact alloc] initWithName:name gender:gender number:number address:address groupName:groupName age:age];
return contact;
}
//两个联系人对象比较的方法
//按姓名
- (NSComparisonResult)compareByName:(Contact *)con
{
return [[self name] compare:[con name]];
}
//按年龄
- (NSComparisonResult)compareByAge:(Contact *)con
{
//1.获取年龄
NSInteger age1 = [self age];
NSInteger age2 = [con age];
// 2.比较年龄
// if (age1 > age2) {
// return NSOrderedDescending;
// } else if (age1 == age2) {
// return NSOrderedSame;
// } else {
// return NSOrderedAscending;
// }
return -[@(age1) compare:@(age2)];
}
//按联系方式
- (NSComparisonResult)compareByNumber:(Contact *)con
{
return -[[self number] compare:[con number]];
}
//输出联系人信息
- (void)printContactInfo {
NSLog(@”name = %@, gender = %@, number = %@, address = %@, groupName = %@, age = %ld”, _name, _gender, _number, _address, _groupName, _age);
}
@end