Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:18:16 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: exercise 15-4 (with modified sort)  (Read 1419 times)
mdziedzic
Newbie
*
Posts: 41



WWW
« on: December 08, 2009, 05:49:11 PM »

AddressCard.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface AddressCard: NSObject {
NSString *firsName;
NSString *lastName;
NSString *streetAddress;
NSString *city;
NSString *usState;
NSString *zip;
NSString *country;
NSString *phone;
NSString *email;
}

@property (copy, nonatomic) NSString *firstName, *lastName, *streetAddress, *city,
*usState, *zip, *country, *phone, *email;

-(void) setFirstName: (NSString *) theFirstName
andLastName: (NSString *) theLastName
andStreetAddress: (NSString *) theStreetAddress
andCity: (NSString *) theCity
 andUsState: (NSString *) theUsState
 andZip: (NSString *) theZip
 andCountry: (NSString *) theCountry
andPhone: (NSString *) thePhone
andEmail: (NSString *) theEmail;

-(void) print;
-(NSComparisonResult) compareNames: (id) element;

@end

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


@implementation AddressCard

@synthesize firstName, lastName, streetAddress, city, usState, zip, country, phone, email;


-(void) setFirstName: (NSString *) theFirstName
andLastName: (NSString *) theLastName
andStreetAddress: (NSString *) theStreetAddress
andCity: (NSString *) theCity
 andUsState: (NSString *) theUsState
 andZip: (NSString *) theZip
 andCountry: (NSString *) theCountry
andPhone: (NSString *) thePhone
andEmail: (NSString *) theEmail
{
self.firstName = theFirstName;
self.lastName = theLastName;
self.streetAddress = theStreetAddress;
self.city = theCity;
self.usState = theUsState;
self.zip = theZip;
self.country = theCountry;
self.phone = thePhone;
self.email = theEmail;
}

-(void) print
{
NSString *fullName = [self.firstName stringByAppendingString: @" "];
fullName = [fullName stringByAppendingString: self.lastName];

NSLog(@"====================================");
NSLog(@"|                                  |");
NSLog(@"|  %-31s |", [fullName UTF8String]);
NSLog(@"|  %-31s |", [streetAddress UTF8String]);
NSLog(@"|  %-31s |", [city UTF8String]);
NSLog(@"|  %-31s |", [usState UTF8String]);
NSLog(@"|  %-31s |", [zip UTF8String]);
NSLog(@"|  %-31s |", [country UTF8String]);
NSLog(@"|  %-31s |", [phone UTF8String]);
NSLog(@"|  %-31s |", [email UTF8String]);
NSLog(@"|                                  |");
NSLog(@"|                                  |");
NSLog(@"|       O                  O       |");
NSLog(@"====================================");
}

-(NSComparisonResult) compareNames: (id) element
{
NSString *eFullName = [[element lastName] stringByAppendingString: @" "];
eFullName = [eFullName stringByAppendingString: [element firstName]];

NSString *fullName = [self.lastName stringByAppendingString: @" "];
fullName = [fullName stringByAppendingString: self.firstName];

return [fullName compare: eFullName];
}

@end

AddressBook.h
Code: (Objective-C)
#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;
-(void) sort;
-(NSMutableArray *) lookup: (NSString *) theName;
-(void) dealloc;

@end

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


@implementation AddressBook

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

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

if (self) {
bookName = [[NSString alloc] initWithString: name];
book = [[NSMutableArray alloc] init];
}

return self;
}


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


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


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


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

for (AddressCard *theCard in book) {
NSString *fullName = [theCard.lastName stringByAppendingString: @", "];
fullName = [fullName stringByAppendingString: theCard.firstName];

NSLog(@"%-15s    %-32s", [fullName UTF8String],
 [theCard.email UTF8String]);
}

NSLog(@"==================================================");
}


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


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

for (AddressCard *nextCard in book) {

NSString *fullName = [nextCard.firstName stringByAppendingString: @" "];
fullName = [fullName stringByAppendingString: nextCard.lastName];

NSRange textSearch = [fullName rangeOfString: theName options: NSCaseInsensitiveSearch];
if (textSearch.location != NSNotFound)
[matches addObject: nextCard];
}

return matches;
}


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

@end

main
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "AddressBook.h"


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *aFirstName = @"Julia";
NSString *aLastName = @"Kochan";
NSString *aStreetAddress = @"123 Main Street";
NSString *aCity = @"Denver";
NSString *aUsState = @"Colorado";
NSString *aZip = @"33454-2323";
NSString *aCountry = @"USA";
NSString *aPhone = @"333-343-9876";
NSString *aEmail = @"jewls377@axlc.com";

NSString *bFirstName = @"Tony";
NSString *bLastName = @"Iannino";
NSString *bStreetAddress = @"717 Atlantic Avenue";
NSString *bCity = @"Boston";
NSString *bUsState = @"Massachusetts";
NSString *bZip = @"22122";
NSString *bCountry = @"USA";
NSString *bPhone = @"217-333-5656";
NSString *bEmail = @"tony.iannino@techfitness.com";

