My Gold Solution


#1

My solution will list the contents of any number of BNRContainers nested in other BNRContainer objects. I wasn’t sure whether the BNRContainer itself should have a serial number or creation date, so I had the designated initializer (initWithContainerName:) make those nil, and simply chose not to reference them in the description output for BNRContainers. Also, I wasn’t sure if the BNRContainer should have any inherent value of its own, so the initializer sets it to zero.

I used a class extension in BNRContainer.m to declare the subItems property because it seems to me that the array itself doesn’t really need to be public. The user can add items to it, and the container object can handle the array internally, but the user does not need direct access to its elements. (The specifications didn’t call for a “removeItem” type method, so I left that out.) Also, the listContentsOfContainer method is private (not declared in the .h file) because it only needs to be invoked within the overridden description method and in the listContentsOfContainer method itself.

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem

// designated initializer

  • (instancetype)initWithContainerName:(NSString *)name;

  • (instancetype)init;

  • (void)addItemToContainer:(BNRItem *)item;

@end[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@interface BNRContainer ()

@property (nonatomic, copy) NSMutableArray *subItems;

@end

@implementation BNRContainer

  • (instancetype)initWithContainerName:(NSString *)name
    {
    self = [super init];

    if (self) {
    _subItems = [[NSMutableArray alloc] init];
    _itemName = name;
    _serialNumber = nil;
    _valueInDollars = 0;
    _dateCreated = nil;
    }
    return self;
    }

  • (instancetype)init
    {
    return [self initWithContainerName:@“Container”];
    }

  • (void)addItemToContainer:(BNRItem *)item
    {
    [self.subItems addObject:item];
    self.valueInDollars += item.valueInDollars;
    }

// listContentsOfContainer does most of the work on behalf of description override

  • (NSString *)listContentsOfContainer
    {
    // This temporary mutable string holds all the info about the container’s contents
    // until it’s ready to be sent back to the description method
    NSMutableString *tempDescription = [[NSMutableString alloc] init];

    // start by appending the name of the container and total value of its contents
    [tempDescription appendFormat:@"\nContainer: %@ Value: $%d\n", self.itemName, self.valueInDollars];

    for (int i = 0; i < [self.subItems count]; i++) {
    // if item in container’s array is a BNRContainer, that container must also have
    // its contents detailed for our description, so call listContentsOfContainer on it
    // and append the resulting NSString to our tempDescription string
    // NOTE: THIS WILL ENSURE THAT ANY NUMBER OF NESTED CONTAINERS HAVE THEIR CONTENTS LISTED
    // FOR OUR DESCRIPTION
    if ([self.subItems[i] isKindOfClass:[BNRContainer class]]) {
    [tempDescription appendString:[self.subItems[i] listContentsOfContainer]];
    } else {
    // if the item in the container’s array is not a BNRContainer, it must be a BNRItem,
    // so we can simply append its item name, serial number, value, and date created
    // to the temporary string
    [tempDescription appendFormat:@"\tItem: %@ SN: %@ Value: $%d Date: %@\n",
    [self.subItems[i] itemName],
    [self.subItems[i] serialNumber],
    [self.subItems[i] valueInDollars],
    [self.subItems[i] dateCreated]];
    }
    }
    NSString *description = [tempDescription copy];
    return description;
    }

  • (NSString *)description
    {
    NSString *description = [self listContentsOfContainer];
    return description;
    }

@end
[/code]

main.m

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

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

@autoreleasepool {
    
    // containerOne holds containerTwo at the end of its array
    // containerTwo holds containerThree at the end of its array
    // This is to demonstrate that the listContentsOfContainer method
    // in BNRContainer.m handles nested BNRContainer objects
    
    BNRContainer *containerOne = [[BNRContainer alloc] initWithContainerName:@"Large Chest"];
    
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        [containerOne addItemToContainer:item];
    }
    
    BNRContainer *containerTwo = [[BNRContainer alloc] initWithContainerName:@"Small Chest"];
    
    for (int i = 0; i < 4; i++) {
        BNRItem *item = [BNRItem randomItem];
        [containerTwo addItemToContainer:item];
    }
    

    
    BNRContainer *containerThree = [[BNRContainer alloc] initWithContainerName:@"Medium Chest"];
    
    for (int i = 0; i < 7; i++) {
        BNRItem *item = [BNRItem randomItem];
        [containerThree addItemToContainer:item];
    }
    
    [containerTwo addItemToContainer:containerThree];
    [containerOne addItemToContainer:containerTwo];
    
    NSLog(@"%@", containerOne);
    
}
return 0;

}[/code]

Sample Output (“Small Chest” is in “Large Chest” and “Medium Chest” is in “Small Chest” … so the BNRContainers go down three levels, and all contents are listed with a single call to the description method on “Large Chest”)

[code]2014-04-21 21:05:34.539 RandomItems[1705:303]
Container: Large Chest Value: $984
Item: Fluffy Mac SN: 0G9A8 Value: $43 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Spork SN: 4D1D5 Value: $52 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Mac SN: 5R4R9 Value: $3 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Spork SN: 7M0B7 Value: $58 Date: 2014-04-21 12:05:34 +0000
Item: Rusty Bear SN: 2O0O1 Value: $46 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Bear SN: 6O4T8 Value: $81 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Spork SN: 8I1P8 Value: $40 Date: 2014-04-21 12:05:34 +0000
Item: Rusty Bear SN: 9Y1G7 Value: $22 Date: 2014-04-21 12:05:34 +0000
Item: Rusty Mac SN: 3R8O4 Value: $36 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Spork SN: 2K4V1 Value: $82 Date: 2014-04-21 12:05:34 +0000

Container: Small Chest Value: $521
Item: Rusty Spork SN: 8U3W4 Value: $55 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Mac SN: 1S3D3 Value: $63 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Spork SN: 7M4V9 Value: $2 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Spork SN: 7F8I7 Value: $49 Date: 2014-04-21 12:05:34 +0000

Container: Medium Chest Value: $352
Item: Rusty Spork SN: 3S5T9 Value: $19 Date: 2014-04-21 12:05:34 +0000
Item: Fluffy Mac SN: 5W9U6 Value: $36 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Mac SN: 4L0J3 Value: $86 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Bear SN: 2V4O2 Value: $9 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Mac SN: 7D4S2 Value: $40 Date: 2014-04-21 12:05:34 +0000
Item: Rusty Mac SN: 7K9Y5 Value: $68 Date: 2014-04-21 12:05:34 +0000
Item: Shiny Spork SN: 0U4C6 Value: $94 Date: 2014-04-21 12:05:34 +0000
Program ended with exit code: 0[/code]