Contact.h
#import <Foundation/Foundation.h>
@interface Contact:nsobject
{
NSString *_name;
NSString *_gender;
Nsinteger _age;
NSString *_address;
NSString *_phonenumber;
NSString *_group;
}
-(void) SetName: (NSString *) name;
-(NSString *) name;
-(void) Setgender: (NSString *) gender;
-(NSString *) gender;
-(void) Setage: (Nsinteger) age;
-(Nsinteger) age;
-(void) Setaddress: (NSString *) address;
-(NSString *) address;
-(void) Setphonenumber: (NSString *) PhoneNumber;
-(NSString *) PhoneNumber;
-(void) Setgroup: (NSString *) group;
-(NSString *) group;
-(Instancetype) Initwithname: (NSString *) name Gender: (NSString *) Gender Age: (Nsinteger) Address of age: (NSString *) Address PhoneNumber: (NSString *) PhoneNumber;
-(void) introduce;
-(NSString *) description;
-(Nscomparisonresult) Comparebyname: (contact *) Contact;
-(Nscomparisonresult) Comparebyage: (contact *) Contact;
@end
Contact.m
#import "Contact.h"
@implementation Contact
-(void) SetName: (NSString *) name {
_name = name;
}
-(NSString *) name {
return _name;
}
-(void) Setgender: (NSString *) Gender {
_gender = gender;
}
-(NSString *) Gender {
return _gender;
}
-(void) Setage: (Nsinteger) Age {
_age = age;
}
-(Nsinteger) Age {
return _age;
}
-(void) Setaddress: (NSString *) address {
_address = address;
}
-(NSString *) address {
return _address;
}
-(void) Setphonenumber: (NSString *) PhoneNumber {
_PhoneNumber = PhoneNumber;
}
-(NSString *) PhoneNumber {
return _phonenumber;
}
-(void) Setgroup: (NSString *) Group {
_group = Group;
}
-(NSString *) Group {
return _group;
}
-(Instancetype) Initwithname: (NSString *) name Gender: (NSString *) Gender Age: (Nsinteger) Address of age: (NSString *) Address PhoneNumber: (NSString *) PhoneNumber {
self = [super init];
if (self) {
[Self setname:name];
[Self setgender:gender];
[Self setage:age];
[Self setaddress:address];
[Self setphonenumber:phonenumber];
_group = [_name substringtoindex:1];
}
return self;
}
-(void) Introduce {
NSLog (@ "%@%@%ld%@%@%@", _name, _gender, _age, _address, _PhoneNumber, _group);
}
-(NSString *) Description {
return [[NSString alloc] initwithformat:@ "%@%@%ld%@%@%@", _name, _gender, _age, _address, _PhoneNumber, _group];
}
-(Nscomparisonresult) Comparebyname: (Contact *) Contact {
NSString *selfname = [self name];
NSString *contactname = [contact name];
Nscomparisonresult result = [Selfname compare:contactname];
return result;
}
-(Nscomparisonresult) Comparebyage: (Contact *) Contact {
Nsinteger selfage = [self-age];
Nsinteger contactage = [contact Age];
if (Selfage > Contactage) {
return nsorderedascending;
}
else if (Selfage < contactage) {
return nsordereddescending;
}
Else
{
return nsorderedsame;
}
}
@end
ContactManger.h
#import <Foundation/Foundation.h>
#import "Contact.h"
@interface Contactmanager:nsobject
{
NSString *_name;
Nsmutabledictionary *_contactlist;
}
-(void) SetName: (NSString *) name;
-(NSString *) name;
-(Instancetype) Initwithname: (NSString *) name;
Add a Contact
-(void) Addnewcontact: (contact *) Contact;
Get all the contacts under the group and sort by name in ascending order
-(Nsarray *) Getallcontactsbygroupname: (NSString *) groupName;
Search for corresponding contacts by number
-(Contact *) Findcontactbyphonenumber: (NSString *) PhoneNumber;
Get all female contacts in descending order of age
-(Nsarray *) Getallfemalcontactsbygender: (NSString *) gender;
Delete a contact by name
-(void) Removecontactbyname: (NSString *) name;
Delete all contacts under a group
-(void) Removeallcontactsbygroupname: (NSString *) groupName;
Show All Contacts
-(void) showallcontacts;
@end
Contactmanger.m
#import "ContactManager.h"
@implementation Contactmanager
-(void) SetName: (NSString *) name {
_name = name;
}
-(NSString *) name {
return _name;
}
-(Instancetype) Initwithname: (NSString *) name {
self = [super init];
if (self) {
[Self setname:name];
_contactlist = [[Nsmutabledictionary alloc] initwithcapacity:0];
}
return self;
}
Add a Contact
-(void) Addnewcontact: (Contact *) Contact {
if (nil = = [contact Name] | | nil = = [Contact PhoneNumber]) {
NSLog (@ "Are you out of your mind?");
} else {
Nsmutablearray *array = [_contactlist objectforkey:[contact Group]];
if (nil = = array) {
To initialize an array
Array = [[Nsmutablearray alloc] initwithcapacity:0];
[Array addobject:contact];
[_contactlist Setobject:array forkey:[contact Group];
}
else {
[Array addobject:contact];
}
}
}
Get all the contacts under the group and sort by name in ascending order
-(Nsarray *) Getallcontactsbygroupname: (NSString *) GroupName {
Nsarray *contacts = [_contactlist objectforkey:groupname];
Nsarray *newarray = [Contacts sortedarrayusingselector: @selector (comparebyname:)];
return newarray;
}
Search for corresponding contacts by number
-(Contact *) Findcontactbyphonenumber: (NSString *) PhoneNumber {
For (NSString *key in _contactlist) {
Nsmutablearray *array = [_contactlist Objectforkey:key];
For (contact *contact in array) {
if ([PhoneNumber isequaltostring:[contact PhoneNumber]]) {
return contact;
}
}
}
return nil;
}
Get all female contacts in descending order of age
-(Nsarray *) Getallfemalcontactsbygender: (NSString *) Gender {
Nsmutablearray *femalarray = [[Nsmutablearray alloc] initwithcapacity:0];
For (NSString *key in _contactlist) {
Nsmutablearray *array = [_contactlist Objectforkey:key];
For (contact *contact in array) {
if ([Gender isequaltostring:[contact Gender]]) {
[Femalarray Addobject:contact];
}
}
}
[Femalarray sortusingselector: @selector (comparebyage:)];
return femalarray;
}
Delete a contact by name
-(void) Removecontactbyname: (NSString *) name {
Contact *c;
NSString *newkey;
For (NSString *key in _contactlist) {
Nsmutablearray *array = [_contactlist Objectforkey:key];
For (contact *contact in array) {
if ([name Isequaltostring:[contact name]]) {
[Array removeobject:contact];
Return
c = Contact;
NewKey = key;
}
}
}
[[_contactlist Objectforkey:newkey] removeobject:c];
}
Delete all contacts under a group
-(void) Removeallcontactsbygroupname: (NSString *) GroupName {
[_contactlist Removeobjectforkey:groupname];
}
Show All Contacts
-(void) Showallcontacts {
Nsarray *allkeys = [_contactlist AllKeys];
Nsarray *newarray = [AllKeys sortedarrayusingselector: @selector (compare:)];
For (NSString *key in NewArray) {
Nsmutablearray *array = [_contactlist Objectforkey:key];
NSLog (@ "-----%@-----", key);
For (contact *contact in array) {
[Contact introduce];
}
}
}
@end
Main.m
#import <Foundation/Foundation.h>
#import "ContactManager.h"
int main (int argc, const char * argv[]) {
Contactmanager *cm = [[Contactmanager alloc] initwithname:@ "SHS150913 Address Book (medium difficulty)"];
Contact *C1 = [[Contact alloc] initwithname:@ "Wangzhiwen" gender:@ "male" age:18 address:@ "Li de" phonenumber:@ "15156056193"];
Contact *C2 = [[Contact alloc] initwithname:@ "xumin" gender:@ "female" age:19 address:@ "Lai Tak Hotel" phonenumber:@ "15951758095"];
Contact *C3 = [[Contact alloc] initwithname:@ "Zhanglu" gender:@ "female" age:20 address:@ "Lai Tak Hotel" phonenumber:@ "15951758639"];
Contact *C4 = [[Contact alloc] initwithname:@ "Xuzizhong" gender:@ "male" age:21 address:@ "Li de" phonenumber:@ "18862162030"];
Contact *C5 = [[Contact alloc] initwithname:@ "Wangtao" gender:@ "male" age:22 address:@ "Li de" phonenumber:@ "18329032011"];
Contact *C6 = [[Contact alloc] initwithname:@ "qiuse" gender:@ "male" age:23 address:@ "Li de" phonenumber:@ "18321978135"];
Contact *C7 = [[Contact alloc] initwithname:@ "JuJu" gender:@ "female" age:24 address:@ "Lai Tak" phonenumber:@ "15156697015"];
[cm ADDNEWCONTACT:C1];
[cm ADDNEWCONTACT:C2];
[cm addnewcontact:c3];
[cm ADDNEWCONTACT:C4];
[cm addnewcontact:c5];
[cm addnewcontact:c6];
[cm Addnewcontact:c7];
[cm showallcontacts];
NSLog (@ "-----gorgeous split-line-----");
Nsarray *array = [cm getallcontactsbygroupname:@ "w"];
NSLog (@ "%@", array);
NSLog (@ "-----gorgeous split-line-----");
Contact *resultcontact = [cm findcontactbyphonenumber:@ "15156697015"];
NSLog (@ "%@", resultcontact);
NSLog (@ "-----gorgeous split-line-----");
Nsarray *femalarray = [cm getallfemalcontactsbygender:@ "female"];
NSLog (@ "%@", Femalarray);
NSLog (@ "-----gorgeous split-line-----");
[cm removecontactbyname:@ "juju"];
[cm showallcontacts];
NSLog (@ "-----gorgeous split-line-----");
[cm removeallcontactsbygroupname:@ "w"];
[cm showallcontacts];
return 0;
}
OC Contacts Medium Difficulty