NSString *cFirstName = @"Stephen";
NSString *cLastName = @"Kochan";
NSString *cStreetAddress = @"321 Main Street";
NSString *cCity = @"Boulder";
NSString *cUsState = @"Colorado";
NSString *cZip = @"33454";
NSString *cCountry = @"USA";
NSString *cPhone = @"333-555-3434";
NSString *cEmail = @"steven@kochan.wood.com";

NSString *dFirstName = @"Jamie";
NSString *dLastName = @"Baker";
NSString *dStreetAddress = @"1 Infinite Loop";
NSString *dCity = @"Cupertino";
NSString *dUsState = @"California";
NSString *dZip = @"99332";
NSString *dCountry = @"USA";
NSString *dPhone = @"444-555-3333";
NSString *dEmail = @"jbaker@kochan-wood.com";

NSString *eFirstName = @"Maki";
NSString *eLastName = @"Dziedzic";
NSString *eStreetAddress = @"2824 NW Santanita Terrace";
NSString *eCity = @"Portland";
NSString *eUsState = @"Oregon";
NSString *eZip = @"97210";
NSString *eCountry = @"USA";
NSString *ePhone = @"503-220-4010";
NSString *eEmail = @"maki@eggfoo.com";


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


AddressBook *myBook = [AddressBook alloc];
NSMutableArray *matchedCards;

// first set up five address cards

[card1 setFirstName: aFirstName
andLastName: aLastName
  andStreetAddress: aStreetAddress
andCity: aCity
andUsState: aUsState
andZip: aZip
andCountry: aCountry
  andPhone: aPhone
  andEmail: aEmail];

[card2 setFirstName: bFirstName
andLastName: bLastName
  andStreetAddress: bStreetAddress
andCity: bCity
andUsState: bUsState
andZip: bZip
andCountry: bCountry
  andPhone: bPhone
  andEmail: bEmail];

[card3 setFirstName: cFirstName
andLastName: cLastName
  andStreetAddress: cStreetAddress
andCity: cCity
andUsState: cUsState
andZip: cZip
andCountry: cCountry
  andPhone: cPhone
  andEmail: cEmail];

[card4 setFirstName: dFirstName
andLastName: dLastName
  andStreetAddress: dStreetAddress
andCity: dCity
andUsState: dUsState
andZip: dZip
andCountry: dCountry
  andPhone: dPhone
  andEmail: dEmail];

[card5 setFirstName: eFirstName
andLastName: eLastName
  andStreetAddress: eStreetAddress
andCity: eCity
andUsState: eUsState
andZip: eZip
andCountry: eCountry
  andPhone: ePhone
  andEmail: eEmail];


myBook = [myBook initWithName: @"Michael's Address Book"];

// add some cards to the address book

[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
[myBook addCard: card5];

// look up a person by name

NSString *searchTerm = @"ko";

NSLog(@"Lookup: %@", searchTerm);
matchedCards = [myBook lookup: searchTerm];

if ([matchedCards count] > 0) {
for (AddressCard *theCard in matchedCards)
[theCard print];
} else {
NSLog(@"Not found!");
}

NSLog(@"\n");
NSLog(@"Unsorted Address Book:\n\n");

[myBook list];

NSLog(@"\n");
NSLog(@"Sorted Address Book:\n\n");

[myBook sort];
[myBook list];


[card1 release];
[card2 release];
[card3 release];
[card4 release];
[card5 release];
[myBook release];

[pool drain];
    return 0;
}



Lookup: ko
====================================
|                                  |
|  Julia Kochan                    |
|  123 Main Street                 |
|  Denver                          |
|  Colorado                        |
|  33454-2323                      |
|  USA                             |
|  333-343-9876                    |
|  jewls377@axlc.com               |
|                                  |
|                                  |
|       O                  O       |
====================================
====================================
|                                  |
|  Stephen Kochan                  |
|  321 Main Street                 |
|  Boulder                         |
|  Colorado                        |
|  33454                           |
|  USA                             |
|  333-555-3434                    |
|  steven@kochan.wood.com          |
|                                  |
|                                  |
|       O                  O       |
====================================

Unsorted Address Book:

======== Contents of: Michael's Address Book =========
Kochan, Julia      jewls377@axlc.com               
Iannino, Tony      tony.iannino@techfitness.com   
Kochan, Stephen    steven@kochan.wood.com         
Baker, Jamie       jbaker@kochan-wood.com         
Dziedzic, Maki     maki@eggfoo.com                 
==================================================

Sorted Address Book:

======== Contents of: Michael's Address Book =========
Baker, Jamie       jbaker@kochan-wood.com         
Dziedzic, Maki     maki@eggfoo.com                 
Iannino, Tony      tony.iannino@techfitness.com   
Kochan, Julia      jewls377@axlc.com               
Kochan, Stephen    steven@kochan.wood.com         
==================================================


« Last Edit: December 08, 2009, 05:51:10 PM by scarbelly » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: December 09, 2009, 12:43:59 PM »

Looks good!   All those instance variables in the AddressCard class need to be released in a dealloc method (since they have the copy attribute).

Cheers,

Steve

Logged
mdziedzic
Newbie
*
Posts: 41



WWW
« Reply #2 on: December 09, 2009, 01:33:19 PM »

Thanks Steve. That helps... will do.
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.