Amazon.com Widgets Chapter 15 Exercise 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 05:55:22 AM
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
| | | |-+  Chapter 15 Exercise 6
Pages: [1]   Go Down
Print
Author Topic: Chapter 15 Exercise 6  (Read 419 times)
clouded
Full Member
***
Posts: 123






« on: June 14, 2012, 02:21:18 PM »

Here's what I did on this one... not sure if this is what the book wanted, but here it is:

Code: (Objective-C)
//  Chapter 15 Exercise 6
// Add the method removeName: to the AddressBook class to remove someone from
// the address book given this declaration for the method:
//     -(BOOL) removeName: (NSString *) theName;
// Use the lookup: method developed in exercise 3. If the name is not found or
// if multiple entries exist, have the method return NO. If the person is
// successfully removed, have it return YES.

//  main.m

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

int main (int argc, char * argv[])
{
    @autoreleasepool {
        
        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 = @"Jamie Baker";
        NSString *dEmail = @"jbaker@classroomM.com";
        NSString *eName = @"Ella Smith";
        NSString *eEmail = @"ella.smith@csmithent.com";
        NSString *eAddress = @"123 Main Street";
        NSString *eCity = @"Anywhere";
        NSString *eZip = @"12345";
        NSString *eCountry = @"USA";
        NSString *ePhone = @"(212) 555-1212";
        
        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] initWithName: @"Linda’s Address Book"];
        AddressBook *myResults;
        
        // First 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];
        [card5 setName: eName andEmail: eEmail andAddress: eAddress
               andCity: eCity andZip: eZip andCountry: eCountry andPhone: ePhone];
        
        // Add some cards to the address book
        [myBook addCard: card1];
        [myBook addCard: card2];
        [myBook addCard: card3];
        [myBook addCard: card4];
        [myBook addCard: card5];
        
        // List the book
        [myBook list];
        
        // Remove koc
        NSLog (@"remove: koc");
        if ([myBook removeName: @"koc"])
             NSLog (@"remove sucessful");
        else
            NSLog (@"remove unsucessful");
        
        // Lookup reason for answer
        NSLog (@"lookup: koc");
        myResults = [myBook lookup: @"koc"];
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
        
        // Remove steve
        NSLog (@"remove: steve");
        if ([myBook removeName: @"steve"])
            NSLog (@"remove successful");
        else
            NSLog (@"remove unsuccessful");
        
        // List the book
        [myBook list];
        
    }
    return 0;
}
Code: (Objective-C)
//  AddressBook.h

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

@interface AddressBook: NSObject

@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, strong) NSMutableArray *book;
-(id) initWithName: (NSString *) name;
-(BOOL) removeName: (NSString *) theName;
-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;
-(AddressBook *) lookup: (NSString *) theName;
-(int) entries;
-(void) list;
-(void) sort;
-(void) printCards;

@end
Code: (Objective-C)
//  AddressBook.m

#import "AddressBook.h"

@implementation AddressBook
@synthesize bookName, 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"];
}
-(BOOL) removeName: (NSString *) theName
{
    AddressBook *results = [self lookup: theName];
    AddressCard *holder;
    if (results.book.count == 1)  {
        for ( AddressCard *nextCard in book ) {
            if ( [nextCard search: theName])
                holder = nextCard;
            }
        [self removeCard: holder];
        return YES;
    }
    return NO;
}
-(void) addCard: (AddressCard *) theCard
{
    [book addObject: theCard];
}
-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}
-(AddressBook *) lookup: (NSString *) theName
{
    AddressBook *results = [[AddressBook alloc] initWithName: @"lookup results"];
    
    for ( AddressCard *nextCard in book ) {
        if ( [nextCard search: theName])
            [results addCard: nextCard];
    }
    
    if (results.book.count == 0)
        return nil;
    
    return results;
}
-(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 (@"==================================================");
}
-(void) sort
{
    [book sortUsingSelector: @selector(compareNames:)];
}
-(void) printCards
{
    NSLog (@"======== Contents of: %@ =========", bookName);
    
    for ( AddressCard *theCard in book ) {
        NSLog (@"====================================");
        NSLog (@"|                                  |");
        NSLog (@"|  %-31s |", [theCard.name UTF8String]);
        NSLog (@"|  %-31s |", [theCard.firstname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.lastname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.address UTF8String]);
        NSLog (@"|  %-31s |", [theCard.city UTF8String]);
        NSLog (@"|  %-31s |", [theCard.zip UTF8String]);
        NSLog (@"|  %-31s |", [theCard.country UTF8String]);
        NSLog (@"|  %-31s |", [theCard.phone UTF8String]);
        NSLog (@"|  %-31s |", [theCard.email UTF8String]);
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"====================================");
        NSLog (@" ");
    }
}
@end
Code: (Objective-C)
//  AddressCard.h

#import <Foundation/Foundation.h>

@interface AddressCard: NSObject

@property (copy, nonatomic) NSString *name, *firstname, *lastname, *address, *city, *zip, *country, *phone, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone;
-(void) print;
-(void) printAll;
-(BOOL) search: (NSString *) criteria;
-(NSComparisonResult) compareNames: (id) element;

@end
Code: (Objective-C)
//  AddressCard.m

#import "AddressCard.h"

