Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:09:32 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Ch 15 #2, #3 Solutions/ questions  (Read 1338 times)
dpieretti
Newbie
*
Posts: 11


Email
« on: September 08, 2011, 07:07:14 PM »


My attempts at exercises 15-2 and 15-3 are included below.

1.) I'm not certain I handled memory management correctly.    (Steve says that will not be a concern in the near future.)  Whew!

2.) Is it better to implement showMatchlist: and printMatchList: as methods or as functions?  Did it both ways.  Should matchList be an instance variable in AddressBook?

Thanks for any input!  -- Dennis.



AddressBook.h

Code: (Objective-C)
//
//  AddressBook.h
//  AddressCards
//
//  Created by Dennis Pieretti on 9/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

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

@interface AddressBook : NSObject
{
    NSString *bookName;
    NSMutableArray *book;
}

-(id) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(AddressCard *) lookup: (NSString *) theName;
-(void) showMatchList: (NSMutableArray *) theMatchList;
-(void) printMatchList: (NSMutableArray *) theMatchList;
-(NSMutableArray *) lookupAllMatchesFor: (NSString *) theName;

-(void) sort;

@end


AddressBook.m


Code: (Objective-C)
//
//  AddressBook.m
//  AddressCards
//
//  Created by Dennis Pieretti on 9/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AddressBook.h"
#import "AddressCard.h"

@implementation AddressBook

-(id) initWithName: (NSString *) name
{
    self = [super init];
    if (self)
    {
        bookName = [[NSString alloc] initWithString: name];
        book = [[NSMutableArray alloc] init];
    }
    return self;
}

-(id) init
{
    return [self initWithName:@"My AddressBook"];
}

-(void) addCard: (AddressCard *) theCard
{
    [book addObject:theCard];
}

-(void) removeCard: (AddressCard *) theCard
{
    [book removeObject:theCard];   // will use isEqual: method
}

-(int) entries
{
    return (int) [book count];
}

-(void) list
{
    NSLog(@"=========  Contents of: %@ =========", bookName);
   
    for(AddressCard *theCard in book)
        NSLog(@"%-20s %-32s", [theCard.name UTF8String], [theCard.email UTF8String]);
    NSLog(@"=======================================================");
   
}
/*
 // original lookup: method
-(AddressCard *) lookup: (NSString *) theName
{
    for (AddressCard *nextCard in book)
        if ([[nextCard name] caseInsensitiveCompare:theName] == NSOrderedSame)
            return nextCard;
   
    return nil;
}
*/

// Ch 15 Ex 2 lookup: method
-(AddressCard *) lookup: (NSString *) theName
{
    NSRange subRange;
       
   
    for (AddressCard *nextCard in book)
    {
        subRange = [[nextCard name] rangeOfString: theName options:NSCaseInsensitiveSearch];
       
        if (subRange.location != NSNotFound)
            return nextCard;
    }
    return nil;
}

// Ch 15 Ex 3 lookup: method
-(NSMutableArray *) lookupAllMatchesFor: (NSString *) theName
{
   NSMutableArray *matchList = [[[NSMutableArray alloc] init] autorelease];
    NSRange subRange;
   
    for (AddressCard *nextCard in book)
    {
        subRange = [[nextCard name] rangeOfString: theName options:NSCaseInsensitiveSearch];
   
        if (subRange.location != NSNotFound)
            [matchList addObject: nextCard];
    }
    if ([matchList count])
        return matchList;
    else
        return nil;
}

-(void) showMatchList: (NSMutableArray *) theMatchList
{
    NSLog(@"=========  Contents of All Match List  ==========");
       
    for(AddressCard *nextCard in theMatchList)
        NSLog(@"%-20s %-32s", [nextCard.name UTF8String], [nextCard.email UTF8String]);
    NSLog(@"=======================================================");
}

-(void) printMatchList: (NSMutableArray *) theMatchList
{
       
    for(AddressCard *nextCard in theMatchList)
        [nextCard print];
   
}

-(void) sort
{
    [book sortUsingSelector: @selector (compareNames:)];
}

