OC中级通讯录

做一个班级信息程序,包含4个自定义的类:OurClass、Teacher、Student、
Person。
OurClass代表班级,⾥⾯包含一个Teacher对象,和一个NSMutableArray(用于存放多个Student对象)班级能够更换老师,能够增添学生和移除学生。Person类作为Student、Teacher类的父类,定义公共属性和⽅法。Student包含一个成绩属性(NSMutableDictionary类型),能存储课程名称和成绩,Student类还能比较年龄⼤小。Teacher类有一个exam:⽅法(参数是课程名称,内部是每个学生设置考试成绩,成绩随机)。

//———————————通讯录——————————
// MyAddressBook.h
// 中级通讯录
//
// Created by lanou on 15/10/31.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
@interface MyAddressBook : NSObject
// 要有一个存储所有信息的字典 字典key数组存放26个英文字母 value存放名字开头字母和这个字母相同的联系人数组
@property(retain,nonatomic)NSMutableDictionary *myDic; // 存储的字典

- (instancetype)init;

// 添加一个联系人
- (void)addContact:(Contact *)contact;

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter;

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum;

//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc;

//根据姓名删除
- (void)removeContactByName:(NSString *)name;

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter;

//根据分组显示
- (void)showAllContactByFirstLetter;
@end

//——————————————
// MyAddressBook.m
// 中级通讯录
//
// Created by lanou on 15/10/31.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "MyAddressBook.h"

@implementation MyAddressBook
- (instancetype)init{
self = [super init];
if (self){

//存储联系人的字典初始化 共有26个键值对
_myDic = [NSMutableDictionary dictionary];
for (int i = 0; i < 26; i++){
[_myDic setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%c",'A'+i] ];
}
}
return self;
}

//判断是否合法联系人
- (BOOL)isRightContact:(Contact *)contact{
if (contact.name.length == 0||contact.phoneNum.length == 0){
return NO;
}
return YES;
}
// 添加一个联系人
- (void)addContact:(Contact *)contact{
if ([self isRightContact:contact]){
//截取联系人名字首字母 并大写
NSString *firstLetter = [contact.name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
//判断加入哪个分组
for (NSString *obj in [_myDic allKeys]) {
if ([obj isEqualToString:firstLetter])
[_myDic[obj] addObject:contact];
}
}else{
NSLog(@"添加的联系人信息不全,添加失败!");
}
}

//showArray字符化
- (NSString *)showArrayDescription:(NSMutableArray *)showArray{
NSString *str = @"";
for (int i = 0; i < showArray.count; i++) {
Contact *c = showArray[i];
str = [str stringByAppendingString:[NSString stringWithFormat:@"\n %@",c]];
}
str = [str stringByAppendingString:@""];
return str;
}

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;//首个字母 大写形式
NSMutableArray *showArray = _myDic[firstLetter];
NSMutableString *str = [NSMutableString stringWithString:@""];
if (showArray.count != 0){
[str appendString: [NSMutableString stringWithString:firstLetter]];
[str appendString:[self showArrayDescription:showArray]];
[str appendString:@"\n"];
}
return str;
}

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum{
for (NSString *obj in [_myDic allKeys]) {
NSMutableArray *arr = _myDic[obj];
for (int i = 0; i < arr.count; i++){
if ([[arr[i] phoneNum] isEqualToString:phoneNum])
return [arr[i] description];
}
}
return @"";
}

//根据姓名删除
- (void)removeContactByName:(NSString *)name{
//汉字转拼音
NSMutableString *name2 = [NSMutableString stringWithString:name];
CFStringTransform((CFMutableStringRef)name2, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name2, NULL, kCFStringTransformStripDiacritics, false);
NSString *name3 = [name2 stringByReplacingOccurrencesOfString:@" " withString:@""];
//从字典删除
NSString *firstLetter = [name3 substringWithRange:NSMakeRange(0, 1)].uppercaseString;
NSMutableArray *array = _myDic[firstLetter];
for (int i = 0; i < array.count; i++ ){
if ([[array[i] name] isEqualToString:name3]){
[array removeObject:array[i]];
i = -1; //为了删除更彻底 让i归0
}
}
}

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;
_myDic[firstLetter] = [NSMutableArray array];
}



