@fiy-fish
2015-07-18T19:16:29.000000Z
字数 3285
阅读 1555
Objective-c
// day06-02-电话本
//
// Created by Aaron on 15/7/8.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PhoneBook.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
PhoneBook *book = [[PhoneBook alloc] init];
for(int i = 0; i < 5; i++)
{
if(i%2)
{
Person *p = [[Person alloc] initWithName:@"laowang" withAddr:@"..." withCompany:@"company" withPhone:@"110"];
[book addPerson:p];
}
else
{
Person *p = [[Person alloc] initWithName:@"gebi" withAddr:@"..." withCompany:@"company" withPhone:@"110"];
[book addPerson:p];
}
}
[book showInfo];
//NSLog(@"%@",[book searchByName:@"gebi"]);
}
return 0;
}
#import <Foundation/Foundation.h>
/*
设计一个电话本,人物信息包含姓名,地址,电话,公司
需要实现功能:按姓名自动排序
指定姓名查找
指定公司查找
人物可重名
显示所有人物信息
//姓名模糊查询
*/
@interface Person : NSObject
{
NSString *_name;
NSString *_addr;
NSString *_company;
NSString *_phone;
}
-(instancetype)initWithName:(NSString *)name withAddr:(NSString *)addr withCompany:(NSString *)company withPhone:(NSString *)phone;
-(NSString *)name;
-(NSString *)addr;
-(NSString *)company;
-(NSString *)phone;
@end
#import "Person.h"
@implementation Person
-(instancetype)initWithName:(NSString *)name withAddr:(NSString *)addr withCompany:(NSString *)company withPhone:(NSString *)phone
{
if(self = [super init])
{
_name = name;
_addr = addr;
_company = company;
_phone = phone;
}
return self;
}
-(NSString *)name
{
return _name;
}
-(NSString *)addr
{
return _addr;
}
-(NSString *)company
{
return _company;
}
-(NSString *)phone
{
return _phone;
}
@end
/*
设计一个电话本,人物信息包含姓名,地址,电话,公司
需要实现功能:按姓名自动排序
指定姓名查找
指定公司查找
人物可重名
显示所有人物信息
//姓名模糊查询
*/
#import "Person.h"
@interface PhoneBook : NSObject
{
//姓名和id关联
NSMutableDictionary *_nameAndIdDic;
//人和id关联
NSMutableDictionary *_personAndIdDic;
//公司和id关联
NSMutableDictionary *_companyAndIdDic;
//记录最大的id
NSInteger _maxId;
//添加一个数组
NSMutableArray *_personArray;
}
//添加人
-(void)addPerson:(Person *)p;
//按姓名查找
-(NSArray *)searchByName:(NSString *)name;
//按公司查找
-(NSArray *)searchByCompany:(NSString *)company;
//测试
-(void)showInfo;
@end
#import "PhoneBook.h"
@implementation PhoneBook
-(instancetype)init
{
if(self = [super init])
{
_nameAndIdDic = [NSMutableDictionary dictionary];
_personAndIdDic = [NSMutableDictionary dictionary];
_companyAndIdDic = [NSMutableDictionary dictionary];
_personArray = [NSMutableArray array];
_maxId = 0;
}
return self;
}
-(void)addPerson:(Person *)p
{
//计算ID
NSNumber *currentID = [NSNumber numberWithInteger:_maxId++];
//将名字和id关联
[_nameAndIdDic setObject:[p name] forKey:currentID];
//将人和id关联
[_personAndIdDic setObject:p forKey:currentID];
//将公司和id关联
[_companyAndIdDic setObject:[p company] forKey:currentID];
//将人添加到数组
[self insertPerson:p];
}
-(void)insertPerson:(Person *)p
{
if([_personArray count] == 0)
{
[_personArray addObject:p];
}
else
{
for(int i = 0; i < [_personArray count]; i++)
{
Person *obj1 = _personArray[i];
if([[p name] compare:[obj1 name]] == NSOrderedAscending)
{
[_personArray insertObject:p atIndex:i];
break;
}
if(i == [_personArray count]-1)
{
[_personArray addObject:p];
break;
}
}
}
}
-(NSArray *)searchByCompany:(NSString *)company
{
return [self searchByString:company inDic:_companyAndIdDic];
}
-(NSArray *)searchByName:(NSString *)name
{
return [self searchByString:name inDic:_nameAndIdDic];
}
-(NSArray *)searchByString:(NSString *)str inDic:(NSDictionary *)dic
{
NSMutableArray *array = [NSMutableArray array];
//找名字对应的所有id
NSArray *allIds = [dic allKeysForObject:str];
for(id obj in allIds)
{
Person *p = [_personAndIdDic objectForKey:obj];
[array addObject:p];
}
return array;
}
-(void)showInfo
{
for(Person *obj in _personArray)
{
//value = 字典[key]
NSLog(@"%@",[obj name]);
}
}
@end