通讯录 oc

来源:互联网 发布:三维模型渲染软件 编辑:程序博客网 时间:2024/05/27 21:01

源代码:



#import <Foundation/Foundation.h>
#import "Contact.h"
#import "AddressBook.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Contact *list=[Contact contactName:@"name" Gender:@"gender" Phone:@"phone" Address:@"address" Group_Name:@"group"];
        Contact *friend1=[Contact contactName:@"pyte" Gender:@"male" Phone:@"13705678911" Address:@"auti" Group_Name:@"staff"];
        Contact *friend2=[Contact contactName:@"lizi" Gender:@"female" Phone:@"15789342031" Address:@"chongqing" Group_Name:@"bestFridend"];
        Contact *friend3=[Contact contactName:@"yan" Gender:@"female" Phone:@"15834562178" Address:@"nanchuan" Group_Name:@"bestFriend"];
        Contact *friend4=[Contact contactName:@"tutor" Gender:@"male" Phone:@"15724631098" Address:@"xiaohe" Group_Name:@"bestFriend"];
        Contact *friend5=[Contact contactName:@"yuanyuan" Gender:@"male" Phone:@"18231245687" Address:@"chengdu" Group_Name:@"bestFriend"];
        
        NSMutableArray *contactArray=[NSMutableArray array];
        [contactArray addObject:list];
        [contactArray addObject:friend1];
        [contactArray addObject:friend2];
        [contactArray addObject:friend3];
        [contactArray addObject:friend4];
        [contactArray addObject:friend5];
        
        /*
        //显示联系⼈人信息
        NSLog(@"%@",contactArray);
        //、在main.m中定义可变数组,管理所有联系⼈人。可以添加新联系⼈人对象,如果姓名
        //或电话号码为空,打印添加失败。
        Contact *friend6=[[Contact alloc]initWithName:nil Gender:nil Phone:nil Address:nil Group_Name:nil];
        if (nil==[friend6 Name] || nil==[friend6 Phone]) {
            NSLog(@"姓名或者电话号码为空,添加失败");
        }else{
            [contactArray addObject:friend6];
        }
        //、获取某个分组下的所有联系⼈人。
        NSString *group_name=@"bestFriend";
        for (int i=0; i<[contactArray count]; i++) {
            if ([[contactArray[i] Group_Name] isEqualToString:group_name]) {
                NSLog(@"%@",contactArray[i]);
            }
        }
        //、根据电话号码搜索联系⼈人。
        NSString *phone=@"15789012345";
        for (int i=0; i<[contactArray count]; i++) {
            if ([[contactArray[i] Phone] isEqualToString:phone]) {
                NSLog(@"%@",contactArray[i]);
            }
        }
        //、获取所有⼥女性联系⼈人
        NSString *gender=@"female";
        for (int i=0; i<[contactArray count]; i++) {
            if ([[contactArray[i] Gender] isEqualToString:gender]) {
                NSLog(@"%@",[contactArray[i] Name]);
            }
        }
        //6
        //、根据姓名删除联系⼈人
        NSString *name=@"yuanyuan";
        for (int i=0; i<[contactArray count]; i++) {
            if ([[contactArray[i] Name] isEqualToString:name]) {
                [contactArray removeObject:contactArray[i]];
            }
        }
        //7
        //、删除某个分组全部联系⼈人
        NSString *Group_name=@"staff";
        for (int i=0; i<[contactArray count]; i++) {
            if ([[contactArray[i] Group_Name] isEqualToString:Group_name]) {
                [contactArray removeObject:contactArray[i]];
            }
        }
        //8
        //、展⽰示通讯录中所有联系⼈人
        for (int i=0; i<[contactArray count]; i++) {
            NSLog(@"%@",[contactArray[i] name]);
        }
         */
        
        //9
        //选做:定义AddressBook类,封装上述功能
        //1
        AddressBook *manage=[[AddressBook alloc]init];
        [manage showAllMessage:contactArray];
        //2
        Contact *friend7=[Contact contactName:@"jiwa" Gender:@"female" Phone:@"13254679821" Address:@"beijing" Group_Name:@"bestFriend"];
        [manage addContactObject:contactArray :friend7];
        //3
        NSString *_group=@"staff";
        [manage gainAllGruopFriend:contactArray :_group];
        //4
        NSString *_phone=@"15798237621";
        [manage findFridendByPhone:contactArray :_phone];
        //5
        NSString *_gender=@"female";
        [manage gainAllFemaleFriendMessage:contactArray :_gender];
        //6
        NSString *_name=@"shitou";
        [manage deleteMessageByName:contactArray :_name];
        //7
        NSString *_groupName=@"gf";
        [manage deleteMessageByGroup:contactArray :_groupName];
        //8
        [manage showAllFriend:contactArray];
    }
    return 0;
}
//、定义联系⼈人类Contact
//。实例变量:姓名、性别、电话号码、住址、分组名称。方法:初始化⽅方法(姓名、电话号码)、
//1、
//显⽰示联系⼈人信息
//2
//、在main.m中定义可变数组,管理所有联系⼈人。可以添加新联系⼈人对象,如果姓名
//或电话号码为空,打印添加失败。
//3
//、获取某个分组下的所有联系⼈人。
//4
//、根据电话号码搜索联系⼈人。
//5
//、获取所有⼥女性联系⼈人
//6
//、根据姓名删除联系⼈人
//7
//、删除某个分组全部联系⼈人
//8
//、展⽰示通讯录中所有联系⼈人
//9
//、
//选做:定义AddressBook类,封装上述功能