//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc{
NSMutableArray *res = [NSMutableArray array];
//遍历字典及其value 找出所有的女性
for (NSString *obj in [_myDic allKeys]) {
NSArray *arr = _myDic[obj];
for (int i = 0; i < arr.count; i++){
if ([[arr[i] sex] isEqualToString:@"nu"]){
[res addObject:arr[i]];
}
}
}
//对结果数组冒泡排序
for (int i = 0; i < res.count - 1; i++){
for (int j = 0; j < res.count - 1 - i; j++){
if ([[res[j] age] intValue] < [[res[j+1] age] intValue] ){
Contact *c1 = [Contact contactWithContact:res[j]];
Contact *c2 = [Contact contactWithContact:res[j+1]];
[res replaceObjectAtIndex:j withObject:c2];
[res replaceObjectAtIndex:j+1 withObject:c1];
}
}
}
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (Contact *obj in res) {
[str appendString:[obj description] ];
[str appendString:@"\n"];
}
return str;
}


//根据分组显示
- (void)showAllContactByFirstLetter{
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < 26; i++){
NSString *sss = [NSString stringWithFormat:@"%c",'A'+i];
[str appendString: [self showContactByFirstLetter:sss]];
}
NSLog(@"%@",str);
}
@end
//---------------------
// AddressBook.h
// 中级通讯录
//
// Created by lanou on 15/10/30.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
@interface AddressBook : NSObject
// 要有一个存储所有信息的字典 字典key数组存放26个英文字母 value存放名字开头字母和这个字母相同的联系人数组
@property(retain,nonatomic)NSMutableDictionary *myDic; // 存储的字典
@property(retain,nonatomic)NSMutableDictionary *showDic; // 展示的字典
@property(retain,nonatomic)NSMutableArray *myArray; //所有的分组集合
@property(retain,nonatomic)NSMutableArray *showArray; //显示结果



- (instancetype)init;
- (NSString *)description;

// 添加一个联系人
- (void)addContact:(Contact *)contact;

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter;

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum;

//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc;

//根据姓名删除
- (void)removeContactByName:(NSString *)name;

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter;

//根据分组显示
- (void)showAllContactByFirstLetter;
@end

//----------------------
// AddressBook.m
// 中级通讯录
//
// Created by lanou on 15/10/30.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "AddressBook.h"

