OBJECTC----Implement a simple address book (Add and revise)

Source: Internet
Author: User

Created by Guo Chai April 09, 2015 21:27:50

After two days of the downturn, the state of the slowly return, life will continue, life still need to struggle!

Wish: Good Life peace!!!

========================================================================

Title Description:

1. Create the AddressBook class. 1) to use a dictionary as a container, the key value of the dictionary is the group name (name, first word, or uppercase), and the value is an array.

To store contacts (person instances). (5 points) 2) Add contact person. requirements (20 points)
Cannot have duplicate names).
A. Determine the name and the phone cannot be empty otherwise add failed. B. Determine if there is already a grouping, there is direct storage. There is no storage after creating a group. (Note: Contact person
C. Add successful return Yes, name is empty or there is a duplicate of the addition failed to return No. D. The data is stored with person.
E.? Method:-(BOOL) Addperson: (person *) Aperson;
3) Delete contact by name. (15 points)
A.? Methods:-(BOOL) Deletepersonwithname: (nsstring*) Apersonname; B. Delete successful return yes, otherwise return no; C. When you delete a contact and the group is empty, you need to delete the group.
4) Delete? A group. (10 points)
A:? Method:-(BOOL) DeleteGroup: (NSString *) agroupname; B. Delete successful return yes, otherwise return no;
5) Find a contact by phone? (10 points)
A:? method:-(Person *) Findpersonwithphonenum: (NSString *) Aphonenum; B: If found to return the contact person, otherwise return nil. C. Ignore the phone duplication problem and return only the first one found contact?
6) Find contact according to sex? People, and will find the contact? People in ascending order of age (15 points) A:? Method:-(Nsarray *) Findpersonswithsex: (NSString *) asex; B: Find contact? People put into the array, sort the time to make the block.
7) Change the contact information according to the name of the person. (10 points) A:? Method:-(BOOL) Changepersonwithname: (NSString *) name Phonenum:
(NSString *) anum Sex: (NSString *) Asex Age: (Nsuinteger) AAge; B: Find the contact person and modify the successful return Yes, otherwise return no
C. Modify phone, gender, age, parameters cannot be empty.
8) Show all contact information in the Address Book (5 points) A:? Method:-(void) ShowAll;
B. By rewriting the description method show contact person information.
2. Use the person class. (attribute: Name, age, gender, phone, Group) 1) group is the name of the first word? The NSString class?? The method obtained in the project. 2) Rewrite description? method, Output "name: Zhang San Gender: Male Age: 22 Telephone:
123456789 ". (5 points) 3) Define a convenience constructor for the person class? Method contains parameters: Name, age, gender, telephone. (5 points)
Implementation in the 3.main function 1). Create an instance of a AddressBook class (a communication record).
2). Create an instance of the four person class with the convenience builder (contact people). Name: Lucy sex:? Female phone: 123 Age: 21
Name: Joe Sex: Male Phone: 456 Age: 17
Name: Baby sex:? Female phone: 789 Age: 27

