My gold solution


#1

I tried to make the BNRContainer object as simple as possible by only adding the NSMutableArray to hold multiple BNRItems (either containers or items), overriding the description (per the instructions in the book) and overriding valueInDollars to add up any containers and items held in the container.

#import "BNRItem.h"
@interface BNRContainer : BNRItem
{
    NSMutableArray *_containerItems;
}
- (void)addItem:(BNRItem *)item;
@end

BNRContainer class has a NSMutableArray and a method to add items to it.

#import "BNRContainer.h"
@implementation BNRContainer
// Add BNRItems or BNRContainers to the object array
- (void)addItem:(BNRItem *)item
{
    if (!_containerItems) {
        _containerItems = [[NSMutableArray alloc] init];
    }
    [_containerItems addObject:item];
}
// Override of valueInDollars
// BNRItems will return valueInDollars
// BNRContainers will call this method, recursively moving through containers
// until BNRItems are encountered and add those to X
// _valueInDollars also captures the container value itself
- (int)valueInDollars
{
    int x = 0;
    for (BNRItem *temp in _containerItems) {
        x = x + temp.valueInDollars;
    }
    x = x + _valueInDollars;
    return x;
}
// Override class description per book instructions
- (NSString *)description
{
    for (BNRItem *temp in _containerItems) {
        NSLog(@"%@", temp);
    }
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@: Worth %d",
                                   self.itemName,
                                   self.valueInDollars];
    return descriptionString;
}
@end

[code]
#import <Foundation/Foundation.h>
#import “BNRContainer.h”

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

@autoreleasepool {
    
    // Create one BNRContainer object
    BNRContainer *myContainer = [[BNRContainer alloc] initWithName:@"My Container 1" valueInDollars:5 serialNumber:@"Cont1"];
    
    // Fill container 1 with random items
    for (int i = 0; i < 2; i++) {
        BNRItem *item = [BNRItem randomItem];
        [myContainer addItem:item];
    }

    // Create another BNRContainer object
    BNRContainer *myContainer2 = [[BNRContainer alloc] initWithName:@"My Container 2" valueInDollars:15 serialNumber:@"Cont 2"];
    
    // Fill container 2 with random items
    for (int i = 0; i < 2; i++) {
        BNRItem *item = [BNRItem randomItem];
        [myContainer2 addItem:item];
    }

    // Create a "large" container and add the first containers to it
    BNRContainer *largerContainer = [[BNRContainer alloc] initWithName:@"Large Container!" valueInDollars:100 serialNumber:@"Cont 3"];
    [largerContainer addItem:myContainer];
    [largerContainer addItem:myContainer2];
    

    // Create a "super" container with the "large" container and random items
    BNRContainer *superContainer = [[BNRContainer alloc] initWithItemName:@"Super Container!"];
    [superContainer addItem:largerContainer];
    for (int i = 0; i < 2; i++) {
        BNRItem *item = [BNRItem randomItem];
        [superContainer addItem:item];
    }
    
    // Print description of the super container
    NSLog(@"%@", superContainer);
    
    // Validation that super container responds to BNRItem methods
    NSLog(@"%@ with serial number (%@) is worth %d and was created on %@", superContainer.itemName, superContainer.serialNumber, superContainer.valueInDollars, superContainer.dateCreated);
    
    // Destroy the mutable array object
    myContainer = nil;
    
}
return 0;

}[/code]

The output could be improved but it covers all the bases for now. The items are listed for each container, then the container, starting with the “inner most” container…

2014-04-22 20:54:52.850 RandomItems[37448:303] Rusty Mac (7U0J1): Worth 78, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.850 RandomItems[37448:303] Rusty Spork (2T1R2): Worth 31, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.851 RandomItems[37448:303] My Container 1: Worth 114 2014-04-22 20:54:52.851 RandomItems[37448:303] Rusty Mac (1G2E6): Worth 82, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.851 RandomItems[37448:303] Shiny Mac (9K0V3): Worth 30, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.852 RandomItems[37448:303] My Container 2: Worth 127 2014-04-22 20:54:52.852 RandomItems[37448:303] Large Container!: Worth 341 2014-04-22 20:54:52.852 RandomItems[37448:303] Rusty Mac (6E2R6): Worth 8, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.853 RandomItems[37448:303] Fluffy Mac (1M6I0): Worth 9, recorded on 2014-04-23 03:54:52 +0000 2014-04-22 20:54:52.853 RandomItems[37448:303] Super Container!: Worth 358 2014-04-22 20:54:52.854 RandomItems[37448:303] Super Container! with serial number (None) is worth 358 and was created on 2014-04-23 03:54:52 +0000 Program ended with exit code: 0