======》Contact.h

//
//  Contact.h
//  oc 作业4_15-8-18
//
//  Created by lanou3g on 15/8/18.
//  Copyright (c) 2015年 周屹. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Contact : NSObject
{
    NSString *_name;
    NSString *_gender;
    NSString *_phone;
    NSString *_address;
    NSString *_group_name;
}
//自定义初始化
-(id)initWithName:(NSString*)name
           Gender:(NSString*)gender
            Phone:(NSString*)phone
          Address:(NSString*)address
       Group_Name:(NSString*)group_name;
//setter以及getter访问机制
-(void)setPhone:(NSString*)phone;
-(void)setAddress:(NSString*)address;
-(void)setGroup_name:(NSString*)group_name;
-(id)Name;
-(id)Gender;
-(id)Phone;
-(id)Address;
-(id)Group_Name;
//构造器
+(id)contactName:(NSString*)name
          Gender:(NSString*)gender
           Phone:(NSString*)phone
         Address:(NSString*)address
      Group_Name:(NSString*)group_name;
//
-(NSString *)description;
@end
//。实例变量:姓名、性别、电话号码、住址、分组名称。方法:初始化⽅方法(姓名、电话号码)、显⽰示联系⼈人信息

======》Contact.m




#import "Contact.h"

@implementation Contact
-(id)initWithName:(NSString*)name
           Gender:(NSString*)gender
            Phone:(NSString*)phone
          Address:(NSString*)address
       Group_Name:(NSString*)group_name{
    self=[super init];
    if (self) {
        _name=name;
        _gender=gender;
        _phone=phone;
        _address=address;
        _group_name=group_name;
    }
    return self;
}
-(void)setPhone:(NSString*)phone{
    _phone=phone;
}
-(void)setAddress:(NSString*)address{
    _address=address;
}
-(void)setGroup_name:(NSString*)group_name{
    _group_name=group_name;
}
+(id)contactName:(NSString*)name
          Gender:(NSString*)gender
           Phone:(NSString*)phone
         Address:(NSString*)address
      Group_Name:(NSString*)group_name{
    Contact *fridend=[[Contact alloc]initWithName:name Gender:gender Phone:phone Address:address Group_Name:group_name];
    return fridend;
}
-(id)Name{
    return _name;
}
-(id)Gender{
    return _gender;
}
-(id)Phone{
    return _phone;
}
-(id)Address{
    return _address;
}
-(id)Group_Name{
    return _group_name;
}
-(NSString *)description{
    return [NSString stringWithFormat:@"%@  %@  %@  %@  %@",_name,_gender,_phone,_address,_group_name];
}
@end