Name: Linda sex:? Female Phone: 000 Age: 21 3). To add a link to a person? method, add four contact people to your address book. 4). Follow the phone to find the contact person and print (Find phone: 456); 5). Find contact According to sex? People, and will find contact? People are sorted in ascending order of age and printed (find gender:? female) 6). Change the contact information by name. (Modify Linda's information: Gender:? Female phone: 012 Age: 25); 7). Delete Contacts by name, and show the information of all the people in the communication record. (delete contact? Person: Baby) 8). Delete a group and show the information of all the people in the communication record. (Delete group L)

=========================================================================

AddressBook.h:

<span style= "FONT-SIZE:18PX;" >-(BOOL) Addperson: (Person *) aperson;-(BOOL) Isgroupexist: (NSString *) groupname;-(person *) selectpersonwithname :(NSString *) name;-(BOOL) Deletepersonwithname: (NSString *) apersonname;-(BOOL) DeleteGroup: (NSString *) agroupname;- (Person *) Findpersonwithphonenum: (NSString *) aphonenum;-(Nsarray *) Findpersonswithsex: (NSString *) asex;-(BOOL) Changepersonwithname: (NSString *) name Phone: (NSString *) anum Sex: (NSString *) Asex Age: (Nsuinteger) aage;-(void) Showall;</span>
ADDRESSBOOK.M:

<span style= "color: #333333;" ><span style= "FONT-SIZE:18PX;" >//according to the name to find the corresponding person </span><span style= "font-size:18px;"    >-(Person *) Selectpersonwithname: (NSString *) name{NSString * Grup = [name Uppercasepinyinfirstletter];    BOOL isgupexist = [self Isgroupexist:grup];    if (!isgupexist) {return nil;        } else{Nsmutablearray * arr = [_dic Objectforkey:grup];            for (person * per in arr) {if ([Per.name Isequaltostring:name]) {return per; }}} return nil;} Determine if there is a guoupname group-(BOOL) Isgroupexist: (NSString *) groupname{for (NSString * key in _dic) {if ([key Isequalto        String:groupname]) {return YES; }} return NO;    Add contact-(BOOL) Addperson: (person *) aperson{person * per = [self selectPersonWithName:aPerson.name];        if (per) {NSLog (@ "The person is exist!");    return NO;    } BOOL isgroupext = [self isGroupExist:aPerson.group]; if (!isgrouPext) {Nsmutablearray *arr = [Nsmutablearray array];        [_dic Setobject:arr ForKey:aPerson.group];    [Arr Addobject:aperson];        } else{Nsmutablearray * arr = [_dic objectForKey:aPerson.group];    [Arr Addobject:aperson]; } return YES; Delete Contact by name-(BOOL) Deletepersonwithname: (NSString *) apersonname{person * per = [Self selectpersonwithname:apersonname    ];        if (per = = nil) {NSLog (@ "The person was not exist!");    return NO;    } nsmutablearray *arr = [_dic objectForKey:per.group];    [Arr Removeobject:per];    if (arr = = nil) {//[arr count]==0 [_dic RemoveObjectForKey:per.group]; } return YES;    Delete a group-(BOOL) DeleteGroup: (NSString *) agroupname{BOOL isgroubext = [self isgroupexist:agroupname];        if (!isgroubext) {NSLog (@ "The Group is not exist!");    return NO;    } [_dic Removeobjectforkey:agroupname]; return YES;} Find a contact based on the phone-(person *) Findpersonwithphonenum: (NSString *) aphonenum{for(NSString * key in _dic)        {Nsmutablearray * arr = [_dic Objectforkey:key];            for (person * per in arr) {if ([Per.phone Isequaltostring:aphonenum]) {return per; }}} return nil;} Find contacts based on gender and sort the found contacts by age-(Nsarray *) Findpersonswithsex: (NSString *) asex{Nsmutablearray * result = [Nsmutablearr    Ay array];        For (NSString *key in _dic) {Nsmutablearray *arr = [_dic Objectforkey:key];            For (person *per in arr) {if ([Per.sex isequaltostring:asex]) {[Result addobject:per]; }}} Nsarray * arr1 = [result Sortedarrayusingcomparator:^nscomparisonresult (person *obj1, person *obj2        {if (Obj1.age > Obj2.age) {return nsordereddescending;        } else if (obj1.age = = obj2.age) {return nsorderedsame;    } else return nsorderedascending;    }]; return arr1;} Change the practitioner information by name-(BOOL) ChangepersonwitHname: (NSString *) name Phone: (NSString *) anum Sex: (NSString *) Asex Age: (nsuinteger) aage{person *per = [self selectpers    Onwithname:name];        if (!per) {NSLog (@ "NO person!");    return NO;        } per.name = name;        Per.phone = Anum;        Per.sex = Asex;        Per.age = AAge; return YES;}        Show all contact information in Address Book-(void) showall{for (NSString *key in _dic) {Nsmutablearray * arr = [_dic Objectforkey:key];        For (person *per in arr) {NSLog (@ "%@", per);    }}}-(void) dealloc{[_dic release]; [Super Dealloc];} Separate spaces for Nsmutabledictiona types of dic </span></span>
<span style= "color: #333333;" ><span style= "FONT-SIZE:18PX;" >//can also assign nsmutabledictionary space to DIC separately in the main function, where it is no use to redefine-(instancetype) init{self    = [super init];    if (self) {        _dic = [[Nsmutabledictionary alloc]init];    }    return self;} </span><span style= "FONT-SIZE:18PX;" ></span></span>
a Uppercasepinyinfirstletter method is used to take out the initials of the NSString type string and convert it to uppercase, which is implemented in the classification of NSString (class).

my definition of classification is nsstring+firstcapitalletter, putting this class to use as a template , other works can be used directly in the examination of such items.

Implemented in Nsstring+firstcapitalletter.h

<span style= "FONT-SIZE:18PX;" > #import <Foundation/Foundation.h> #define alpha@ "abcdefghijklmnopqrstuvwxyz#" Char pinyinfirstletter ( unsigned short hanzi); @interface nsstring (Firstcapitalletter)//provide initial capital letters-(NSString *) uppercasepinyinfirstletter;-( NSString *) Lowercasepinyinfirstletter; @end </span>
NSSTRING+FIRSTCAPITALLETTER.M

#import "Nsstring+firstcapitalletter.h" #define Hanzi_start 19968#define hanzi_count 20902static Char Firstletterarray [Hanzi_count] = " YDKQSXNWZSSXJBYMGCCZQPSSQBYCDSCDQLDYLYBSSJGYQZJJFGCCLZZNWDWZJLJPFYYNNJJTMYNZWZHFLZPPQHGCCYYNMJQYXXGD ";
Char pinyinfirstletter (unsigned short hanzi) {int index = hanzi-hanzi_start;if (index >= 0 && index <= Hanz I_count) {return firstletterarray[index];} Else{return Hanzi;}} @implementation NSString (Firstcapitalletter)-(NSString *) uppercasepinyinfirstletter{    Unichar firstletter = [ Self characteratindex:0];    Char cfirstletter = Pinyinfirstletter (firstletter);    return [[NSString stringwithformat:@ "%c", Cfirstletter] uppercasestring];} -(NSString *) lowercasepinyinfirstletter{    Unichar firstletter = [self characteratindex:0];    Char cfirstletter = Pinyinfirstletter (firstletter);    return [[NSString stringwithformat:@ "%c", Cfirstletter] lowercasestring];} @end

Just use the template.

==============================================
Person.h

<span style= "FONT-SIZE:14PX;" > @property (nonatomic,copy) nsstring * name; @property (nonatomic,assign) Nsinteger age; @property (nonatomic,copy) NSString * sex, @property (nonatomic,copy) nsstring * phone, @property (nonatomic,copy) NSString * group;//person's Convenience Builder + ( Instancetype) Personwithname: (NSString *) name Age                           : (nsinteger) Age                           Sex: (NSString *) Sex                         Phone: (NSString * ) phone;-(Instancetype) Initwithname: (NSString *) name Age: (Nsinteger) Age Sex: (NSString *) Sex Phone: (NSString *) phone; </span>
person.m

-(void) SetName: (NSString *) name{    if (_name! = name) {        [_name release];        _name = [name copy];    }    Self.group = [name Uppercasepinyinfirstletter];}
There's been a lot of things lately, some sad
-(NSString *) description{    return [nsstring stringwithformat:@ "name=%@,age=%ld,sex=%@,phone=%@", _name,_age,_ Sex,_phone];} + (Instancetype) Personwithname: (NSString *) name Age                           : (nsinteger) Age                           Sex: (NSString *) Sex                         Phone: ( NSString *) phone{person    * per = [[Person alloc]initwithname:name age:age sex:sex Phone:phone];    return [per Autorelease];    } -(Instancetype) Initwithname: (NSString *) name Age: (Nsinteger) Age Sex: (NSString *) Sex Phone: (NSString *) phone{    if (self = [super init]) {        self.name  = name;        Self.age   = age;        Self.sex   = sex;        Self.phone = phone;        =================        self.group = [name Uppercasepinyinfirstletter];    }    return self;    }
=================================================================

This sentence can only assign a pointer space size to DIC in addressbook addressbook * addressbook = [[AddressBook alloc]init]; Separate the nsmutabledictionary type of dic allocation space//If you do not use the ADDRESSBOOK.M init method, you can replace it with this sentence addressbook.dic = [[Nsmutabledictionary           ALLOC] init];    Person * per1 = [person personwithname:@ "Lucy" age:21 sex:@ "female" phone:@ "123"];    Person * Per2 = [person personwithname:@ "Joe" age:17 sex:@ "male" phone:@ "456"];        Person * Per3 = [person personwithname:@ "Baby" age:27 sex:@ "female" phone:@ "789"];    Person * per4 = [person personwithname:@ "Linda" age:21 sex:@ "female" Phone:@ "000"];    ======================== [AddressBook Addperson:per1];    [AddressBook Addperson:per2];    [AddressBook Addperson:per3];    [AddressBook Addperson:per4];    [AddressBook ShowAll];    ======================== person * p = [addressbook findpersonwithphonenum:@ "456"];    if (!p) {NSLog (@ "null!");}    else{NSLog (@ "%@", p); }//======================== Nsarray * arr = [AddressBook Findpersonswithsex:@ "female"];    NSLog (@ "%@", arr);    ======================== [AddressBook changepersonwithname:@ "Linda" phone:@ "012" sex:@ "female" age:25];    NSLog (@ "%@", [AddressBook selectpersonwithname:@ "Linda"]);    ======================== [AddressBook deletepersonwithname:@ "Baby"];    [AddressBook showall];////======================== [addressbook deletegroup:@ "L"]; [AddressBook ShowAll]; [AddressBook ShowAll];
==================================================================

People live, not only for themselves, more is not to let those who care about you disappointed!!!!

-------Guo Chai
































OBJECTC----Implement a simple address book (Add and revise)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.