Interface file
@interface AddressCard: NSObject
{
NSString *name;
NSString *email;
}
@property (copy, nonatomic) NSString *name, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) print;
@end
@interface AddressBook : NSObject
{
NSString *bookName;
NSMutableArray *book;
}
-(id) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(void) dealloc;
@end
[/pre]
implementation
@implementation AddressCard
@synthesize name, email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
self.name = theName;
self.email = theEmail;
}
-(void) print
{
NSLog(@"====================================");
NSLog(@"| |");
NSLog(@"| %-31s |", [name UTF8String]);
NSLog(@"| %-31s |", [email UTF8String]);
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| O O |");
NSLog(@"====================================");
}
@end
@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];
}
-(int) entries
{
return [book count];
}
-(void) list
{
NSLog(@"========= Contents of: %@ =========", bookName);
for (AddressCard *theCard in book)
NSLog(@"%-20s %-32s", [theCard.name UTF8String], [theCard.email UTF8String]);
NSLog(@"=====================================================");
}
-(void) dealloc
{
[bookName release];
[book release];
[super dealloc];
}
@end
[/pre]
Test Program
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import "AddressBook.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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@kochan-wood.com";
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"jbaker@kochan-wood.com";
AddressCard *card1 = [[ AddressCard alloc] init];
AddressCard *card2 = [[ AddressCard alloc] init];
AddressCard *card3 = [[ AddressCard alloc] init];
AddressCard *card4 = [[ AddressCard alloc] init];
// set up Four addressCards
[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];
AddressBook *myBook = [ AddressBook alloc];
// Now initialize the Adress Book
myBook = [ myBook initWithName:@"L=inda'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]);
// list all the entries in the book
[myBook list];
[card1 release];
[card2 release];
[card3 release];
[card4 release];
[pool drain];
return 0;
}
[/pre]