Amazon.com Widgets Exercise 19.4 ..the fun one :)
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 20, 2014, 05:23:15 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 19
| | | |-+ Exercise 19.4 ..the fun one :)
Pages: [1] Go Down
Print
Author Topic: Exercise 19.4 ..the fun one :) (Read 1412 times)
tknp
Newbie
*
Posts: 23






on: February 05, 2012, 04:35:56 AM

This program assumes you have an addrbook.arch in the executable's directory that has name, email, city, and state info for each card.

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


@interface AddressBook : NSObject <NSCoding, NSCopying>

@property (nonatomic, retain) NSString *bookName;
@property (nonatomic, copy) NSMutableArray *book;

-(id)   initWithName: (NSString *) name;

-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;

-(NSMutableArray *) lookup: (NSString *) theName;
-(int)  entries;
-(void) list;
-(void) sort;

@end

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 || ([nextCard.email rangeOfString: theName options: NSCaseInsensitiveSearch]).length )
[cardMatches addObject: nextCard];
}

if ([cardMatches count])
return cardMatches;

return nil;
}

-(int) entries
{
return (int) [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 ) {
        NSString *res = [[theCard.city stringByAppendingString: @", "] stringByAppendingString: theCard.state];
NSLog(@"%-15s   %-20s   %@", [theCard.name UTF8String], [theCard.email UTF8String], res);
    }
NSLog(@"===============================================================");
}

-(id) copyWithZone: (NSZone *) zone
{
    AddressBook *newBook = [[AddressBook allocWithZone: zone] initWithName: @""];
    newBook.book = self.book;
    
    return newBook;
}

-(void) encodeWithCoder: (NSCoder *) encoder
{
    [encoder encodeObject: bookName forKey: @"AddressBookName"];
    [encoder encodeObject: book forKey: @"AddressBook"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
    bookName = [[decoder decodeObjectForKey: @"AddressBookName"] retain];
    book = [[decoder decodeObjectForKey: @"AddressBook"] copy];
    
    return self;
}

@end

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


@interface AddressCard : NSObject <NSCoding>

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

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) setCity: (NSString *) theCity andState: (NSString *) theState;
-(NSComparisonResult) compareNames: (id) element;
-(void) print;

// Additional method for NSCopying protocol
-(void) assignName: (NSString *) theName andEmail: (NSString *) theEmail;

@end

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


@implementation AddressCard

@synthesize name, email, city, state;

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

-(void) setCity: (NSString *) theCity andState: (NSString *) theState
{
self.city = theCity;
self.state = theState;
}

-(NSComparisonResult) compareNames: (id) element
{
return [name compare: [element name]];
}

-(void) print
{
NSString *res = [[city stringByAppendingString: @", "] stringByAppendingString: state];
NSMutableString *filler = [[NSMutableString alloc] init];

for (int i = 0; i <= 31 - res.length; i++)
[filler appendString: @" "];
[filler appendString: @"|"];
res = [res stringByAppendingString: filler];

NSLog(@"====================================");
NSLog(@"|                                  |");
NSLog(@"|  %-31s |", [name UTF8String]);
NSLog(@"|  %-31s |", [email UTF8String]);
NSLog(@"|  %@", res);
NSLog(@"|                                  |");
NSLog(@"|                                  |");
NSLog(@"|      O                  O        |");
NSLog(@"====================================");

}

-(void) encodeWithCoder: (NSCoder *) encoder
{
    [encoder encodeObject: name forKey: @"AddressCardName"];
    [encoder encodeObject: email forKey: @"AddressCardEmail"];
    [encoder encodeObject: city forKey: @"AddressCardCity"];
    [encoder encodeObject: state forKey: @"AddressCardState"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
    self.name = [decoder decodeObjectForKey: @"AddressCardName"];
    self.email = [decoder decodeObjectForKey: @"AddressCardEmail"];
    self.city = [decoder decodeObjectForKey: @"AddressCardCity"];
    self.state = [decoder decodeObjectForKey: @"AddressCardState"];
    
    return self;
}

-(void) assignName: (NSString *) theName andEmail: (NSString *) theEmail
{
    name = theName;
    email = theEmail;
}

@end

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

int main (int argc, const char * argv[])
{
    AddressBook     *myBook;
    @autoreleasepool {
        
        NSProcessInfo       *proc = [NSProcessInfo processInfo];
        NSArray             *args = [proc arguments];
        NSMutableArray      *mySearch;
        AddressCard         *myCard;
        
        if ([args count] != 2) {
            NSLog(@"Usage: %@ NAME", [proc processName]);
            return 1;
        }
        
        myBook = [NSKeyedUnarchiver unarchiveObjectWithFile: @"addrbook.arch"];
        
        [myBook list];  // just to show the possibilities for the search
        NSLog(@"\n");
                        
        NSLog(@"Looking up: %@", [args objectAtIndex: 1]);
        mySearch = [myBook lookup: [args objectAtIndex: 1]];
        NSLog(@"Entries matching search: %lu", mySearch.count);
        if ([mySearch count])
        {
            for (myCard in mySearch)
                [myCard print];
        }
        else
            NSLog(@"Not found!");
        
    }
    return 0;
}

and the output
Code: (Objective-C)
./lookup stephen
 ============== Contents of: Linda's Address Book ==============
Julia Kochan      jewls337@axlc.com      Las Vegas, Nevada
Tony Iannino      tony.iannino@aol.com   San Mateo, California
Stephen Kochan    steve@classroomM.com   Las Vegas, Nevada
Steve Baker       sbaker@aol.com         Portland, Oregon
===============================================================

Looking up: stephen
Entries matching search: 1
====================================
|                                  |
|  Stephen Kochan                  |
|  steve@classroomM.com            |
|  Las Vegas, Nevada               |
|                                  |
|                                  |
|      O                  O        |
====================================
Last Edit: February 05, 2012, 04:38:28 AM by tknp Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 05, 2012, 11:01:32 AM

Nice work!  I see you have me living in Vegas!  I hope I'm winning!!  Smiley

Steve
Logged
tknp
Newbie
*
Posts: 23






Reply #2 on: February 06, 2012, 03:25:09 AM

Stick to the blackjack tables and you'll do fine Smiley

And thanks!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: February 06, 2012, 10:06:45 AM

I like to play craps and Hold 'Em too!
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.