Gold Challenge (THE FINAL SOLUTION, TESTED AND WORKING)


#1

Sorry for the marketing in the thread title, but I wanted other students to be aware that some solutions aren’t complete. What they are missing is to add the value of BNRContainers that are added to other BNRContainers.

for example, if we add 3 BNRItems and a BNRContainer with two BNRItems, then the total value is:
BNRContainer = BNRItem + BNRItem + BNRItem + BNRContainer[BNRItem + BNRItem]
note: this is not real code

I got a lot of help from existing solutions on this board so I wanted to give back in case this helps others.

A little tip: use a boolean check for the kind of class of the item being added to BNRContainer. One trouble I faced was that I checked BNRItem first, and BNRContainer second, and my code wasn’t working as I wanted it to. Why is that? Something to think about.

Also, I had to change the class in the .h and .m files to “id” for the method that adds new objects to the BNRcontainer

[code]//
// BNRContainer.h
// RandomItems
//

#import “BNRItem.h”

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

// designated initializer

  • (instancetype)initWithContainerName: (NSString *)cName
    containerItems:(NSMutableArray *)cItems;

  • (void) addItemToContainer:(id) newItem;

// getters and setters

  • (void) setContainerName:(NSString *)str;

  • (NSString *)containerName;

  • (void) setContainerItems:(NSMutableArray *)items;

  • (NSMutableArray *)containerItems;

  • (void) setContainerTotalValue:(int)v;

  • (int)containerTotalValue;

@end
[/code]

[code]//
// BNRContainer.m
// RandomItems

#import “BNRContainer.h”

@implementation BNRContainer

  • (instancetype)initWithContainerName: (NSString *)cName
    containerItems:(NSMutableArray *)cItems
    {
    self = [ super init ] ;

    if (self) {
    _containerName = cName;
    _containerItems = cItems;
    _containerTotalValue = 0;
    }
    return self;

}

// add BNRItem to our BNRContainer

  • (void) addItemToContainer:(id)newItem {

    // BNRContainer check has to go first because it will pass as a kind of BNRItem
    if ([newItem isKindOfClass: [BNRContainer class]]) {
    BNRContainer *itemToAdd = newItem;
    _containerTotalValue += itemToAdd.containerTotalValue;

      [_containerItems addObject:newItem];
    

    }

    else if ([newItem isKindOfClass: [BNRItem class]]) {

      BNRItem *itemToAdd = newItem;
      _containerTotalValue += itemToAdd.valueInDollars;
      
      [_containerItems addObject:newItem];
    

    }

    // else, unrecognized object
    else {
    NSLog(@“object not recognized. Not added to BNRContainer: %@”, _containerName);
    }

}

// getters and setters

  • (void) setContainerName:(NSString *)str {
    _containerName = str;
    }

  • (NSString *)containerName {
    return _containerName;
    }

  • (void) setContainerTotalValue:(int)v {
    _containerTotalValue = v;
    }

  • (int)containerTotalValue {
    return _containerTotalValue;
    }

  • (void) setContainerItems:(NSMutableArray *)items {
    _containerItems = items;
    }

  • (NSMutableArray *)containerItems {
    return _containerItems;
    }

  • (NSString *)description
    {
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@“Container name: %@ with total value: %d. Items include: %@”,
    self.containerName,
    self.containerTotalValue,
    self.containerItems];
    return descriptionString;
    }

@end
[/code]

main.m

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

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

@autoreleasepool {
    
    // create initial containerItems Array to initialize BNRContainer
    
    NSMutableArray *cItems1 = [[NSMutableArray alloc] init];
    
    BNRContainer *container = [[BNRContainer alloc] initWithContainerName:@"container 1" containerItems:cItems1];
    
    for (int i = 0; i < 10; i++) {
        BNRItem *randomItem = [BNRItem randomItem];
        
        // add object to BNRContainer
        [container addItemToContainer:randomItem];
        
    }
    
    
    
    // add a new BNRContainer to the current BNRContainer
    
    NSMutableArray *cItems2 = [[NSMutableArray alloc] init];
    
    BNRContainer *container2 = [[BNRContainer alloc] initWithContainerName:@"container 2" containerItems:cItems2];
    
    // add object to BNRContainer container 2
    BNRItem *randomItem2 = [BNRItem randomItem];
    [container2 addItemToContainer:randomItem2];
    
    
    // finally, add BNRContainer container2 to BNRContainer container1
    [container addItemToContainer:container2];
    
    
    // print our results
    NSLog(@"%@", container);
}
return 0;

}

[/code]

2014-06-29 23:17:29.699 RandomItems[1363:303] Container name: container 1 with total value: 668. Items include: ( "Fluffy Bear (0U4U8): Worth $5, recorded on 2014-06-30 03:17:21 +0000", "Rusty Spork (9E0J3): Worth $19, recorded on 2014-06-30 03:17:21 +0000", "Fluffy Spork (8K7G5): Worth $84, recorded on 2014-06-30 03:17:21 +0000", "Rusty Spork (9H3V6): Worth $86, recorded on 2014-06-30 03:17:21 +0000", "Rusty Mac (7N1J0): Worth $14, recorded on 2014-06-30 03:17:21 +0000", "Shiny Mac (1E1H2): Worth $81, recorded on 2014-06-30 03:17:21 +0000", "Shiny Bear (1M7G9): Worth $83, recorded on 2014-06-30 03:17:21 +0000", "Shiny Spork (2Y8C2): Worth $51, recorded on 2014-06-30 03:17:21 +0000", "Rusty Bear (1B8U8): Worth $81, recorded on 2014-06-30 03:17:21 +0000", "Fluffy Mac (1J7K6): Worth $77, recorded on 2014-06-30 03:17:21 +0000", "Container name: container 2 with total value: 87. Items include: (\n \"Shiny Mac (9L6P7): Worth $87, recorded on 2014-06-30 03:17:21 +0000\"\n)" ) Program ended with exit code: 0

>>> 5 + 19 + 84 + 86 + 14 + 81 + 83 + 51 + 81 + 77 + 87 668