-(void) dealloc
{
    [bookName release];
    [book release];
    [super dealloc];
}

@end


AddressBookTools.h

Code: (Objective-C)
//
//  AddressBookTools.h
//  AddressCards
//
//  Created by Dennis Pieretti on 9/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

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

void showMatchList(NSMutableArray * theMatchList);
void printMatchList(NSMutableArray * theMatchList);


AddressBookTools.m


Code: (Objective-C)
//
//  AddressBookTools.m
//  AddressCards
//
//  Created by Dennis Pieretti on 9/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AddressBookTools.h"



void showMatchList(NSMutableArray * theMatchList)
{
    NSLog(@"=========  Contents of All Match List  ==========");
   
    for(AddressCard *nextCard in theMatchList)
        NSLog(@"%-20s %-32s", [nextCard.name UTF8String], [nextCard.email UTF8String]);
        NSLog(@"=======================================================");
}

void printMatchList(NSMutableArray * theMatchList)
{
   
    for(AddressCard *nextCard in theMatchList)
        [nextCard print];
   
}



AddressCard.h


Code: (Objective-C)
//
//  AddressCard.h
//  AddressCards
//
//  Created by Dennis Pieretti on 9/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface AddressCard : NSObject
{
    NSString *name;
    NSString *email;
}

@property (copy, nonatomic) NSString *name, *email;

//-(void) setName: (NSString *) theName;
//-(void) setEmail: (NSString *) theEmail;

-(void) setName: (NSString *) theName andEmail:(NSString *) theEmail;


//-(NSString *) name;
//-(NSString *) email;

-(void) print;

-(NSComparisonResult) compareNames: (AddressCard *) element;
-(BOOL) isEqual:(AddressCard *) theCard;

@end


AddressCard.m


Code: (Objective-C)
//  AddressCard.m
//  AddressCards
//
//  Created by Dennis Pieretti on 9/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AddressCard.h"


@implementation AddressCard


@synthesize name, email;

-(void) setName: (NSString *) theName andEmail:(NSString *) theEmail
{
    self.name = theName;    // [self setName: theName];
    self.email = theEmail;
}

/*
-(void) setName: (NSString *) theName
{
    if( name != theName)
    {
        [name release];
        name = [[NSString alloc] initWithString:theName];
    }
}

-(void) setEmail: (NSString *) theEmail
{
    if( email != theEmail)
    {
        [email release];
        email = [[NSString alloc] initWithString:theEmail];
    }
}
 


-(NSString *) name
{
    return name;
}
-(NSString *) email
{
    return email;
}
*/

-(void) print
{
    NSLog(@"=====================================");
    NSLog(@"|                                   |");
    NSLog(@"|   %-32s|", [name UTF8String]);
    NSLog(@"|   %-32s|", [email UTF8String]);
    NSLog(@"|                                   |");
    NSLog(@"|                                   |");
    NSLog(@"|                                   |");
    NSLog(@"|                                   |");
    NSLog(@"|    o                     o        |");
    NSLog(@"=====================================");   
}

-(NSComparisonResult) compareNames: (AddressCard *) element
{
    return [name compare: element.name];
}

-(BOOL) isEqual:(AddressCard *) theCard
{
    if([name isEqualToString: theCard.name] == YES
       && [email isEqualToString:theCard.email] == YES)
        return YES;
    else
        return NO;
}

-(void) dealloc
{
    [name release];
    [email release];
    [super dealloc];
}
@end


main.m