@implementation AddressBook
- (instancetype)init{
self = [super init];
if (self){

//存储联系人的字典初始化 共有26个键值对
_myDic = [NSMutableDictionary dictionary];
for (int i = 0; i < 26; i++){
[_myDic setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%c",'A'+i] ];
}
//用于显示的字典
_showDic = [NSMutableDictionary dictionary];
//便于线性查找
_myArray = [NSMutableArray array];
}
return self;
}

- (NSString *)description{
return [super description];
}

//判断是否合法联系人
- (BOOL)isRightContact:(Contact *)contact{
if (contact.name.length == 0||contact.phoneNum.length == 0){
return NO;
}
return YES;
}
// 添加一个联系人
- (void)addContact:(Contact *)contact{
if ([self isRightContact:contact]){
[_myArray addObject:contact];
//截取联系人名字首字母 并大写
NSString *firstLetter = [contact.name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
// NSLog(@"添加联系人第一个字母:%@",firstLetter);
//判断加入哪个分组
for (NSString *obj in [_myDic allKeys]) {
if ([obj isEqualToString:firstLetter])
// NSLog(@"%@",obj);
[_myDic[obj] addObject:contact];
}
}else{
NSLog(@"添加的联系人信息不全,添加失败!");
}

}
//showArray字符化
- (NSString *)showArrayDescription{
NSString *str = @"";
for (int i = 0; i < _showArray.count; i++) {
Contact *c = _showArray[i];
str = [str stringByAppendingString:[NSString stringWithFormat:@"\n %@",c]];
}
str = [str stringByAppendingString:@""];
return str;
}



//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter{
_showDic = [NSMutableDictionary dictionary];//注意.一定要清空 否则结果还是原来的
firstLetter = firstLetter.uppercaseString;
[_showDic setObject:_myDic[firstLetter] forKey:firstLetter];
NSMutableString *str = [NSMutableString stringWithString:firstLetter];
[str appendString:@"\n"];
NSMutableArray *arrContact = [_showDic allValues][0];
NSMutableArray *arrNames = [NSMutableArray array];
for (int i = 0; i < arrContact.count; i++){
[arrNames addObject: [arrContact[i] name]];
}
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:arrContact forKeys:arrNames];
NSArray *arr = [arrNames sortedArrayUsingSelector:@selector(compare:)];
// NSLog(@"%@",arrNames);
for (NSString* obj in arr) {
[str appendString:[dic[obj] description]];
[str appendString:@"\n"];
}
if (arr.count != 0){
return str;}
return @"";
}


//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum{
[_showArray removeAllObjects];
_showArray = [NSMutableArray array];//注意:一定要这一步
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.phoneNum isEqualToString:phoneNum]){
[_showArray addObject:c];
}

}
return [self showArrayDescription];
}


//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc{
[_showArray removeAllObjects];
_showArray = [NSMutableArray array];//注意:一定要这一步
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.sex isEqualToString:@"f"]){
[_showArray addObject:c];
}
}
NSMutableArray *res = [NSMutableArray arrayWithArray:_showArray];
for (int i = 0; i < res.count - 1; i++){
for (int j = 0; j < res.count - 1 - i; j++){
if ([[res[j] age] intValue] < [[res[j+1] age] intValue] ){
Contact *c1 = [Contact contactWithContact:res[j]];
Contact *c2 = [Contact contactWithContact:res[j+1]];
[res replaceObjectAtIndex:j withObject:c2];
[res replaceObjectAtIndex:j+1 withObject:c1];
}
}
}
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (Contact *obj in res) {
[str appendString:[obj description] ];
[str appendString:@"\n"];
}
return str;
}



//根据姓名删除
- (void)removeContactByName:(NSString *)name{
//从线性表删除
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.name isEqualToString:name]){
[_myArray removeObject:c];
}
}
//从字典删除
NSString *firstLetter = [name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
NSMutableArray *array = _myDic[firstLetter];
for (int i = 0; i < array.count; i++ ){
if ([[array[i] name] isEqualToString:name]){
[array removeObject:array[i]];
}
}
}

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;
_myDic[firstLetter] = [NSMutableArray array];
}

//根据分组显示
- (void)showAllContactByFirstLetter{
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < 26; i++){
NSString *sss = [NSString stringWithFormat:@"%c",'A'+i];
[str appendString: [self showContactByFirstLetter:sss]];
}
NSLog(@"%@",str);
}
@end
//---------------
// Contact.h
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>

// 联系人

@interface Contact : NSObject
@property(retain,nonatomic)NSString *name; //英文的姓名
@property(retain,nonatomic)NSString *nameC; //接受的中文名字
@property(retain,nonatomic)NSString *sex; //性别
@property(retain,nonatomic)NSString *sexC; //性别 中文
@property(retain,nonatomic)NSString *phoneNum; //电话号码
@property(retain,nonatomic)NSString *address; //住址
@property(retain,nonatomic)NSString *addressC;
@property(retain,nonatomic)NSString *arrayName; //分组名称
@property(retain,nonatomic)NSString *arrayNameC;
@property(retain,nonatomic)NSString* age; //年龄
- (instancetype)init;
+ (instancetype)contactWithContact:(Contact *)contact;

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum;

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age;


