5.28,一道oc的混合题

综合练习:模拟一个通讯录过程
@”请选择以下功能:
1、添加联系人
2、移除联系人
3、修改联系人信息
4、显示联系人列表
5、退出系统
要求:创建一个联系人类,其实现过程,在实例方法中,不能将过程写在main函数中;
提示:用字典实现,输入联系人姓名使用英文;将首字母作为key,将联系人作为value进行存储;
并且实现数据持久化(补充内容归档)

//  Contacter.h

#import <Foundation/Foundation.h>

@interface Contacter : NSObject

@property (strong ,nonatomic)NSString       *name;
@property (strong,nonatomic) NSString       *phone;



@end
//  Contacter.m

#import "Contacter.h"

@implementation Contacter

//实时输出所有信息
-(NSString *)description
{
    return [NSString stringWithFormat:@"<Contacter:name=%@ phone = %@>",_name,_phone];
}
@end
//  AddressBook.h

#import <Foundation/Foundation.h>
#import "Contacter.h"

@interface AddressBook : NSObject

@property (strong,nonatomic)NSMutableDictionary        *contact;

- (void)start;

- (void)addContacts;

- (void)removeContacts;

- (void)modifyContacts;

- (void)showContactLish;

- (void)exit;


@end
//
//  AddressBook.m

#import "AddressBook.h"

@implementation AddressBook

//        添加联系人
//        移除联系人"
//        修改联系人信息
//        显示联系人列表"
//        退出系统"

- (void)start
{
    // 1,初始化所有需要的数据
    if (!_contact) {

        //第一步,从文件中读出所有联系人数据,每个人是一个字典
        NSArray *fileInfo = [NSArray arrayWithContentsOfFile:@"/Users/rimi/Desktop/1.doc"];
        //初始化容器
        _contact = [NSMutableDictionary dictionary];
        NSLog(@"%@",fileInfo);
        //依次遍历所有字典
        for (NSDictionary *info  in  fileInfo) {
            //把每个字典转化对象
            Contacter *contact = [[Contacter alloc]init];
            NSDictionary *info = @{@"name":@"jia",@"phone":@"1213"};//数据从文件中读出来
            contact.name = info[@"name"];
            contact.phone = info[@"phone"];

            [_contact setObject:contact forKey:[contact.name substringFromIndex:1]];
        }

    }

    // 2,开始选择菜单
    NSLog(@"请选择以下功能:\n1、添加联系人\n2、移除联系人\n3、修改联系人信息\n 4、显示联系人列表\n5、退出系统\n");

    NSLog(@"选择要执行的功能编号:");
    NSInteger number;
    scanf("%ld",&number);

    switch (number) {
        case 1:
            [self addContacts];break;
        case 2:
            [self removeContacts];break;
        case 3:
            [self modifyContacts];break;
        case 4:
            [self showContactLish];break;
        case 5:
            [self exit];break;
        default:
            break;
    }

}

- (void)addContacts
{
    //一次性输入联系人信息
    NSLog(@"一次性输入联系人信息(姓名,电话,用空格隔开\n");
    char name[20]; //名字
    char phone[15]; //电话
    scanf("%s %s",name,phone);
    //NSLog(@"如果输入成功,显示一下:%s,%s",name,phone);
    NSString  *contactName = [NSString stringWithUTF8String:name];//转化成oc中的字符串类型

    NSString  *contactPhone =[NSString stringWithUTF8String:phone];
    NSLog(@"%@ %@",contactName,contactPhone);

    Contacter *newContact = [[Contacter alloc]init];
    newContact.name = contactName;
    newContact.phone = contactPhone;
    //添加进字典
    [_contact setObject:newContact forKey:[newContact.name substringFromIndex:1]];
    NSLog(@"%@",_contact);

    NSLog(@"操作完成!请继续选择菜单");
    //[self saveInfo];
    [self start];


}
- (void)removeContacts
{
    NSLog(@"请输入要移除的联系人的首字母");
    getchar();//接受上一次输入的回车字符,
    char firstChar;
    scanf("%c",&firstChar);
    NSString *key = [NSString stringWithFormat:@"%c",firstChar];//字符串转对象

    if ([self.contact objectForKey:key]) {
        //判定取到的联系人是否存在
        [self.contact removeObjectForKey:key];
        NSLog(@"移除成功");
    } else {
        NSLog(@"未找到联系人");
    }
    //[self saveInfo];
    [self start];

}

- (void)modifyContacts
{
    NSLog(@"请输入要修改的联系人的首字母");
    getchar();//接受上一次输入的回车字符,
    char firstChar;
    scanf("%c",&firstChar);
    NSString *key = [NSString stringWithFormat:@"%c",firstChar];//字符串转对象

    Contacter *contacter = [self.contact objectForKey:key];
    NSLog(@"选择要更改的信息:1,名称 2,电话");
    NSInteger number;
    scanf("%ld",&number);

    if (number == 1) {
        NSLog(@"输入新的联系人名称");
        char name[20];
        scanf("%s",name);

        contacter.name = [NSString stringWithFormat:@"%s",name];//将获取到的名字,赋值到指定的来那些人

    } else {
            NSLog(@"输入新的联系人电话");
            char phone[20];
            scanf("%s",phone);

            contacter.phone = [NSString stringWithFormat:@"%s",phone];//将获取到的名字,赋值到指定的来那些人
}
    //[self saveInfo];
    [self start];
}

- (void)showContactLish
{
    NSArray *contactList = [self.contact allValues];
    NSLog(@"所有人信息如下:");
    NSLog(@"%@",contactList);

    //[self saveInfo];
    [self start];

}

- (void)exit
{
    NSLog(@"系统退出中。。");
    exit(0);

}

- (void)saveInfo
{
//    NSKeyedArchiver *archiver = [NSKeyedArchiver alloc]initForWritingWithMutableData;
    //每个人作为字典存,对象取
    //所有人的信息保存到一个数组中,然后存在本地
     NSArray *allContacters = [self.contact allValues];

    NSMutableArray *info = [NSMutableArray array];

    for (Contacter *acontacter in allContacters) { //遍历联系人对象,一次转为字典存放
        NSDictionary *dic = @{@"name":acontacter.name,@"phone":acontacter.phone};
        [info addObject:dic];
    }
    NSString *path = @"/Users/rimi/Desktop/1.doc";
    [info writeToFile:path atomically:YES];
    NSLog(@"%@",info);
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值