@implementation AddressCard
@synthesize name, firstname, lastname, address, city, zip, country, phone, email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.email = theEmail;
    self.address = @" ";
    self.city = @" ";
    self.zip = @" ";
    self.country = @" ";
    self.phone = @" ";
}
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.email = theEmail;
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.address = theAddress;
    self.city = theCity;
    self.zip = theZip;
    self.country = theCountry;
    self.phone = thePhone;
}
-(void) print
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(void) printAll
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [firstname UTF8String]);
    NSLog (@"|  %-31s |", [lastname UTF8String]);
    NSLog (@"|  %-31s |", [address UTF8String]);
    NSLog (@"|  %-31s |", [city UTF8String]);
    NSLog (@"|  %-31s |", [zip UTF8String]);
    NSLog (@"|  %-31s |", [country UTF8String]);
    NSLog (@"|  %-31s |", [phone UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(BOOL) search: (NSString *) criteria
{
    if ( [self.name rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.address rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.city rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.zip rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.country rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.phone rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.email rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    
    return NO;
}
-(NSComparisonResult) compareNames: (id) element
{
    return [name compare: [element name]];
}
@end

Output:

======== Contents of: Linda’s Address Book =========
Julia Kochan           jewls337@axlc.com              
Tony Iannino           tony.iannino@techfitness.com    
Stephen Kochan         steve@classroomM.com            
Jamie Baker            jbaker@classroomM.com          
Ella Smith             ella.smith@csmithent.com        
==================================================
remove: koc
remove unsuccessful
lookup: koc
======== Contents of: lookup results =========
====================================
|                                  |
|  Julia Kochan                    |
|  Julia                           |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  jewls337@axlc.com               |
|                                  |
|                                  |
|                                  |
====================================
 
====================================
|                                  |
|  Stephen Kochan                  |
|  Stephen                         |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  steve@classroomM.com            |
|                                  |
|                                  |
|                                  |
====================================
 
remove: steve
remove successful
======== Contents of: Linda’s Address Book =========
Julia Kochan           jewls337@axlc.com              
Tony Iannino           tony.iannino@techfitness.com    
Jamie Baker            jbaker@classroomM.com          
Ella Smith             ella.smith@csmithent.com        
==================================================
« Last Edit: June 14, 2012, 04:26:01 PM by clouded » Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #1 on: September 11, 2012, 10:02:08 AM »

Code: (Objective-C)
-(BOOL) removeName:(NSString *)theName
{
    if([self lookup:theName]){
        [self removeCard:[self lookup:theName]];
        return YES;
    }
    else
        return NO;
}
Code: (Objective-C)
        NSString *aName    = @"Julia Kochan";
        NSString *aEmail   = @"jewls337@axlc.com";
        NSString *aAddress = @"USA";
        NSString *bName    = @"Tony Iannino";
        NSString *bEmail   = @"tony.iannino@techfitness.com";
        NSString *bAddress = @"Italy";
        NSString *cName    = @"Stephen Kochan";
        NSString *cEmail   = @"steve@classroom.com";
        NSString *cAddress = @"USA";
        NSString *dName    = @"Jamie Baker";
        NSString *dEmail   = @"jbaker@classroom.com";
        NSString *dAddress = @"UK";
       
        AddressCard *card1  = [[AddressCard alloc] init];
        AddressCard *card2  = [[AddressCard alloc] init];
        AddressCard *card3  = [[AddressCard alloc] init];
        AddressCard *card4  = [[AddressCard alloc] init];
       
        AddressBook *myBook = [[AddressBook alloc]initWithName:@"Linda's AddressBook"];
       
        [card1 setName:aName andEmail:aEmail andAddress:aAddress];
        [card2 setName:bName andEmail:bEmail andAddress:bAddress];
        [card3 setName:cName andEmail:cEmail andAddress:cAddress];
        [card4 setName:dName andEmail:dEmail andAddress:dAddress];
       
        [myBook addCard:card1];
        [myBook addCard:card2];
        [myBook addCard:card3];
        [myBook addCard:card4];
       
        [myBook list];
       
        [myBook removeName:@"stephen"];
       
        [myBook list];
2012-09-11 18:01:19.351 Address Book[621:303] ========= Contents of: Linda's AddressBook =============
2012-09-11 18:01:19.353 Address Book[621:303] Julia Kochan      jewls337@axlc.com                USA                                     
2012-09-11 18:01:19.354 Address Book[621:303] Tony Iannino      tony.iannino@techfitness.com     Italy                                   
2012-09-11 18:01:19.354 Address Book[621:303] Stephen Kochan    steve@classroom.com              USA                                     
2012-09-11 18:01:19.355 Address Book[621:303] Jamie Baker       jbaker@classroom.com             UK                                     
2012-09-11 18:01:19.355 Address Book[621:303] ========================================================
2012-09-11 18:01:19.355 Address Book[621:303] ========= Contents of: Linda's AddressBook =============
2012-09-11 18:01:19.356 Address Book[621:303] Julia Kochan      jewls337@axlc.com                USA                                     
2012-09-11 18:01:19.356 Address Book[621:303] Tony Iannino      tony.iannino@techfitness.com     Italy                                   
2012-09-11 18:01:19.368 Address Book[621:303] Jamie Baker       jbaker@classroom.com             UK                                     
2012-09-11 18:01:19.368 Address Book[621:303] ========================================================
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.