- (NSString *)description;
@end
//-------------
// Contact.m
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "Contact.h"
@implementation Contact
//将中文转换成拼音
- (NSString *)ChineseToEglish:(NSString *)str{
NSMutableString *name = [NSMutableString stringWithString:str];
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformStripDiacritics, false);
NSString *newstr = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
return newstr;
}
- (instancetype)init{
self = [super self];
if (self){
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
_age = @"1";
}
return self;
}
+ (instancetype)contactWithContact:(Contact *)contact{
Contact *p = [[Contact alloc]init];
p.name = contact.name;
p.sex = contact.sex;
p.phoneNum = contact.phoneNum;
p.address = contact.address;
p.arrayName = contact.arrayName;
p.age = contact.age;

p.nameC = contact.nameC;
p.sexC = contact.sexC;
p.addressC = contact.addressC;
p.arrayNameC = contact.arrayNameC;

return p;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum{
self = [super init];
if (self){
_nameC = name;
_phoneNum = phoneNum;
_name = [self ChineseToEglish:_nameC];
_sexC = @"不明";
_addressC = @"不明";
_age = @"不明";
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
}
return self;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age{
self = [super init];
if (self){
_nameC = name;
_sexC = sex;
_addressC = address;
_arrayNameC = arrayName;
_age = age;
_name = [self ChineseToEglish:_nameC];
_phoneNum = phoneNum;
_sex = [self ChineseToEglish:_sexC];
_address = [self ChineseToEglish:_addressC];
_arrayName = [self ChineseToEglish:_addressC];
}
return self;
}


- (NSString *)description{
NSString *str = [NSString stringWithFormat:@"姓名:%@ 性别: %@ 年龄: %@ 号码: %@ 住址: %@ 分组: %@ ",_nameC,_sexC,_age,_phoneNum,_addressC,_arrayNameC];
return str;

}
@end
//—————————————main.m——————————
// Contact.m
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "Contact.h"
@implementation Contact
//将中文转换成拼音
- (NSString *)ChineseToEglish:(NSString *)str{
NSMutableString *name = [NSMutableString stringWithString:str];
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformStripDiacritics, false);
NSString *newstr = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
return newstr;
}
- (instancetype)init{
self = [super self];
if (self){
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
_age = @"1";
}
return self;
}
+ (instancetype)contactWithContact:(Contact *)contact{
Contact *p = [[Contact alloc]init];
p.name = contact.name;
p.sex = contact.sex;
p.phoneNum = contact.phoneNum;
p.address = contact.address;
p.arrayName = contact.arrayName;
p.age = contact.age;

p.nameC = contact.nameC;
p.sexC = contact.sexC;
p.addressC = contact.addressC;
p.arrayNameC = contact.arrayNameC;

return p;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum{
self = [super init];
if (self){
_nameC = name;
_phoneNum = phoneNum;
_name = [self ChineseToEglish:_nameC];
_sexC = @"不明";
_addressC = @"不明";
_age = @"不明";
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
}
return self;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age{
self = [super init];
if (self){
_nameC = name;
_sexC = sex;
_addressC = address;
_arrayNameC = arrayName;
_age = age;
_name = [self ChineseToEglish:_nameC];
_phoneNum = phoneNum;
_sex = [self ChineseToEglish:_sexC];
_address = [self ChineseToEglish:_addressC];
_arrayName = [self ChineseToEglish:_addressC];
}
return self;
}


- (NSString *)description{
NSString *str = [NSString stringWithFormat:@"姓名:%@ 性别: %@ 年龄: %@ 号码: %@ 住址: %@ 分组: %@ ",_nameC,_sexC,_age,_phoneNum,_addressC,_arrayNameC];
return str;

}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值