Solution to Gold Challenge


#1

Here’s my solution:

[code]#import “BNRItem.h”

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

  • (instancetype)initWithName:(NSString *)name numberofBNRItems:(int)number;

  • (int)containerValueInDollars;

  • (NSMutableArray *)listOfItems;

@end
[/code]

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (instancetype)initWithName:(NSString *)name numberofBNRItems:(int)number
    {
    self = [super init];

    if (self) {

      _containerName = name;
      
      for (int i = 0; i < number; i++) {
          [self.listOfItems addObject:[[self class] randomItem]];
      }
      NSLog(@"%@", self.listOfItems);
    

    }
    return self;
    }

  • (int)containerValueInDollars
    {
    int value = 0;
    for (BNRItem *item in self.listOfItems) {
    value += item.valueInDollars;
    }
    return value;
    }

  • (NSMutableArray *)listOfItems
    {
    if (!_listOfItems) {
    _listOfItems = [[NSMutableArray alloc] init];
    }
    return _listOfItems;
    }

  • (void)setListOfItems:(NSMutableArray *)list
    {
    _listOfItems = list;
    }

  • (NSString *)containerName
    {
    if (!_containerName) {
    _containerName = [[NSString alloc] init];
    }
    return _containerName;
    }

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

  • (NSString *)description
    {
    return [NSString stringWithFormat:@“Name: %@, value: %d, list of items: %@”,
    self.containerName,
    self.containerValueInDollars,
    self.listOfItems];
    }

@end[/code]

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

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

@autoreleasepool {
    
    NSMutableArray *items = [[NSMutableArray alloc]init];
 
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        [items addObject:item];
    }

    
    for (BNRItem *item in items) {
        NSLog(@"%@", item);
    }
    
    BNRContainer *cointeiter = [[BNRContainer alloc]
                                initWithName:@"first container"                          numberofBNRItems:5];
    NSLog(@"%@",[cointeiter description]);
    
    cointeiter = nil;
    items = nil;
    
}
return 0;

}[/code]

Any comments are welcomed


#2

Your BNRContainer.h is missing the containerName setter and getter declaration i.e.

BNRContainer.h

[code]- (NSString *)containerName;

  • (void)setContainerName:(NSString *)name;[/code]

Other then that it looks fine.


#3

Duplicating instance variables and methods in the superclass are not needed. Here’s my solution, fwiw. It’s a pleasure to share thoughts with everyone.

#import "BNRItem.h"

@interface BNRContainer : BNRItem

{
    NSMutableArray *_containedItems;
}

// Designated initialzer for BNRContainer
- (instancetype)initWithContainerName:(NSString *)name
                       valueInDollars:(int)value;

- (NSMutableArray *)containedItems;

- (int)totalValueInDollars;

@end
#import "BNRContainer.h"

@implementation BNRContainer

- (instancetype)initWithContainerName:(NSString *)name
                       valueInDollars:(int)value
{
    self = [super initWithItemName:name
                   valueInDollars:value
                     serialNumber:@""];
    
    if (self) {
        _containedItems = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (NSMutableArray *)containedItems
{
    return _containedItems;
}

- (int)totalValueInDollars
{
    int valueOfSubitems = 0;
    
    for (BNRItem *item in self.containedItems) {
        valueOfSubitems += item.valueInDollars;
    }
    
    return (valueOfSubitems + self.valueInDollars);
}

- (NSString *)description
{
    NSString *descriptionString = [NSString stringWithFormat:@"%@: Worth $%d\n%@",
                                         self.itemName,
                                        [self totalValueInDollars],
                                        [self.containedItems description]];
    
    return descriptionString;
}

@end
#import <Foundation/Foundation.h>
#import "BNRContainer.h"

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

    @autoreleasepool {
        
        NSMutableArray *items = [[NSMutableArray alloc] init];
        
        for (int i=0; i<10; i++) {
            items[i] = [BNRItem randomItem];
        }
        
        BNRContainer *container = [[BNRContainer alloc] initWithContainerName:@"My Container"
                                                               valueInDollars:50];
        
        for (BNRItem *item in items) {
            [container.containedItems addObject:item];
        }
        
        NSLog(@"%@", container);
        
        items = nil;
        container = nil;
        
    }
    return 0;
}