Gold Challenge Solution - add the add: and insert: method


#1

Beyond the requirement, I added two more common NSMutableArray method - addItemToContainer: and insertItem:atIndex: to my BNRContainer.

Hope u guys can give me some suggestion~ :smiley:

Here’s my solution:

BNRContainer.h

[code]#import “BNRItem.h”

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

  • (instancetype)initWithContainerName:(NSString *)cName
    containerArray:(NSMutableArray *)cArray
    containerTotalValue:(int)cTotalValue;

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

  • (void)insertItem:(BNRItem *)iItem
    atIndex:(int)i;

@end[/code]

BNRContainer:

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (instancetype)initWithContainerName:(NSString *)cName
    containerArray:(NSMutableArray *)cArray
    containerTotalValue:(int)cTotalValue
    {
    self = [super init];

    if (self) {
    _containerName = cName;
    _containerArray = cArray;
    _containerTotalValue = cTotalValue;
    }

    return self;
    }

  • (void)addItemToContainer:(BNRItem *)addtionalitem
    {
    [_containerArray addObject:addtionalitem];
    _containerTotalValue += addtionalitem.valueInDollars;
    }

  • (void)insertItem:(BNRItem *)iItem
    atIndex:(int)i
    {
    [_containerArray insertObject:iItem atIndex:i];
    _containerTotalValue += iItem.valueInDollars;
    }

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"\nThe container name is %@,\nand the value in total is %d,\nand the list is %@.", _containerName, _containerTotalValue, _containerArray];
    return descriptionString;
    }

@end[/code]

main.m

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

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

@autoreleasepool {
    
    NSMutableArray *items = [[NSMutableArray alloc] init];
    int totalValue = 0;
    
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        totalValue += item.valueInDollars;
        [items addObject:item];
    }
    
    BNRContainer *firstContainer = [[BNRContainer alloc] initWithContainerName:@"1st container"
                                                                containerArray:items
                                                           containerTotalValue:totalValue];
    NSLog(@"%@", firstContainer);
    
    BNRItem *addItem = [BNRItem randomItem];
    
    [firstContainer addItemToContainer:addItem];
    
    NSLog(@"%@", firstContainer);
    
    BNRItem *insertItem = [BNRItem randomItem];
    
    [firstContainer insertItem:insertItem atIndex:0];
    
    NSLog(@"%@", firstContainer);
    
}
return 0;

}[/code]

Console

2014-03-07 17:51:31.347 RandomItems[3818:303] The container name is 1st container, and the value in total is 502, and the list is ( "Fluffy Spork (2F2E7): Worth $68, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (1M5D5): Worth $10, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (4S4V7): Worth $97, record on 2014-03-07 09:51:31 +0000", "Rusty Bear (0Y6A8): Worth $7, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (9H9D4): Worth $59, record on 2014-03-07 09:51:31 +0000", "Fluffy Bear (9D0D7): Worth $6, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (2D7H4): Worth $34, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (1Y1S7): Worth $74, record on 2014-03-07 09:51:31 +0000", "Shiny Bear (3B2Q8): Worth $70, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (4J5X6): Worth $77, record on 2014-03-07 09:51:31 +0000" ). 2014-03-07 17:51:31.348 RandomItems[3818:303] The container name is 1st container, and the value in total is 594, and the list is ( "Fluffy Spork (2F2E7): Worth $68, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (1M5D5): Worth $10, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (4S4V7): Worth $97, record on 2014-03-07 09:51:31 +0000", "Rusty Bear (0Y6A8): Worth $7, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (9H9D4): Worth $59, record on 2014-03-07 09:51:31 +0000", "Fluffy Bear (9D0D7): Worth $6, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (2D7H4): Worth $34, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (1Y1S7): Worth $74, record on 2014-03-07 09:51:31 +0000", "Shiny Bear (3B2Q8): Worth $70, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (4J5X6): Worth $77, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (8W9P9): Worth $92, record on 2014-03-07 09:51:31 +0000" ). 2014-03-07 17:51:31.349 RandomItems[3818:303] The container name is 1st container, and the value in total is 613, and the list is ( "Fluffy Mac (8H3W5): Worth $19, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (2F2E7): Worth $68, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (1M5D5): Worth $10, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (4S4V7): Worth $97, record on 2014-03-07 09:51:31 +0000", "Rusty Bear (0Y6A8): Worth $7, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (9H9D4): Worth $59, record on 2014-03-07 09:51:31 +0000", "Fluffy Bear (9D0D7): Worth $6, record on 2014-03-07 09:51:31 +0000", "Shiny Mac (2D7H4): Worth $34, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (1Y1S7): Worth $74, record on 2014-03-07 09:51:31 +0000", "Shiny Bear (3B2Q8): Worth $70, record on 2014-03-07 09:51:31 +0000", "Shiny Spork (4J5X6): Worth $77, record on 2014-03-07 09:51:31 +0000", "Fluffy Spork (8W9P9): Worth $92, record on 2014-03-07 09:51:31 +0000" ). Program ended with exit code: 0