main函数中通过使用Person类和AddressBook类,创建对象、调用方法,实现以下8个功能:
1).使用便利构造器创建四个Person类的实例(联系人).
姓名:Lucy 性别:女 电话:123 年龄:21
姓名:Joe 性别:男 电话:456 年龄:17
姓名:Baby 性别:女 电话:789 年龄:27
姓名:Linda 性别:女 电话:000 年龄:21
2).创建一个AddressBook类的实例对象(通讯录).
3).添加联系人,将四个联系人添加到通讯录中.
4).根据电话查找联系人,并打印(查找电话:456);
5).根据性别查找联系人,并将查到的联系人按照年龄升序排序,并打印(查找性别:女)
6).根据姓名,更改联系人信息.(修改Linda的信息: 性别:女 电话:012 年龄:25);
7).按照姓名删除联系人,并展示通信录中所有人信息.(删除联系人:Baby)
8).删除一个分组,并展示通信录中所有人信息.(删除L组)
9).实现3~8之间每个功能后,输出通讯录中所有联系人信息。
Person.h-------------------------------------------------------
#import
@interface Person : NSObject
{
NSString *_name;
NSString *_sex;
NSString *_phoneNum;
NSString *_groupName;
NSInteger _age;
}
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *sex;
@property(nonatomic,retain)NSString *phoneNum;
@property(nonatomic,retain)NSString *groupName;
@property(nonatomic,assign)NSInteger age;
-(id)initWithName:(NSString *)name
sex:(NSString *)sex
phoneNum:(NSString *)phoneNum
groupName:(NSString *)groupName
age:(NSInteger)age;
+(id)personWithName:(NSString *)name
sex:(NSString *)sex
phoneNum:(NSString *)phoneNum
groupName:(NSString *)groupName
age:(NSInteger)age;
@end
#import "Person.h"
@implementation Person
//初始化:
-(id)initWithName:(NSString *)name
sex:(NSString *)sex
phoneNum:(NSString *)phoneNum
groupName:(NSString *)groupName
age:(NSInteger)age{
self = [super init];
if (self) {
_name = name;
_sex = sex;
_phoneNum = phoneNum;
_groupName = groupName;
_age = age;
}
return self;
}
//便利构造器:
+(id)personWithName:(NSString *)name
sex:(NSString *)sex
phoneNum:(NSString *)phoneNum
groupName:(NSString *)groupName
age:(NSInteger)age{
Person *p = [[Person alloc] initWithName:name sex:sex phoneNum:phoneNum groupName:groupName age:age];
return [p autorelease];
}
//重写description方法:
-(NSString *)description{
return [NSString stringWithFormat:@"姓名:%@ 性别:%@ 电话:%@ 分组:%@ 年龄:%ld",_name,_sex,_phoneNum,_groupName,_age];
}
//Person被销毁:
-(void)dealloc{
[_name release];
[_sex release];
[_phoneNum release];
[_groupName release];
NSLog(@"Person被销毁了");
[super dealloc];
}
@end
#import
@class Person;
@interface AddressBook : NSObject
{
NSMutableDictionary *_allPersonDic;
}
@property(nonatomic,retain)NSMutableDictionary *allPersonDic;
//初始化:
-(id)init;
//添加联系人:
-(BOOL)addPerson:(Person *)aPerson;
//显示所有联系人:
-(void)showAllPerson;
//根据电话查找联系人:
-(Person *)findPersonByPhoneNum:(NSString *)aPhoneNum;
//根据性别查找联系人,并按照年龄升序排序:
-(NSArray *)findPersonBySex:(NSString *)aSex;
//根据姓名,更改联系人信息:
-(BOOL)changePersonWithName:(NSString *)name sex:(NSString *)aSex phoneNum:(NSString *)aNum groupName:(NSString *)agroup age:(NSUInteger) aAge;
//按照姓名删除联系人:
-(BOOL)deletePersonWithName:(NSString *)aPersonName;
//删除一个分组:
-(BOOL)deleteGroup:(NSString *)aGroupName;
@end
#import "AddressBook.h"
#import "Person.h"
@implementation AddressBook
//初始化:
-(id)init{
self = [super init];
if (self) {
_allPersonDic = [[NSMutableDictionary alloc] init];
}
return self;
}
//重写dealloc:
-(void)dealloc{
[_allPersonDic release];
NSLog(@"AddressBook被销毁了");
[super dealloc];
}
//添加联系人:
-(BOOL)addPerson:(Person *)aPerson{
//判断是否为空
if ((aPerson.name == nil) || (aPerson.phoneNum == nil) ) {
NSLog(@"添加失败");
return NO;
}else{
NSString *newName = nil;
NSString *first = [aPerson.name substringToIndex:1];
for (NSString *group in [_allPersonDic allKeys]) {
if ([first isEqualToString:group]) {
//判断是否重名:
for (Person *tempPerson in [_allPersonDic objectForKey:group]) {
if ([tempPerson.name isEqualToString:aPerson.name]) {
return NO;
}
}
newName = group;
break;
}
}
if ([_allPersonDic objectForKey:first] == nil) {
NSMutableArray *newArr = [NSMutableArray arrayWithObject:aPerson];
[_allPersonDic setObject:newArr forKey:first];
return YES;
}else{
[[_allPersonDic objectForKey:first] addObject:aPerson];
return YES;
}
}
}
//显示所有联系人:
-(void)showAllPerson{
for (NSString *group in [_allPersonDic allKeys]) {
NSLog(@"------%@------",group);
for (Person *tempPerson in [_allPersonDic objectForKey:group]) {
NSLog(@"%@",tempPerson);
}
}
}
//根据电话查找联系人:
-(Person *)findPersonByPhoneNum:(NSString *)aPhoneNum{
for (NSString *group in _allPersonDic) {
NSMutableArray *groupArr = [_allPersonDic objectForKey:group];
for (Person *tempPerson in groupArr) {
if ([aPhoneNum isEqualToString:tempPerson.phoneNum]) {
return tempPerson;
}
}
}
return nil;
}
//根据性别查找联系人,并按照年龄升序排序(block方法):
-(NSArray *)findPersonBySex:(NSString *)aSex{
NSMutableArray *arr = [NSMutableArray array];
for (NSString *group in _allPersonDic) {
for (Person *tempPerson in [_allPersonDic objectForKey:group]) {
if ([aSex isEqualToString:tempPerson.sex]) {
[arr addObject:tempPerson];
}
}
}
[arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 age] > [obj2 age];
}];
return arr;
}
//根据姓名,更改联系人信息:
-(BOOL)changePersonWithName:(NSString *)name sex:(NSString *)aSex phoneNum:(NSString *)aNum groupName:(NSString *)aGroup age:(NSUInteger)aAge{
Person *p = nil;
for (NSMutableArray *tempArr in [_allPersonDic allValues]) {
for (Person *tempPerson in tempArr) {
if ([tempPerson.name isEqualToString:name]) {
p = tempPerson;
break;
}
}
}
if (p == nil) {
NSLog(@"查无此人");
return NO;
}else{
NSLog(@"修改前:%@",p);
p.phoneNum = aNum;
p.sex = aSex;
p.age = aAge;
return YES;
}
}
//按照姓名删除联系人:
-(BOOL)deletePersonWithName:(NSString *)aPersonName{
Person *p = nil;
NSString * recordKey = nil;
for (NSString *group in [_allPersonDic allKeys]) {
for (Person *tempPerson in [_allPersonDic objectForKey:group]) {
if ([tempPerson.name isEqualToString:aPersonName]) {
recordKey = group;
p = tempPerson;
break;
}
}
}
if (p == nil) {
NSLog(@"查无此人");
return NO;
}else{
NSLog(@"根据姓名找到联系人%@",p);
[[_allPersonDic objectForKey:recordKey] removeObject:p];
//判断删除联系人后分组是否为空,是则删除该分组。
if ([[_allPersonDic objectForKey:recordKey] count] == 0) {
[_allPersonDic removeObjectForKey:recordKey];
}
return YES;
}
}
//删除一个分组:
-(BOOL)deleteGroup:(NSString *)aGroupName{
NSArray *arr = [_allPersonDic allKeys];
for (NSString *group in arr) {
if ([group isEqualToString:aGroupName]) {
[_allPersonDic removeObjectForKey:group];
}
}
return YES;
}
@end