Code: (Objective-C)
//
//  main.m
//  AddressCards
//
//  Created by Dennis Pieretti on 9/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AddressBook.h"
#import "AddressBookTools.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    NSString *aName = @"John Williams";
    NSString *aEmail = @"JWilliams@abc.com";
    NSString *bName = @"T Steve Smith";
    NSString *bEmail = @"TSSmith@xyz.com";
    NSString *cName = @"Mary Bea";
    NSString *cEmail = @"mbea@abc.com";
    NSString *dName = @"Julia & Steve Kean";
    NSString *dEmail = @"jewls@xyz.com";
   
    AddressCard *card1 = [[AddressCard alloc] init];
    AddressCard *card2 = [[AddressCard alloc] init];
    AddressCard *card3 = [[AddressCard alloc] init];
    AddressCard *card4 = [[AddressCard alloc] init];
    AddressCard *myCard;
   
    // set up Four addressCards
    [card1 setName: aName];
    [card1 setEmail: aEmail];
    [card2 setName: bName];
    [card2 setEmail: bEmail];
    [card3 setName: cName];
    [card3 setEmail: cEmail];
    [card4 setName: dName];
    [card4 setEmail: dEmail];   
   
    AddressBook *myBook = [AddressBook alloc];
   
    // Now init the Address Book
    myBook = [myBook initWithName:@"Donna's Address Book"];
   
    NSLog( @"Entries in Address Book after creation: %i", [myBook entries]);
                                                         
    // add some cards to the address book
    [myBook addCard:card1];
    [myBook addCard:card2];
    [myBook addCard:card3];
    [myBook addCard:card4];
   
    NSLog(@"Entries in Address Book after adding cards: %i", [myBook entries]);
    NSLog(@"\n-----------------------------\n");
   
    // list all entries in the book
    [myBook list];
   
    // lookup person by name
    NSLog(@"\n-----------------------------\n");
    NSLog(@"Lookup: Steve");
    myCard = [myBook lookup: @"Steve"];
   
    if(myCard != nil)
        [myCard print];
    else
        NSLog(@"NOT FOUND!");
   
    NSLog(@"\n-----------------------------\n");
    [myBook sort];
    [myBook list];
   
    NSLog(@"\n-----------------------------\n");
    NSMutableArray *matchList;
   
    matchList = [myBook lookupAllMatchesFor:@"StEvE"];
   
    // implemented showMatchList and printMatchList as methods
    if(!matchList)
        NSLog(@"NO MATCHES FOUND");
    else
    {
        [myBook showMatchList:matchList];
        [myBook printMatchList:matchList];
    }
   
    // implemented showMatchList and printMatchList as functions
    NSLog(@"\n-----------------------------\n");
    if(!matchList)
        NSLog(@"NO MATCHES FOUND");
    else
    {
        showMatchList(matchList);
        printMatchList(matchList);
    }
   
    [card1 release];
    [card2 release];
    [card3 release];
    [card4 release];
    [myBook release];
   
    [pool drain];
    return 0;
}



SAMPLE OUTPUT