=======》AddressBook.h




#import <Foundation/Foundation.h>
#import "Contact.h"
@interface AddressBook : NSObject
//1、
//显⽰示联系⼈人信息
-(void)showAllMessage:(NSMutableArray *)contactArray;
//2
//、可以添加新联系⼈人对象,如果姓名或电话号码为空,打印添加失败。
-(void)addContactObject:(NSMutableArray*)contactArray : (Contact*)fridend;
//3
//、获取某个分组下的所有联系⼈人。
-(void)gainAllGruopFriend:(NSMutableArray*)contactArray :(NSString*)group;
//4
//、根据电话号码搜索联系⼈人。
-(void)findFridendByPhone:(NSMutableArray*)contactArray :(NSString*)phone;
//5
//、获取所有⼥女性联系⼈人
-(void)gainAllFemaleFriendMessage:(NSMutableArray*)contactArray :(NSString*)gender;
//6
//、根据姓名删除联系⼈人
-(void)deleteMessageByName:(NSMutableArray*)contactArray :(NSString*)name;
//7
//、删除某个分组全部联系⼈人
-(void)deleteMessageByGroup:(NSMutableArray*)contacArray :(NSString*)group;
//8
//、展⽰示通讯录中所有联系⼈人
-(void)showAllFriend:(NSMutableArray*)contactArray;
@end


========》AddressBook.m




#import "AddressBook.h"

@implementation AddressBook
//1
-(void)showAllMessage:(NSMutableArray *)contactArray{
    NSLog(@"%@",contactArray);
}
//2
-(void)addContactObject:(NSMutableArray *)contactArray : (Contact*)fridend{
    [contactArray addObject:fridend];
    AddressBook *manage=[[AddressBook alloc]init];
    [manage showAllMessage:contactArray];
}
//3
-(void)gainAllGruopFriend:(NSMutableArray*)contactArray :(NSString*)group{
    for (int i=0; i<[contactArray count]; i++) {
        if ([[contactArray[i] Group_Name] isEqualToString:group]) {
            NSLog(@"%@",contactArray[i]);
        }
    }
}
//4
-(void)findFridendByPhone:(NSMutableArray*)contactArray :(NSString*)phone{
    for (int i=0; i<=[contactArray count]; i++) {
        //注意判断的先后顺序
        if (i==[contactArray count]) {
            NSLog(@"抱歉,您的通讯录里没有该号码的好友.");
            break;
        }
        if ([[contactArray[i] Phone] isEqualToString:phone]) {
            NSLog(@"%@",contactArray[i]);
        }
        
    }
}
//5
-(void)gainAllFemaleFriendMessage:(NSMutableArray*)contactArray :(NSString*)gender{
     NSLog(@"获得所有%@的信息,如下:",gender);
    for (int i=0; i<[contactArray count]; i++) {
        if ([[contactArray[i] Gender] isEqualToString:gender]) {
            NSLog(@"%@",contactArray[i]);
        }
    }
}
//6
-(void)deleteMessageByName:(NSMutableArray*)contactArray :(NSString*)name{
    for (int i=0; i<=[contactArray count]; i++) {
        if (i==[contactArray count]) {
            NSLog(@"抱歉,通讯录里没有你要删除的联系人信息.");
            break;
        }
        if ([[contactArray[i] Name] isEqualToString:name]) {
            [contactArray removeObject:contactArray[i]];
        }
    }
}
//7
-(void)deleteMessageByGroup:(NSMutableArray*)contacArray :(NSString*)group{
    for (int i=0; i<=[contacArray count]; i++) {
        if (i==[contacArray count]) {
            NSLog(@"你的通讯录里不存在该分组.");
            break;
        }
        if ([[contacArray[i] Group_Name] isEqualToString:group]) {
            [contacArray removeObject:contacArray[i]];
        }
    }
}
//8
-(void)showAllFriend:(NSMutableArray*)contactArray{
    for (int i=0; i<[contactArray count]; i++) {
        NSLog(@"%@",[contactArray[i] Name]);
    }
}
@end


0 0