Gold Challenge Solutions


#1

So here’s my solution. main.m might be a little long-winded, but I set up multiple containers and loaded them up with items so that I could check that the main container can hold other containers and take their ultimate value into account when computing its own loaded value. Probably not exactly what we were supposed to do, but the report that prints does cover all the bases.

BNRContainer.h

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSString *_containerName;
    int _containerValueInDollars;
    int _containerTotalValue;
}

-  (NSMutableArray *)loadContainer:(NSString *)containerName
                       withPayload:(NSMutableArray *)a;

// Designated Initializer
- (instancetype)initWithContainerName:(NSString *)containerName
                       containerValue:(int)containerValue;

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

- (void)setContainerValueInDollars:(int)cv;
- (int)containerValueInDollars;

- (void)setContainerTotalValue:(int)tv;
- (int)containerTotalValue;

@end

BNRContainer.m

#import "BNRContainer.h"

@implementation BNRContainer

// Designated initializer
- (instancetype)initWithContainerName:(NSString *)containerName
                       containerValue:(int)containerValue {
    self = [super init];
    if (self) {
        _containerName = containerName;
        _containerValueInDollars = containerValue;
    }
    return self;
}

// Create a container that holds an array
-  (NSMutableArray *)loadContainer:(NSString *)containerName
                       withPayload:(NSMutableArray *)a {
    NSMutableArray *container = [[NSMutableArray alloc] initWithArray:a];
    return container;
}

- (NSString *)description {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"The %@ container, empty value: $%d, loaded value: $%d",
                                   self.containerName,
                                   self.containerValueInDollars,
                                   self.containerTotalValue];
    return descriptionString;
}

- (void)setContainerName:(NSString *)n {
    _containerName = n;
}

- (NSString *)containerName {
    return _containerName;
}

- (void)setContainerValueInDollars:(int)cv {
    _containerValueInDollars = cv;
}

- (int)containerValueInDollars {
    return _containerValueInDollars;
}

- (void)setContainerTotalValue:(int)tv {
    _containerTotalValue = tv;
}

- (int)containerTotalValue {
    return _containerTotalValue + _containerValueInDollars;
}

@end

main.m

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

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

    @autoreleasepool {
        
        // Each "container" incorporates a mutable array that holds x number of arrays, each of
        // which contain 1 - some number of BNRItem objects.
        // A "container" can also hold other containers, each of which may optionally hold arrays
        // that contain a random number of BNRItem objects.

        // Create an array to hold containers
        NSMutableArray *c = [[NSMutableArray alloc] init];
        
        // Create instances of BNRContainer (the "container" object)
        [c addObject:[[BNRContainer alloc] initWithContainerName:@"Big Barrel"
                                                  containerValue:1500]];
        [c addObject:[[BNRContainer alloc] initWithContainerName:@"JimBob Crate"
                                                  containerValue:1000]];
        [c addObject:[[BNRContainer alloc] initWithContainerName:@"Annie's Run"
                                                  containerValue:2000]];
        
        // Initialize x number of arrays that will contain BNRItem objects
        NSMutableArray *items_1 = [[NSMutableArray alloc] init];
        NSMutableArray *items_2 = [[NSMutableArray alloc] init];
        NSMutableArray *items_3 = [[NSMutableArray alloc] init];
        NSMutableArray *items_4 = [[NSMutableArray alloc] init];
        NSMutableArray *items_5 = [[NSMutableArray alloc] init];
        NSMutableArray *items_6 = [[NSMutableArray alloc] init];
        NSMutableArray *items_7 = [[NSMutableArray alloc] init];
        NSMutableArray *items_8 = [[NSMutableArray alloc] init];
        NSMutableArray *items_9 = [[NSMutableArray alloc] init];
        
        // Create a payloads array
        NSMutableArray *payloads = [[NSMutableArray alloc] init];
        
        // Initialize array that will be used to load individual instances of BNRContainer
        [payloads addObject:[NSMutableArray arrayWithObjects:items_1, items_2, items_3, c[1], c[2], nil]];
        [payloads addObject:[NSMutableArray arrayWithObjects:items_4, items_5, items_6, nil]];
        [payloads addObject:[NSMutableArray arrayWithObjects:items_7, items_8, items_9, nil]];
        
        // Create a random number of items and add them to the "itemsToLoad" array
        for (NSMutableArray *collections in payloads) {
            for (id items in collections) {
                if ([items isKindOfClass:[NSMutableArray class]]) {
                    for (int i = 0; i < (arc4random() % 10) + 1; i++) {
                        BNRItem *item = BNRItem.randomItem;
                        items[i] = item;
                    }
                }
            }
        }
        
        // Create a subitems array
        NSMutableArray *subitems = [[NSMutableArray alloc] init];
        
        // Load the payloads in subitems.
        subitems[0] = [c[0] loadContainer:@"Big Barrel" withPayload:payloads[0]];
        // Then load any other containers that will be loaded in the main container
        subitems[1] = [c[1] loadContainer:@"JimBob Crate" withPayload:payloads[1]];
        subitems[2] = [c[1] loadContainer:@"Annie's Run" withPayload:payloads[2]];

        /*
         c objects (BNRCo) contain subitems (NSMA)
         subitems contain payloads (NSMA)
         payloads contain collections (NSMA) (item_1, c[x], item_2, etc.)
         collections contain items [fluffy bunny, etc.]
         */
        
        int loadedValue = 0;
        
        for (int i = ((int)subitems.count - 1); i > -1; i--) {
            int containerLoadedValue = 0;
            for (id collections in payloads[i]) {
                NSLog(@"\nNew Collection...\n");
                if ([collections isKindOfClass:[BNRContainer class]]) {
                    containerLoadedValue = containerLoadedValue + [(BNRContainer *)collections containerTotalValue];
                    NSLog(@"%@",collections);
                } else {
                    if ([collections isKindOfClass:[NSMutableArray class]]) {
                        for (BNRItem *item in collections) {
                            containerLoadedValue = containerLoadedValue + [item valueInDollars];
                            NSLog(@"%@", item);
                        }
                    }
                }
            }
            [c[i] setContainerTotalValue:containerLoadedValue];
            loadedValue = loadedValue + containerLoadedValue;
            NSLog(@"\nContainer Info...\n");
            NSLog(@"Total Value of Container %@ is $%d\n\n", [c[i] containerName], [c[i] containerTotalValue]);
        }
        
        // Delete arrays
        
        items_1 = nil;
        items_2 = nil;
        items_3 = nil;
        items_4 = nil;
        items_5 = nil;
        items_6 = nil;
        items_7 = nil;
        items_8 = nil;
        items_9 = nil;
        c = nil;
        payloads = nil;
        subitems = nil;
        
    }
    return 0;
}