Gold Challenge Solution - Can someone check please?


#1

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem

{
NSString *_conName;
int _valueInDolls;
NSArray *_arrai;
}

  • (instancetype)newMethod;
  • (instancetype)initWithContainer:(NSString *)name
    valueInDolls:(int)value
    subItems:(NSArray *)sItems;

  • (void)setConName:(NSString *)name;

  • (NSString *)conName;

  • (void)setValueInDolls:(int)v;

  • (int)valueInDolls;

  • (void)setArray:(NSArray *)arrai;

  • (NSArray *)arrai;

@end[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (void)setConName:(NSString *)name
    {
    _conName = name;
    }

  • (NSString *)conName
    {
    return _conName;
    }

  • (void)setValueInDolls:(int)v
    {
    _valueInDolls = v;
    }

  • (int)valueInDolls
    {
    return _valueInDolls;
    }

  • (void)setArray:(NSArray *)arrai
    {
    _arrai = arrai;
    }

  • (NSArray *)arrai
    {
    return _arrai;
    }

  • (instancetype)newMethod
    {
    NSString *name = [NSString stringWithFormat:@"%c%@",
    ‘R’ + arc4random() % 10, @“affi”];

    NSMutableArray *values = [[NSMutableArray alloc] init];

    int sum = 0;
    for (int i = 0; i < 10; i++) {
    BNRItem *value = [BNRItem randomItem];
    sum += value.valueInDollars;
    [values addObject:value];
    }

    sum *=2;

    BNRContainer *container = [[BNRContainer alloc] initWithContainer:name valueInDolls:sum subItems:values];

    return container;

}

  • (instancetype)initWithContainer:(NSString *)name valueInDolls:(int)value subItems:(NSArray *)sItems
    {
    self = [super init];

    if (self) {
    _conName = name;
    _valueInDolls = value;
    _arrai = sItems;
    }

    return self;
    }

  • (NSString *)description
    {
    NSString *def = [NSString stringWithFormat:@"%@ is a container: with value: %d, and subItems: %@", self.conName, self.valueInDolls, self.arrai];
    return def;
    }

@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 sum = 0;
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        sum += item.valueInDollars;
        [items addObject:item];
    }
    
    sum = sum * 2;
    
    for (BNRItem *item in items) {
        NSLog(@"%@", item);
    }
    
    NSLog(@"\n\n\n\n");
    
    BNRContainer *con = [BNRContainer newMethod];        
    NSLog(@"%@", con);
    
    items = nil;
}
return 0;

}

[/code]