Gold Challenge Solution


#1

Discussion desired, constructive criticism welcome, nonconstructive criticism tolerated…

BNRContainer.h

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSMutableArray *_subItems;
}

- (instancetype)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber numberOfRandomItems:(int)numRandomItems;

- (NSMutableArray *)subItems;
- (void)setSubItems:(NSArray *)subItems;

- (void)addSubItem:(BNRItem *)item;

@end

BNRContainer.m

#import "BNRContainer.h"

@interface BNRContainer()

- (int)valueOfContentsInDollars;
- (NSString *)contentDescriptions;

@end

@implementation BNRContainer

- (instancetype) initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber numberOfRandomItems:(int)numRandomItems
{
    self = [super initWithItemName:name valueInDollars:value serialNumber:sNumber];
    
    if (self)
    {
        for (int i = 0; i < numRandomItems; i++) {
            [self addSubItem:[BNRItem randomItem]];
        }
    }
    
    return self;
}

- (NSMutableArray *)subItems
{
    if (!_subItems)
    {
        _subItems = [[NSMutableArray alloc] init];
    }
    
    return _subItems;
}
- (void)setSubItems:(NSArray *)subItems
{
    [self.subItems addObjectsFromArray];
}

- (void)addSubItem:(BNRItem *)item
{
    [self.subItems addObject:item];
}

- (int)valueOfContentsInDollars
{
    int sum = 0;
    
    for (id item in self.subItems)
    {
        if ([item respondsToSelector:@selector(valueOfContentsInDollars)])
        {
            sum += [item valueOfContentsInDollars];
        }
        
        if ([item respondsToSelector:@selector(valueInDollars)])
        {
            sum += [item valueInDollars];
        }
    }
    
    return sum;
}

- (NSString *)contentDescriptions
{
    NSMutableString *descriptions = [[NSMutableString alloc] init];
    
    for (id item in self.subItems)
    {
        [descriptions appendFormat:@"%@\n", item];
    }
    
    return descriptions;
}

- (NSString *)description
{
    NSString *desc = [NSString stringWithFormat:@"%@, Value: $%d, Value of Contents: $%d, Total Value: $%d, Contains: [\n%@]",
                      self.itemName,
                      self.valueInDollars,
                      [self valueOfContentsInDollars],
                      self.valueInDollars + [self valueOfContentsInDollars],
                      [self contentDescriptions]];
    return desc;
}

@end

main.m

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

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

    @autoreleasepool {
        
        BNRContainer *container = [[BNRContainer alloc] initWithItemName:@"Gold Box"
                                                           valueInDollars:150
                                                             serialNumber:@"GB100"
                                                     numberOfRandomItems:5];
        
        BNRContainer *subContainer = [[BNRContainer alloc] initWithItemName:@"Silver Box"
                                                             valueInDollars:100
                                                               serialNumber:@"SB100"
                                                        numberOfRandomItems:3];
        
        [container addSubItem:subContainer];
        
        NSLog(@"%@", container);
    }
    return 0;
}