Amazon.com Widgets Exercise 15.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 23, 2014, 05:21:51 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 15
| | | |-+ Exercise 15.2
Pages: [1] Go Down
Print
Author Topic: Exercise 15.2 (Read 702 times)
tknp
Newbie
*
Posts: 23






on: January 22, 2012, 11:58:19 PM

Came up with this thanks to some pointers I found in another board here.

AddressBook.m
Code: (Objective-C)
#import "AddressBook.h"


@implementation AddressBook

@synthesize bookName, book;

// set up the AddressBook's name and an empty book

-(id) initWithName: (NSString *) name
{
self = [super init];

if (self) {
bookName = [NSString stringWithString: name];
book = [NSMutableArray array];
}

return self;
}

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

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

-(void) removeCard: (AddressCard *) theCard
{
[book removeObjectIdenticalTo: theCard];
}

-(NSMutableArray *) lookup: (NSString *) theName
{
NSMutableArray *cardMatches = [[NSMutableArray alloc] init];

for ( AddressCard *nextCard in book )
{
// rangeOfString returns the location & length if there is a match. The .length is there as a check to see if the value exists
if ( ([nextCard.name rangeOfString: theName options: NSCaseInsensitiveSearch]).length )
[cardMatches addObject: nextCard];
}

if ([cardMatches count])
return cardMatches;

return nil;
}

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

-(void) sort
{
[book sortUsingComparator: ^(id obj1, id obj2) {
return [[obj1 name] compare: [obj2 name]];
} ];
}

-(void) list
{
NSLog(@"========= Contents of: %@ =========", bookName);

for ( AddressCard *theCard in book )
NSLog(@"%-20s    %-32s", [theCard.name UTF8String], [theCard.email UTF8String]);
NSLog(@"=====================================================");
}

@end

snippet from main.m
Code: (Objective-C)
NSString *aName = @"Julia Kochan";
NSString *aEmail = @"jewls337@axlc.com";
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"tony.iannino@techfitness.com";
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"steve@classroomM.com";
NSString *dName = @"Steve Baker";
NSString *dEmail = @"sbaker@aol.com";

AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];

// Set up a new address book

AddressBook *myBook = [[AddressBook alloc] initWithName: @"Linda's Address Book"];
AddressCard *myCard;
NSMutableArray *mySearch;

NSLog(@"Entries in address book after creation: %i", [myBook entries]);

// Now set up four address cards

[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];

// Add the 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]);


// List all the entries in the book now

[myBook list];


// Look up a person by name

NSLog(@"Looking up: Julia Kochan");
mySearch = [myBook lookup: @"julia kochan"];
NSLog(@"Entries matching search: %i", mySearch.count);
if ([mySearch count])
{
for (myCard in mySearch)
[myCard print];
}
else
NSLog(@"Not found!");


// Try a partial lookup

NSLog(@"Looking up: st");
mySearch = [myBook lookup: @"st"];
NSLog(@"Entries matching search: %i", mySearch.count);
if ([mySearch count])
{
for (myCard in mySearch)
[myCard print];
}
else
NSLog(@"Not found!");

    [pool drain];
    return 0;

outputs:

2012-01-22 22:53:26.933 exercise15address[5722:a0f] Entries in address book after creation: 0
2012-01-22 22:53:26.937 exercise15address[5722:a0f] Entries in address book after adding cards: 4
2012-01-22 22:53:26.938 exercise15address[5722:a0f] ========= Contents of: Linda's Address Book =========
2012-01-22 22:53:26.939 exercise15address[5722:a0f] Julia Kochan            jewls337@axlc.com              
2012-01-22 22:53:26.940 exercise15address[5722:a0f] Tony Iannino            tony.iannino@techfitness.com    
2012-01-22 22:53:26.940 exercise15address[5722:a0f] Stephen Kochan          steve@classroomM.com            
2012-01-22 22:53:26.941 exercise15address[5722:a0f] Steve Baker             sbaker@aol.com                  
2012-01-22 22:53:26.941 exercise15address[5722:a0f] =====================================================
2012-01-22 22:53:26.942 exercise15address[5722:a0f] Looking up: Julia Kochan
2012-01-22 22:53:26.942 exercise15address[5722:a0f] Entries matching search: 1
2012-01-22 22:53:26.943 exercise15address[5722:a0f] ====================================
2012-01-22 22:53:26.944 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.944 exercise15address[5722:a0f] |  Julia Kochan                    |
2012-01-22 22:53:26.945 exercise15address[5722:a0f] |  jewls337@axlc.com               |
2012-01-22 22:53:26.945 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.946 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.946 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.947 exercise15address[5722:a0f] |      O                  O        |
2012-01-22 22:53:26.948 exercise15address[5722:a0f] ====================================
2012-01-22 22:53:26.948 exercise15address[5722:a0f] Looking up: st
2012-01-22 22:53:26.949 exercise15address[5722:a0f] Entries matching search: 2
2012-01-22 22:53:26.949 exercise15address[5722:a0f] ====================================
2012-01-22 22:53:26.950 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.951 exercise15address[5722:a0f] |  Stephen Kochan                  |
2012-01-22 22:53:26.951 exercise15address[5722:a0f] |  steve@classroomM.com            |
2012-01-22 22:53:26.952 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.952 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.953 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.953 exercise15address[5722:a0f] |      O                  O        |
2012-01-22 22:53:26.954 exercise15address[5722:a0f] ====================================
2012-01-22 22:53:26.954 exercise15address[5722:a0f] ====================================
2012-01-22 22:53:26.955 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.955 exercise15address[5722:a0f] |  Steve Baker                     |
2012-01-22 22:53:26.956 exercise15address[5722:a0f] |  sbaker@aol.com                  |
2012-01-22 22:53:26.956 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.957 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.958 exercise15address[5722:a0f] |                                  |
2012-01-22 22:53:26.958 exercise15address[5722:a0f] |      O                  O        |
2012-01-22 22:53:26.959 exercise15address[5722:a0f] ====================================
Last Edit: January 23, 2012, 12:15:15 AM by tknp Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.