Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:12:36 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: 15-6 Simple way  (Read 1285 times)
tadej5553
Full Member
***
Posts: 145


Email
« on: May 21, 2010, 01:50:59 PM »

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

#define FN @"First Name"
#define LN @"Last Name"

@interface AddressCard: NSObject
{
    NSMutableDictionary *fields;
}

@property (copy, nonatomic) NSMutableDictionary *fields;

-(AddressCard *) initWithFirstName: (NSString *) fn andLastName: (NSString *) ln;
-(void) setFirstName: (NSString *) fn andLastName: (NSString *) ln;

-(void) setField: (NSString *) newField;
-(void) setField: (NSString *) newField withContents: (NSString *) newContents;

-(NSComparisonResult) compareNames: (id) element;

-(void) print;
-(void) dealloc;

@end

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

@implementation AddressCard

@synthesize  fields;

-(void) print
{
    NSLog (@"==============================================");
    NSLog (@"|                                            |");
    NSLog (@"|  %-41s |", [[NSString stringWithFormat:@"%@ %@", [fields valueForKey: FN], [fields valueForKey: LN]] UTF8String]);
    NSLog (@"|                                            |");
    
       for(NSString *key in fields)
        if([key caseInsensitiveCompare: FN] != NSOrderedSame && 
           [key caseInsensitiveCompare: LN] != NSOrderedSame)
            NSLog (@"|  %-15s: %-24s |", [key UTF8String], [[fields valueForKey: key] UTF8String]);   
    
    NSLog (@"|                                            |");
    NSLog (@"|                                            |");
    NSLog (@"|                                            |");
    NSLog (@"|            O                  O            |");
    NSLog (@"==============================================");
}

-(AddressCard *) init
{
    [self initWithFirstName: @"" andLastName: @""];    
    return self;
}

-(AddressCard *) initWithFirstName:(NSString *) fn andLastName:(NSString *) ln
{
    self = [super init];
    
    if(self) {
        fields = [[NSMutableDictionary alloc] init];
        
        [fields setObject: fn forKey: FN];
        [fields setObject: ln forKey: LN];
    }
    
    return self;
}

-(void) setFirstName:(NSString *) fn andLastName:(NSString *) ln
{
    [fields setObject: fn forKey: FN];
    [fields setObject: ln forKey: LN];
}

-(void) setField: (NSString *) newField withContents: (NSString *) newContents
{
    [fields setObject: newContents forKey: newField];
}

-(void) setField: (NSString *) newField
{
    [self setField: newField withContents: nil];
}

-(NSComparisonResult) compareNames: (id) element
{
    NSComparisonResult result;
    
    result = [[[self fields] objectForKey: LN] caseInsensitiveCompare: [[element fields] objectForKey: LN]];
    
    if (result == NSOrderedSame)
        result = [[[self fields] objectForKey: FN] caseInsensitiveCompare: [[element fields] objectForKey: FN]];
    
    return result;
}

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

@end

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

@interface AddressBook: NSObject
{
    NSString        *bookName;
    NSMutableArray  *book;
}

-(id) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(NSMutableArray *) lookup: (NSString *) theName;
-(BOOL) removeName: (NSString *) name;

-(int)  entries;
-(void) list;
-(void) sort;
-(void) dealloc;

@end

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

@implementation AddressBook;

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

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

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

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

-(void) list
{
    NSLog (@"========= Contents of: %@ =============", bookName);
    for ( AddressCard *theCard in book )
        NSLog (@"   %@", [NSString stringWithFormat:@"%@ %@", [[theCard fields] valueForKey: FN], [[theCard fields] valueForKey: LN]]);
    
    NSLog (@"===========================================");
}

-(NSMutableArray *) lookup: (NSString *) theName
{
    NSMutableArray *result = [NSMutableArray array];
    
    for (AddressCard *nextCard in book)
    {
        for(NSString *key in nextCard.fields)
            if ([[nextCard.fields valueForKey: key] rangeOfString: theName options: NSCaseInsensitiveSearch].length)
            {
                [result addObject: nextCard];  
                break;
            }
        
        if(![result containsObject: nextCard])
            if ([[NSString stringWithFormat: @"%@ %@", [nextCard.fields valueForKey: FN], [nextCard.fields valueForKey: LN]] compare: theName] == NSOrderedSame)
                [result addObject: nextCard];
    }
    
    if([result count])
        return result;
    
    return nil;
}

-(BOOL) removeName: (NSString *) theName
{
    NSMutableArray *result = [self lookup: theName];
    
    if([result count] != 1)
        return NO;
    
    [book removeObject: [result objectAtIndex: 0]];
    return YES;
}

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

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

@end

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

int main (int argc, const char * argv[]) {  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSMutableArray *result;
    
    NSString  *fn = @"Julia";
    NSString  *ln = @"Baker";
    AddressCard *card1 = [[AddressCard alloc] initWithFirstName: fn andLastName: ln];
    
    NSString  *fn2 = @"Joanna";
    NSString  *ln2 = @"Baker";
    AddressCard *card2 = [[AddressCard alloc] initWithFirstName: fn2 andLastName: ln2];
    
    NSString  *fn3 = @"Thomas";
    NSString  *ln3 = @"Green";
    AddressCard *card3 = [[AddressCard alloc] initWithFirstName: fn3 andLastName: ln3];
    
    NSString  *fn4 = @"George";
    NSString  *ln4 = @"Anderson";
    AddressCard *card4 = [[AddressCard alloc] initWithFirstName: fn4 andLastName: ln4];
    
    AddressBook *book1 = [[AddressBook alloc] initWithName: @"MyBook"];
    [book1 addCard: card1];
    [book1 addCard: card2];
    [book1 addCard: card3];
    [book1 addCard: card4];
    [book1 sort];
    [book1 list];
    
    //Testing the new ability to search full names                            
    NSLog(@"Lookup: Joanna Baker");
    result = [book1 lookup: @"Joanna Baker"];
    
    for (AddressCard *theCard in result)
        NSLog (@"   %@ %@", [[theCard fields] valueForKey: FN], [[theCard fields] valueForKey: LN]);
    
    NSLog(@"Remove: Julia Baker");
    [book1 removeName: @"Julia Baker"];
    [book1 list];     //To check if the name was removed
    
    [card1 release];
    [card2 release];
    [card3 release];
    [card4 release];
    [book1 release];    

    [pool drain];  
    return 0;  
}
« Last Edit: May 23, 2010, 12:28:57 PM by tadej5553 » 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.