1.Contact.h
#import <Foundation/Foundation.h>
@interface Contact : NSObject
/**
* 姓名
*/
@property(nonatomic, copy) NSString *name;
/**
* 电话号码
*/
@property(nonatomic, copy) NSString *phoneNumber;
/**
* 分组名称
*/
@property(nonatomic, copy) NSString *groupName;
- (instancetype)initWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber;
+ (instancetype)contactWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber;
- (void)showInfo;
@end
2.Contact.m
#import "Contact.h"
@implementation Contact
#pragma mark 重写dealloc
- (void)dealloc
{
[_name release];
[_phoneNumber release];
[_groupName release];
[super dealloc];
NSLog(@"contact对象已释放!");
}
#pragma mark 初始化方法
- (instancetype)initWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber
{
self = [super init];
if (self)
{
//初始化过程中直接将姓名首字母大写,规范格式
_name = [name capitalizedString];
_phoneNumber = phoneNumber;
//直接将分组名设置为首字母
_groupName = [_name substringToIndex:1];
}
return self;
}
#pragma mark 遍历构造器
+ (instancetype)contactWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber
{
return [[[Contact alloc] initWithName:name PhoneNumber:phoneNumber] autorelease];
}
#pragma mark 显示联系人信息
- (void)showInfo
{
NSLog(@"%@-%@", self.name, self.phoneNumber);
}
@end
3.ContactBook.h
#import#import "Contact.h"
@interface ContactBook : NSObject
/**
* 联系人字典
*/
@property(nonatomic, retain) NSMutableDictionary *contactDic;
- (void)showContactInfo;
- (void)addNewContact;
- (void)removeContactByName;
- (void)removeContactByPhoneNumber;
- (void)removeAllGroup;
- (void)searchByName;
- (void)searchByPhoneNumber;
- (void)sortByNameAscending;
@end
4.Contact.m
#import "ContactBook.h"
@implementation ContactBook
#pragma mark 重写init
- (instancetype)init
{
self = [super init];
if (self)
{
//在init中进行字典的初始化过程,可以让程序更好的封装
//1.定义可变数组allKey接收26个大写英文字母
NSMutableArray *allKey = [NSMutableArray array];
//2.定义可变数组allValue接收26个存储在对应key下的存储联系人的数组
NSMutableArray *allValue = [NSMutableArray array];
for (NSInteger i = 65; i < 91; i++)
{
//此处通过format这个万能公式将长整形i转化为字符类型,将26个字母存入allKey数组(此处操作会出现编译警告,暂时没想出其他的方法)
[allKey addObject:[NSString stringWithFormat:@"%c",i]];
//此处将26个空数组放入allValue
[allValue addObject:[NSMutableArray array]];
}
//通过allKey和allValue为存储字典赋值
_contactDic = [NSMutableDictionary dictionaryWithObjects:allValue forKeys:allKey];
}
//用来检测字典是否创建成功,正式运行时可注释
NSLog(@"%@", _contactDic);
return self;
}
#pragma mark 显示通讯录联系人信息
- (void)showContactInfo
{
//用来进行计数
NSInteger count = 0;
//对字典进行循环
for (NSString *str in self.contactDic)
{
//对每个key对应的数组进行遍历,输出联系人
for (Contact *contact in [self.contactDic objectForKey:str])
{
NSLog(@"%@-%@", contact.name, contact.phoneNumber);
count ++;
}
}
//如果flag等于0则说明26个数组全为空
if (count == 0)
{
//输出通讯录为空
printf("通讯录为空!\n");
}
else
{
NSLog(@"输出结束,总共%ld个联系人!", count);
}
}
#pragma mark 添加联系人
- (void)addNewContact
{
char name[30];
printf("请输入联系人姓名:");
scanf("%s", name);
char phoneNumber[30];
printf("请输入联系人电话号码:");
scanf("%s", phoneNumber);
//通过NSString的方法将用户输入字符串转换为对象
NSString *s_name = [NSString stringWithUTF8String:name];
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
//用s_name和s_phoneNumber新建联系人
Contact *newContact = [Contact contactWithName:s_name PhoneNumber:s_phoneNumber];
//通过分组名称找到对应数组将联系人添加进数组
[[self.contactDic objectForKey:[newContact groupName]] addObject:newContact];
printf("添加成功!\n");
NSLog(@"%@", self.contactDic);
}
#pragma mark 通过姓名删除联系人
- (void)removeContactByName
{
NSInteger count = 0;
char name[30];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
NSString *s_name = [[NSString stringWithUTF8String:name] capitalizedString];
NSString *s_groupName = [s_name substringToIndex:1];
//接收groupName对应的目标数组
NSMutableArray *targetArray = [self.contactDic objectForKey:s_groupName];
for (Contact *contact in targetArray)
{
if ([contact.name isEqualToString: s_name])
{
[targetArray removeObject:contact];
count ++;
printf("删除成功!\n");
}
}
//如果没有删除到联系人则显示无此联系人
if (count == 0)
{
printf("无此联系人!\n");
}
}
#pragma mark 通过电话号码删除联系人
- (void)removeContactByPhoneNumber
{
NSInteger count = 0;
char phoneNumber[30];
printf("请输入联系人电话号码:");
scanf("%s", phoneNumber);
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
for (NSString *str in self.contactDic)
{
NSMutableArray *targetArray = [self.contactDic objectForKey:str];
for (Contact *contact in targetArray)
{
if ([contact.phoneNumber isEqualToString:s_phoneNumber])
{
[targetArray removeObject:contact];
printf("删除成功!\n");
count ++;
}
}
[targetArray release];
}
//如果没有删除到联系人则显示无此联系人
if (count == 0)
{
printf("此电话号码不在通讯录中!\n");
}
}
#pragma mark 删除分组
- (void)removeAllGroup
{
char groupName[30];
printf("请输入分组字母:");
scanf("%s", groupName);
NSString *s_groupName = [[NSString stringWithUTF8String:groupName] capitalizedString];
//如果输入字符过多则返回输入有误
if (s_groupName.length > 1)
{
printf("输入有误!");
}
else if ((groupName[0] >= 'a' && groupName[0] <= 'z') || (groupName[0] >= 'A' && groupName[0] <= 'Z'))
{
[self.contactDic removeObjectForKey:s_groupName];
printf("删除成功!\n");
}
}
#pragma mark 按照姓名查找
- (void)searchByName
{
NSInteger count = 0;
char name[30];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
NSString *s_name = [[NSString stringWithUTF8String:name] capitalizedString];
NSString *s_groupName = [s_name substringToIndex:1];
//接收groupName对应的目标数组
NSMutableArray *targetArray = [self.contactDic objectForKey:s_groupName];
for (Contact *contact in targetArray)
{
if ([contact.name isEqualToString: s_name])
{
[contact showInfo];
}
}
//如果没有删除到联系人则显示无此联系人
if (count == 0)
{
printf("无此联系人!\n");
}
[s_groupName release];
[targetArray release];
}
#pragma mark 按电话号码查找
- (void)searchByPhoneNumber
{
NSInteger count = 0;
char phoneNumber[30];
printf("请输入联系人电话号码:");
scanf("%s", phoneNumber);
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
for (NSString *str in self.contactDic)
{
NSMutableArray *targetArray = [self.contactDic objectForKey:str];
for (Contact *contact in targetArray)
{
if ([contact.phoneNumber isEqualToString:s_phoneNumber])
{
[contact showInfo];
}
}
[targetArray release];
}
//如果没有删除到联系人则显示无此联系人
if (count == 0)
{
printf("此电话号码不在通讯录中!\n");
}
}
#pragma mark 按照姓名升序排序
- (void)sortByNameAscending
{
NSSortDescriptor *desName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:1];
for (NSString *str in self.contactDic)
{
//进行排序
NSMutableArray *targetArr = [self.contactDic objectForKey:str];
[targetArr sortUsingDescriptors:@[desName]];
//输出排序结果
for (Contact *contact in targetArr)
{
[contact showInfo];
}
[targetArr release];
}
[desName release];
}
@end
5.main.m
#import <Foundation/Foundation.h>
#import "Contact.h"
#import "ContactBook.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
ContactBook *myContactBook = [[ContactBook alloc] init];
while (YES)
{
printf("-----------------------------\n");
printf("------------通讯录------------\n");
printf("------1.查看通讯全部联系人------\n");
printf("------2.添加联系人-------------\n");
printf("------3.删除联系人-------------\n");
printf("------4.查找联系人-------------\n");
printf("------5.对联系人进行排序--------\n");
printf("------6.退出------------------\n");
printf("-----------------------------\n");
//提示用户输入操作代码
NSInteger userNumber = 0;
printf("请输入您要进行的操作:");
scanf("%ld", &userNumber);
//运用switch语句进行分支功能的执行
switch (userNumber)
{
case 1:
{
[myContactBook showContactInfo];
}
break;
case 2:
{
[myContactBook addNewContact];
}
break;
case 3:
{
BOOL b = YES;
while (b)
{
printf("-----------------------------\n");
printf("--------1.通过姓名删除---------\n");
printf("--------2.通过号码删除---------\n");
printf("--------3.删除整个分组---------\n");
printf("--------4.返回上级菜单---------\n");
printf("-----------------------------\n");
NSInteger Num = 0;
printf("请输入操作代码:");
scanf("%ld", &Num);
switch (Num) {
case 1:
{
[myContactBook removeContactByName];
}
break;
case 2:
{
[myContactBook removeContactByPhoneNumber];
}
break;
case 3:
{
[myContactBook removeAllGroup];
}
break;
case 4:
{
b = NO;
break;
}
break;
default:
{
printf("输入有误!\n");
break;
}
break;
}
}
}
break;
case 4:
{
BOOL b = YES;
while (b)
{
printf("-----------------------------\n");
printf("--------1.通过姓名查找---------\n");
printf("--------2.通过号码查找---------\n");
printf("--------3.返回上级菜单---------\n");
printf("-----------------------------\n");
NSInteger Num = 0;
printf("请输入操作代码:");
scanf("%ld", &Num);
switch (Num) {
case 1:
{
[myContactBook searchByName];
}
break;
case 2:
{
[myContactBook searchByPhoneNumber];
}
break;
case 3:
{
b = NO;
break;
}
break;
default:
{
printf("输入有误!\n");
break;
}
break;
}
}
}
break;
case 5:
{
[myContactBook sortByNameAscending];
}
break;
case 6:
{
return 0;
}
break;
default:
break;
}
}
}
return 0;
}
网友评论