Code: (Objective-C)
2011-09-08 21:11:23.072 AddressCards[589:903] Entries in Address Book after creation: 0
2011-09-08 21:11:23.075 AddressCards[589:903] Entries in Address Book after adding cards: 4
2011-09-08 21:11:23.075 AddressCards[589:903]
-----------------------------
2011-09-08 21:11:23.076 AddressCards[589:903] =========  Contents of: Donna's Address Book =========
2011-09-08 21:11:23.077 AddressCards[589:903] John Williams        JWilliams@abc.com               
2011-09-08 21:11:23.077 AddressCards[589:903] T Steve Smith        TSSmith@xyz.com                 
2011-09-08 21:11:23.078 AddressCards[589:903] Mary Bea             mbea@abc.com                   
2011-09-08 21:11:23.079 AddressCards[589:903] Julia & Steve Kean   jewls@xyz.com                   
2011-09-08 21:11:23.080 AddressCards[589:903] =======================================================
2011-09-08 21:11:23.081 AddressCards[589:903]
-----------------------------
2011-09-08 21:11:23.082 AddressCards[589:903] Lookup: Steve
2011-09-08 21:11:23.082 AddressCards[589:903] =====================================
2011-09-08 21:11:23.083 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.084 AddressCards[589:903] |   T Steve Smith                   |
2011-09-08 21:11:23.084 AddressCards[589:903] |   TSSmith@xyz.com                 |
2011-09-08 21:11:23.084 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.085 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.086 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.086 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.087 AddressCards[589:903] |    o                     o        |
2011-09-08 21:11:23.087 AddressCards[589:903] =====================================
2011-09-08 21:11:23.088 AddressCards[589:903]
-----------------------------
2011-09-08 21:11:23.088 AddressCards[589:903] =========  Contents of: Donna's Address Book =========
2011-09-08 21:11:23.089 AddressCards[589:903] John Williams        JWilliams@abc.com               
2011-09-08 21:11:23.089 AddressCards[589:903] Julia & Steve Kean   jewls@xyz.com                   
2011-09-08 21:11:23.090 AddressCards[589:903] Mary Bea             mbea@abc.com                   
2011-09-08 21:11:23.090 AddressCards[589:903] T Steve Smith        TSSmith@xyz.com                 
2011-09-08 21:11:23.091 AddressCards[589:903] =======================================================
2011-09-08 21:11:23.091 AddressCards[589:903]
-----------------------------
2011-09-08 21:11:23.092 AddressCards[589:903] =========  Contents of All Match List  ==========
2011-09-08 21:11:23.093 AddressCards[589:903] Julia & Steve Kean   jewls@xyz.com                   
2011-09-08 21:11:23.093 AddressCards[589:903] T Steve Smith        TSSmith@xyz.com                 
2011-09-08 21:11:23.094 AddressCards[589:903] =======================================================
2011-09-08 21:11:23.094 AddressCards[589:903] =====================================
2011-09-08 21:11:23.096 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.097 AddressCards[589:903] |   Julia & Steve Kean              |
2011-09-08 21:11:23.097 AddressCards[589:903] |   jewls@xyz.com                   |
2011-09-08 21:11:23.098 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.099 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.099 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.100 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.101 AddressCards[589:903] |    o                     o        |
2011-09-08 21:11:23.101 AddressCards[589:903] =====================================
2011-09-08 21:11:23.102 AddressCards[589:903] =====================================
2011-09-08 21:11:23.103 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.104 AddressCards[589:903] |   T Steve Smith                   |
2011-09-08 21:11:23.104 AddressCards[589:903] |   TSSmith@xyz.com                 |
2011-09-08 21:11:23.105 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.106 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.106 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.107 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.107 AddressCards[589:903] |    o                     o        |
2011-09-08 21:11:23.108 AddressCards[589:903] =====================================
2011-09-08 21:11:23.109 AddressCards[589:903]
-----------------------------
2011-09-08 21:11:23.111 AddressCards[589:903] =========  Contents of All Match List  ==========
2011-09-08 21:11:23.115 AddressCards[589:903] Julia & Steve Kean   jewls@xyz.com                   
2011-09-08 21:11:23.119 AddressCards[589:903] T Steve Smith        TSSmith@xyz.com                 
2011-09-08 21:11:23.120 AddressCards[589:903] =======================================================
2011-09-08 21:11:23.121 AddressCards[589:903] =====================================
2011-09-08 21:11:23.123 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.125 AddressCards[589:903] |   Julia & Steve Kean              |
2011-09-08 21:11:23.126 AddressCards[589:903] |   jewls@xyz.com                   |
2011-09-08 21:11:23.127 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.127 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.128 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.132 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.133 AddressCards[589:903] |    o                     o        |
2011-09-08 21:11:23.137 AddressCards[589:903] =====================================
2011-09-08 21:11:23.137 AddressCards[589:903] =====================================
2011-09-08 21:11:23.138 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.139 AddressCards[589:903] |   T Steve Smith                   |
2011-09-08 21:11:23.139 AddressCards[589:903] |   TSSmith@xyz.com                 |
2011-09-08 21:11:23.140 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.140 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.141 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.141 AddressCards[589:903] |                                   |
2011-09-08 21:11:23.142 AddressCards[589:903] |    o                     o        |
2011-09-08 21:11:23.142 AddressCards[589:903] =====================================
Program ended with exit code: 0


Logged
LateralusRok
Newbie
*
Posts: 6


Email
« Reply #1 on: September 14, 2011, 10:26:57 AM »

Not sure on your questions but your code was very easy to follow thanks!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.