Chapter Gold Challenge Solution


#1

Here are my h and m code for the new subclass, BNRContainer followed by the code in main(). Feedback is appreciated. However, be nice. My only source of programming is Visual Basic and MatLab, both so very different from Objective-C. But I’m enjoying the challenge.

BNRContainer.h file

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSString *containerName;
    int containerValueInDollars;
    NSMutableArray *containerArray;
}

- (id)initWithContainerName: (NSString *)contName
             containerArray: (NSMutableArray *)contArray
    containerValueInDollars: (int)contValue;

- (id)initWithItem: (BNRItem *)item
     containerName: (NSString *)contArray
    containerArray: (NSMutableArray *)contArray;

- (void)addBNRItem: (BNRItem *)item
       toContainer: (NSMutableArray *)contArray;

- (void)setContainerName: (NSString *)contName;
- (NSString *)containerName;

- (void)setContainerValueInDollars: (int)contValue;
- (int)containerValueInDollars;

- (void)setContainerArray: (NSMutableArray *)contArray;
- (NSMutableArray *)containerArray;

@end

BNRContainer.m File

#import "BNRContainer.h"

@implementation BNRContainer

- (id)initWithContainerName: (NSString *)contName
             containerArray: (NSMutableArray *)contArray
    containerValueInDollars: (int)contValue
{
    self = [super init];
    
    if (self)
    {
        [self setContainerName];
        [self setContainerArray:contArray];
        [self setContainerValueInDollars:contValue];
    }
    return self;
    
}

- (id)initWithItem: (BNRItem *)item
     containerName: (NSString *)contName
    containerArray: (NSMutableArray *)contArray;
{
    
    {
        [self setContainerName];
        [self setContainerArray:contArray];
        //[self addBNRItem:item toContainer:contArray];
    }
    
    return self;
    
}

- (void)addBNRItem:(BNRItem *)item toContainer:(NSMutableArray *)contArray
{
    [contArray addObject:item];
}

- (NSString *)description
{
    //Get the total value of the objects in the container
    int totalContainerValue = 0;
    for (BNRItem *item in containerArray)
    {
        totalContainerValue = totalContainerValue + [item valueInDollars];
    }
    
    //Print the container data
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"Container: %@ Worth $%d and Containing $%d \n",
                                                                   containerName,
                                                                   containerValueInDollars,
                                                                   totalContainerValue];
    //for (BNRItem *item in containerArray)
    for (int i = 0; i < [containerArray count]; i++)
    {
        BNRItem *currentItem = [containerArray objectAtIndex:i];
        descriptionString = [descriptionString stringByAppendingString:[NSString stringWithFormat:@"%@ (%@) %@ $%d \n",
                                                                        [currentItem itemName],
                                                                        [currentItem serialNumber],
                                                                        [currentItem dateCreated],
                                                                        [currentItem valueInDollars]]];
        
//        descriptionString = [descriptionString stringByAppendingString:[NSString stringWithFormat:@"%@ (%@) %@ $%d \n",
//                                                                        itemName,
//                                                                        serialNumber,
//                                                                        dateCreated,
//                                                                        valueInDollars ]];
    }
    
    
    return descriptionString;
}


//GETTERS AND SETTERS BELOW

- (void)setContainerName:(NSString *)contName
{
    containerName = contName;
}

- (NSString *)containerName
{
    return containerName;
}

- (void)setContainerValueInDollars:(int)contValue
{
    containerValueInDollars = contValue;
}

- (int)containerValueInDollars
{
    return containerValueInDollars;
}


- (void)setContainerArray:(NSMutableArray *)contArray
{
    containerArray = contArray;
}

- (NSMutableArray *)containerArray
{
    return containerArray;
}

@end

main() File

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

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        //Alloc and initialize a muttable array and a BNRItem. 
        NSMutableArray *theContainerArray = [[NSMutableArray alloc] init];
        BNRItem *item = [[BNRItem alloc] initWithItemName: @"The First Item"
                                           valueInDollars: 1000
                                             serialNumber: @"A1B2C3"];
        
        //Setup the container
        BNRContainer *c = [[BNRContainer alloc] initWithContainerName:@"My First Container"
                                                       containerArray:theContainerArray
                                              containerValueInDollars:5000];
        
        //Add the BNRItem, item to the container
        [c addBNRItem:item toContainer:theContainerArray];
        
        //Add a random item
        BNRItem *randomItem = [BNRItem randomItem];
        [c addBNRItem: randomItem toContainer:theContainerArray];
                           
//        BNRContainer *c = [[BNRContainer alloc] initWithItem:item
//                                               containerName:@"My First Container"
//                                              containerArray:theContainerArray];
        
        NSLog(@"%@", c);
        
        c = nil;
        theContainerArray = nil;
        randomItem = nil;

    }
 
    return 0;
}

#2

Thank your post, I get whole idea for this challenge until I read your post.


#3

Thanks, Mgpeng. Happy to help out.