OC简易通讯录

来源:互联网 发布:2016淘宝客赚不赚钱 编辑:程序博客网 时间:2024/06/15 18:14

// AddressBook.m

import “AddressBook.h”

@implementation AddressBook

//重写初始化方法
//- (instancetype)init
//{
// self = [super init];
// if (self) {
// //初始化对象的同时 初始化数组
// _contactArray = [NSMutableArray array];
// }
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
_contactArray = [NSMutableArray array];
}
return self;
}
//添加新联系人
//- (void)addContact:(Contact *)contact
//{
//
// if ([[contact name]length] == 0 || [[contact telephone]length ] == 0) {
// NSLog(@”打印失败”);
// }else
// {
// [_contactArray addObject:contact];
// }
//}
- (void)addContact:(Contact *)contact
{
if ([[contact name]length] == 0 || [[contact telephone]length] == 0) {
NSLog(@”打印失败”);
}else
{
[_contactArray addObject:contact];
}
}
//获取某个分组下的所有联系⼈
- (void)getGroupAllContactByGroupName:(NSString *)groupName
{
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人
Contact *contact = _contactArray[i];
//判断是否与分组名相同 相同 打印
if ([[contact groupName] isEqualToString:groupName]) {
NSLog(@”%@”,contact);
}

}

}

//通过电话号码查找联系人
- (void)findContactByTelephone:(NSString *)telephone
{
//遍历整个数组
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人
Contact *contact = _contactArray[i];
//判断电话号码是否相等 相等 输出
if ([[contact telephone] isEqualToString:telephone]) {
NSLog(@”%@”,contact);
}
}

}

//获取所有⼥性联系⼈;
- (void)findAllWomanContact:(NSString *)sex
{
//遍历整个数组
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人

    Contact *contact = _contactArray[i];    //判断性别是否相等  相等 输出    if ([[contact sex] isEqualToString:sex]) {        NSLog(@"%@",contact);    }}

}

//根据姓名删除联系⼈
- (void)deleteContactByName:(NSString *)name
{
for (int i = 0; i < [_contactArray count]; i++) {

    //判断姓名是否相等  相等 删除    if ([[_contactArray[i] name] isEqualToString:name]) {        [_contactArray  removeObject:_contactArray[i]];        i--;    }else {    NSLog(@"%@",_contactArray[i]);    }}

}
//删除某个分组全部联系⼈
- (void)deleteGroupAllContactByGroupName:(NSString *)groupName
{
for (int i = 0; i < [_contactArray count]; i++) {

    //判断分组名称是否相等  相等 删除    if ([[_contactArray[i] groupName] isEqualToString: groupName]) {        [_contactArray  removeObject:_contactArray[i]];        i--;    }else    {        NSLog(@"%@",_contactArray[i]);    }}

}
//展⽰通讯录中所有联系⼈
- (void)printfAllContact
{
for (int i = 0; i < [_contactArray count]; i++) {
NSLog(@”%@”,_contactArray[i]);
}
}

// main .m
Contact *c1 = [[Contact alloc]initWithName:@”尼鹏” sex:@”不祥” telephone:@”123456789” address:@”妓院” groupName:@”D”];
Contact *c2 = [[Contact alloc]initWithName:@”陆桃桃” sex:@”男” telephone:@”12345” address:@”学院” groupName:@”C”];
Contact *c3 = [[Contact alloc]initWithName:@”大军” sex:@”女” telephone:@”1234569” address:@”学习院” groupName:@”B”];
Contact *c4 = [[Contact alloc]initWithName:@”徐阳” sex:@”女” telephone:@”123459” address:@”试试” groupName:@”C”];
Contact *c5 = [[Contact alloc]initWithName:@”流汗” sex:@”男” telephone:@”13456789” address:@”55院” groupName:@”D”];
AddressBook *address = [[AddressBook alloc]init];
[address addContact:c1];
[address addContact:c2];
[address addContact:c3];
[address addContact:c4];
[address addContact:c5];
[address deleteGroupAllContactByGroupName:@”B”];
NSLog(@”\n”);
[address printfAllContact];
NSLog(@”\n”);
[address getGroupAllContactByGroupName:@”同学”];
NSLog(@”\n”);
[address findAllWomanContact:@”女”];
NSLog(@”\n”);
[address findContactByTelephone:@”123456789”];
NSLog(@”\n”);
[address deleteContactByName:@”大军”];
NSLog(@”\n”);
[address deleteContactByName:@”尼鹏”];
NSLog(@”\n”);

[address deleteContactByName:@"徐阳"];NSLog(@"\n");[address printfAllContact];

NSLog(@”\n”);
[address printfAllContact];
NSLog(@”\n”)

0 0