Here is my solution to the Gold Challenge


#1

BNRContainer.h

[code]

#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSMutableArray *arrayOfContainerItems; //May contain other BNRContainers and/or BNRItems
NSString *containerName;
int containerValue;
}

-(id) initWithContainerName: (NSString *) aContainerName;

-(void) addItem: (BNRItem *) anItem;
-(void) addAContainer: (BNRContainer *) aContainer;

-(NSMutableArray *) arrayOfContainerItems;
-(void) setArrayOfContainerItems: (NSMutableArray *) anArray;

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

-(int) containerValue;
-(void) setContainerValue: (int) aValue;

@end[/code]

BNRContainer.m


#import "BNRContainer.h"

@implementation BNRContainer

-(id) initWithContainerName: (NSString *) aContainerName
{
    //Call the superclass's designated initializer
    self = [super init];
    
    [self setContainerName: aContainerName];
    [self setContainerValue: 0]; //Initialize the container to value to 0
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setArrayOfContainerItems:array];
    
    return self;
}

-(NSString *) description
{
    //Since the container description string that is returned is displayed last, I return an empty string and do all the string formating in the description method
    
    //First -- display the name and value of the container
    NSLog(@"\n\tContainers name: %@\n\tContainer value: $%d", [self containerName], [self containerValue]);
    
    
    for (int i = 0; i < [[self arrayOfContainerItems] count]; i++)  //iterate through the items in the container
    {
        id item = [arrayOfContainerItems objectAtIndex:i];
        NSLog(@"\n\t\t%@", [item description]);  //If item is a BNRItem it will display the BNRItem description and if it is a BNRContainer it will display the BNRContainer description and look inside the container for more items or containers.
    }
    NSString *descriptionString = [[NSString alloc] initWithString:@""];  //return an empty string
    return descriptionString;
}

-(void) addItem: (BNRItem *) anItem 
{
    [[self arrayOfContainerItems] addObject:anItem]; //Add the item to the Container array
    
   [self setContainerValue: [self containerValue] + [anItem valueInDollars]];
}

-(void) addAContainer: (BNRContainer *) aContainer
{
    [[self arrayOfContainerItems] addObject: aContainer]; //Add the container to the Container array
    
    [self setContainerValue: [self containerValue] + [aContainer containerValue]];
}

-(NSMutableArray *) arrayOfContainerItems
{
    return arrayOfContainerItems;

}


-(void) setArrayOfContainerItems: (NSMutableArray *) anArray
{
    arrayOfContainerItems = anArray;
}

-(NSString *) containerName
{
    return containerName;

}

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

-(int) containerValue
{
    return containerValue;
}

-(void) setContainerValue: (int) aValue
{
    containerValue = aValue;
}

@end

main.m

[code]

#import <Foundation/Foundation.h>
#import “BNRItem.h”
#import “BNRContainer.h”

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

@autoreleasepool {
    //Create a mutable array object, store its address in the items variable.
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    //Create 3 containers all of which will hold item. But store container1 and container2 in container3 with 10 items
    BNRContainer *container1 = [[BNRContainer alloc]initWithContainerName: @"First SubContainer"];
    BNRContainer *container2 = [[BNRContainer alloc]initWithContainerName: @"Second SubContainer"];
    BNRContainer *container3 = [[BNRContainer alloc]initWithContainerName: @"Main Container"];
    
    //Put 10 items in container1
     for (int i = 0; i < 10; i++)
    {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container1 addItem:p];
    }
    
    //Put 10 items in container2
    for (int i = 0; i < 10; i++)
    {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container2 addItem:p ];
    }
    
    //Put container1 into container3
    [container3 addAContainer:container1];
    
    //Add 10 items to container3
    for (int i = 0; i < 10; i++)
    {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container3 addItem:p];
    }
    
    //Put container2 into container3
    [container3 addAContainer:container2]; 
    
    NSLog(@"%@", container3);  //Call the description method for container3 and display all its contents
    
    NSLog (@"\n\n\n\n");  //Put a separator between the container display code and the earlier item display code
    
    for (BNRItem *item in items)
    {
        NSLog (@"%@", [item description]);  //Call the description method
    }
    NSLog (@"\n\n");
    for (int i = 0; i < [items count]; i++) {
        // BNRItem *item = [items objectAtIndex: i];
        NSLog(@"%@", [items objectAtIndex: i]);
    }
    
    //Destroy the array pointed to by the items variable
    NSLog (@"\n\n\n\n***************SETTING ITEM TO NIL**************\n\n\n\n");
    items = nil;   
}
return 0;

}[/code]


#2

You don’t need to add extra containerName and containerValue variables. Since BNRContainer derives from BNRItem, you can use the itemName and valueInDollars from BNRItem.