Gold challenge - extending initWith... into BNRContainer


#1

Hello,

Initially I created variables for BNRContainer class and I thought why not make use of the variables I already have in BNRitem: _itemName, _serialNumber, _valueInDollars and _dateCreated. My rational is a container will have those values as well in addition when creating an instance of BNRContainer those variables are present although not used in my initial thought of how to use this. What I have added to the new initialization method is and array _containerItems and kept the BNRitem variables as they are:

-(instancetype)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber containerItems:(NSArray *)cItems;

What I would like to know if it’s wrong or not good practice that I have used BNRitem variables instead of for example using _containerName and so on?

My code:
BNRContainer.h

[code]#import “BNRitem.h”

@interface BNRContainer : BNRitem
{
NSArray *_containerItems;

}

// Designated intializer for BNRContainer
-(instancetype)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
containerItems:(NSArray *)cItems;

-(void) setContainerItems:(NSArray *)cItems;
-(NSArray *)containerItems;

-(void)itemsInContainer;
-(int)itemValueInContainer;

@end[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

-(instancetype)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
containerItems:(NSArray *)cItems
{
// Call the superclass’s designated initializer
self = [super init];

// Did the superclass's designation initializer succeed?
if (self) {
    // Give the instance variable initial values
    _itemName = name;
    _serialNumber = sNumber;
    _valueInDollars = value;
    // Set _dateCreated to the current date and time
    _dateCreated = [[NSDate alloc] init];
    _containerItems = [cItems copy];
    
}
// Return the address of the newly initialized object
return self;

}

-(void) setContainerItems:(NSArray *)cItems
{
_containerItems = [cItems copy];
}

-(NSArray *)containerItems
{
return _containerItems;
}

-(void)itemsInContainer
{
for (BNRitem *item in _containerItems) {
NSLog(@"%@ (%@): Worth $%d, recorded on %@",
item.itemName,
item.serialNumber,
item.valueInDollars,
item.dateCreated);
}
}

-(int)itemValueInContainer
{
int sum = 0;
for (BNRitem *item in _containerItems) {
sum = sum + item.valueInDollars;
}
return sum;
}

-(NSString *) description
{
NSString *descriptionString = [[NSString alloc] initWithFormat:@“Container name: %@, Container cost: $%d, Total value of items in container: %d, Number of items in container: %lu”,
self.itemName,
self.valueInDollars,
[self itemValueInContainer],
[self.containerItems count]];
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 {
    
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    for (int i = 0 ; i < 10; i++) {
        BNRitem *item = [BNRitem randomItem];
        [items addObject:item];
    }
    
    BNRContainer *container = [[BNRContainer alloc] initWithItemName:@"SXSW"
                                                 valueInDollars:200
                                                   serialNumber:@"2ABB1"
                                                 containerItems:items];
    [container itemsInContainer];
    NSLog(@"%@", container);
    
    
    items = nil;
    
}
return 0;

}[/code]


#2

After reading some of the posts here it seems the drawback would be that I can’t access the ivars in BNRitem form BNRContainer directly. Well it still works for this exercise but I will need to take care if such variables need to be accessed from within BNRContainer. Thanks ibex10 for pointing that out in another post.