My take on the Gold Challenge


#1

BNRItem class: unchanged

BNRContainer class:

BNRContainer.h:

//
//  BNRContainer.h
//  RandomPossessions
//

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSMutableArray *subitems; // Contains instances of BNRItem
    NSString       *name;
    int             containerValueInDollars;
}

@property (assign) int containerValueInDollars;

- (id)initWithName:(NSString *)theName containerValueInDollars:(int)contValue itemsInContainer:(NSMutableArray *)theItems;

@end

BNRContainer.m:

//
//  BNRContainer.m
//  RandomPossessions
//

#import "BNRContainer.h"

@implementation BNRContainer

@synthesize containerValueInDollars;

- (id)initWithName:(NSString *)theName containerValueInDollars:(int)contValue itemsInContainer:(NSMutableArray *)theItems
{
    self = [super init];
    
    if (self) {
        name = [theName copy];
        containerValueInDollars = contValue;
        subitems = [theItems copy];
        if (subitems) {
            NSLog(@"Container initialized with items: %@\n\n", subitems);
        }
        else {
            NSLog(@"Nothing added to container.");
        }
    }
    
    return self;
}

- (NSString *)description
{   
    NSString *itsName = [NSString stringWithFormat:@"\nContainer name: %@\n", name];
    NSString *itsValue = [NSString stringWithFormat:@"Container value: $%i\n", containerValueInDollars];
    
    int contentsTotalValueInt = 0;
    
    for (id item in subitems) {
        if ([item class] == [BNRItem class]) {
            contentsTotalValueInt += [item valueInDollars];
        }
        if ([item class] == [self class]) {
            contentsTotalValueInt += self.containerValueInDollars;
        }
    }
    
    NSString *contentsTotalValueString = [NSString stringWithFormat:@"Total value of contents in container: $%i\n", contentsTotalValueInt];
    
    NSString *desc = [itsName stringByAppendingString];
    desc = [desc stringByAppendingString:contentsTotalValueString];
    
    for (id item in subitems) {
        desc = [desc stringByAppendingString:[item description]];
        desc = [desc stringByAppendingString:@"\n"];
    }
    
    return desc;
}

@end

main.m:

//
//  main.m
//  RandomPossessions
//

#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 addess in items variable
        NSMutableArray *items = [[NSMutableArray alloc] init];
        
        for (int i = 0 ; i < 10 ; i++) {
            BNRItem *p = [BNRItem randomItem];
            [items addObject:p];
        }
        
        BNRContainer *container = [[BNRContainer alloc] initWithName:@"Container" containerValueInDollars:25 itemsInContainer:items];
        
        NSLog(@"%@", container);
       
        items = nil;
        
    }
    return 0;
}

Output:

2012-07-17 18:45:22.458 RandomPossessions[97458:403] Container initialized with items: (
    "Rusty Spork (8Q2U8): Worth $73, recorded on 2012-07-17 23:45:22 +0000",
    "Shiny Spork (5Y2V3): Worth $40, recorded on 2012-07-17 23:45:22 +0000",
    "Rusty Spork (2F9Z7): Worth $40, recorded on 2012-07-17 23:45:22 +0000",
    "Rusty Bear (8G5V6): Worth $99, recorded on 2012-07-17 23:45:22 +0000",
    "Shiny Spork (3P9B1): Worth $10, recorded on 2012-07-17 23:45:22 +0000",
    "Rusty Mac (6R5C1): Worth $93, recorded on 2012-07-17 23:45:22 +0000",
    "Fluffy Spork (3E4O0): Worth $1, recorded on 2012-07-17 23:45:22 +0000",
    "Fluffy Mac (3A6T4): Worth $30, recorded on 2012-07-17 23:45:22 +0000",
    "Shiny Spork (8S3I1): Worth $77, recorded on 2012-07-17 23:45:22 +0000",
    "Rusty Spork (4F6F9): Worth $65, recorded on 2012-07-17 23:45:22 +0000"
)

2012-07-17 18:45:22.462 RandomPossessions[97458:403] 
Container name: Container
Container value: $25
Total value of contents in container: $528
Rusty Spork (8Q2U8): Worth $73, recorded on 2012-07-17 23:45:22 +0000
Shiny Spork (5Y2V3): Worth $40, recorded on 2012-07-17 23:45:22 +0000
Rusty Spork (2F9Z7): Worth $40, recorded on 2012-07-17 23:45:22 +0000
Rusty Bear (8G5V6): Worth $99, recorded on 2012-07-17 23:45:22 +0000
Shiny Spork (3P9B1): Worth $10, recorded on 2012-07-17 23:45:22 +0000
Rusty Mac (6R5C1): Worth $93, recorded on 2012-07-17 23:45:22 +0000
Fluffy Spork (3E4O0): Worth $1, recorded on 2012-07-17 23:45:22 +0000
Fluffy Mac (3A6T4): Worth $30, recorded on 2012-07-17 23:45:22 +0000
Shiny Spork (8S3I1): Worth $77, recorded on 2012-07-17 23:45:22 +0000
Rusty Spork (4F6F9): Worth $65, recorded on 2012-07-17 23:45:22 +0000