main.m
int main(int argc, const char * argv[]) {
@autoreleasepool {
Contact *Yang = [[Contact alloc]initWithName:@"yangyang" tel:@158 age:16 sex:@"m"];
Contact *Na = [[Contact alloc]initWithName:@"nana" tel:@139 age:16 sex:@"w"];
Contact *Li = [[Contact alloc]initWithName:@"LiYang" tel:@145 age:23 sex:@"m"];
Contact *Han = [[Contact alloc]initWithName:@"HanLiang" tel:@128 age:12 sex:@"w"];
Contact *Zhao = [[Contact alloc]initWithName:@"ZhaoMing" tel:@178 age:24 sex:@"m"];
Contact *Sun = [[Contact alloc]initWithName:@"SunYang" tel:@189 age:19 sex:@"m"];
Contact *YangLi = [[Contact alloc]initWithName:@"YangLi" tel:@197 age:17 sex:@"w"];
Contact *HanHong = [[Contact alloc]initWithName:@"HanHong" tel:@146 age:34 sex:@"w"];
Contact *LiHua = [[Contact alloc]initWithName:@"LiHua" tel:@176 age:28 sex:@"m"];
//建立通訊錄對象
ContactBook *contactbook = [[ContactBook alloc]init];
//然後往通訊錄添加聯系人
[contactbook addPerson:Yang];
[contactbook addPerson:YangLi];
[contactbook addPerson:Na];
[contactbook addPerson:Li];
[contactbook addPerson:Zhao];
[contactbook addPerson:Sun];
[contactbook addPerson:HanHong];
[contactbook addPerson:Han];
[contactbook addPerson:LiHua];
//查找
NSLog(@"1 查找某個分組下的所有聯系人(Y)");
[contactbook FindGroupName:@"Y"];
//通過Tel查找
NSLog(@"2 通過tel查找聯系人(158)");
Contact *contact = [contactbook FindPersonByTel:@158];
NSLog(@"%@",contact);
//擷取所有的女性聯系人
NSLog(@"All female");
NSArray *female = [contactbook AllFemale];
//将已經排好序的女性聯系人枚舉周遊輸出
for (Contact *contact in female) {
NSLog(@"name: %@ age: %ld",contact.name,contact.age);
}
NSLog(@"删除某個聯系人(HanHong)");
[contactbook deleteByName:@"HanHong"];
NSLog(@"删除對應分組(L)");
[contactbook deleteGroup:@"L"];
}
return 0;
}
Contact.m
#import "Contact.h"
@implementation Contact
//初始化
-(id)initWithName:(NSString *)name
tel:(NSNumber *)tel
age:(NSInteger )age
sex:(NSString *)sex
{
if (self = [super init]) {
_name = name ;
_age = age ;
_tel = tel;
_sex = sex;
NSString *FirstName = [[name substringToIndex:1]uppercaseString];
_Group = FirstName;
}
return self;
}
//顯示所有人資訊
-(void)ShowAllPerson
{
NSLog(@"name:%@ age:%ld tel:%@ sex:%@",_name,_age,_tel,_sex);
}
//重寫description方法
-(NSString *)description
{
return [NSString stringWithFormat:@"name: %@ age: %ld tel: %@ sex: %@",_name,_age,_tel,_sex];
}
//通過姓名進行升序排序
-(NSComparisonResult)CompareByName:(Contact *)anotherContact
{
return [self.name compare:anotherContact.name];
}
//通過年齡進行降序排序
-(NSComparisonResult)CompareByAge:(Contact *)anotherContact
{
if (self.age > anotherContact.age) {
return NSOrderedAscending;
}else if(self.age < anotherContact.age)
{
return NSOrderedDescending;
}
return NSOrderedSame;
}
@end
"ContactBook.m"
#import "ContactBook.h"
#import "Contact.h"
@implementation ContactBook
//初始化
-(id)init
{
if (self = [super init]) {
//當通訊錄建立的時候就需一個容器
_ContDic = [[NSMutableDictionary alloc]init];
}
return self;
}
//添加聯系人
-(void)addPerson:(Contact *)contact
{
if (contact.name == nil || contact.tel == nil) {
NSLog(@"add person error");
}else
{
//添加
//先擷取對應的分組
NSMutableArray *group = [_ContDic valueForKey:contact.Group];
if (group == nil) {
//如果分組名是空的 那就新建立一個分組
group = [NSMutableArray array];
//然後 将建立的分組添加到通訊錄字典
[_ContDic setValue:group forKey:contact.Group];
}
//若分組名不是空的 則将其添加到對應的分組
[group addObject:contact];
}
}
//查找 給定的分組聯系人
-(void)FindGroupName:(NSString *)Group
{
NSMutableArray *group = [_ContDic valueForKey:Group];
//通過 姓名 升序
[group sortUsingSelector:@selector(CompareByName:)];
//枚舉輸出
for (Contact *contact in group) {
[contact ShowAllPerson];
}
}
//搜尋
-(Contact *)FindPersonByTel:(NSNumber *)tel
{
//周遊通訊錄字典
for (NSString *groupName in _ContDic) {
//通過分組名 擷取對應的分組
NSMutableArray *group = [_ContDic valueForKey:groupName];
//周遊分組 擷取聯系人
for (Contact *contact in group) {
if ([contact.tel isEqualToNumber:tel]) {
return contact;
}
}
}
NSLog(@"Find error! No people");
return nil;
}
//擷取所有女性聯系人
-(NSArray *)AllFemale
{
NSMutableArray *female = [[NSMutableArray alloc]init];
for (NSString *groupname in _ContDic) {
NSMutableArray *group = [_ContDic valueForKey:groupname];
for (Contact *contact in group) {
//通過性别進行篩選
if ([contact.sex isEqualToString:@"m"]) {
//将符合條件的聯系人 放入數組
[female addObject:contact];
}
}
}
//按照年齡進行排序
[female sortUsingSelector:@selector(CompareByAge:)];
return female;
}
-(void)deleteByName:(NSString *)name
{
//周遊通訊錄字典
for (NSString *groupName in _ContDic) {
//通過分組名 擷取對應的分組
NSMutableArray *group = [_ContDic valueForKey:groupName];
//周遊分組 擷取聯系人
for (int i=0; i<group.count; i++) {
if ([[group[i] name] isEqualToString:name]) {
[group removeObject:group[i]];
NSLog(@"delete %@ success",name);
NSLog(@"%@",_ContDic);
}
}
}
}
-(BOOL)deleteGroup:(NSString *)Group
{
[_ContDic removeObjectForKey:Group];
NSLog(@"剩下的聯系人:%@",_ContDic);
return YES;
}
@end