Gold Challenge Question: BNRContainer


#1

Could anyone please help me refine the code and make appropriate changes in it…

BNRItem.m

#import "BNRItem.h"

@implementation BNRItem
+(id)randomItem{
    //Item Names
    NSArray *nounList= [NSArray arrayWithObjects:@"Mac",@"iPhone",@"iPad", nil];
    NSArray *adjectiveList=[NSArray arrayWithObjects:@"Snazzy",@"Cool",@"Sexy", nil];
    
    int randomNounIndex= rand()%[nounList count];
    int randomAdjectiveIndex=rand()% [adjectiveList count];
    
    NSString *randomName=[NSString stringWithFormat:@"%@ %@",[adjectiveList objectAtIndex:randomAdjectiveIndex],[nounList objectAtIndex:randomNounIndex]];
    
    //Item ValuesInDollars
    int randomValue=rand()%100;
    
    //Item Serial-Numbers
    NSString *randomSerialNumber=[NSString stringWithFormat:@"%c%c%c%c%c",'A'+rand()%26,'0'+rand()%10,'0'+rand()%10,'A'+rand()%26,'A'+rand()%26];
    
    BNRItem *newItem=[[self alloc] initWithItemName:randomName valueInDollars:randomValue serialNumber:randomSerialNumber];
    return newItem;
}
-(void)setItemName:(NSString *)currentItemName{
    itemName=currentItemName;
}
-(NSString *)itemName{
    return itemName;
}
-(void)setSerialNumber:(NSString *)currentSerialNumber{
    serialNumber=currentSerialNumber;
}
-(NSString *)serialNumber{
    return serialNumber;
}
-(void)setValueInDollars:(int)currentValueInDollars{
    valueInDollars=currentValueInDollars;
}
-(int)valueInDollars{
    return valueInDollars;
}
-(NSString *)description{
    return [NSString stringWithFormat:@"%@(%@) having %d dollars as cost.Date of listing: %@",[self itemName],[self serialNumber],[self valueInDollars],dateCreated];
}
-(id)init{
    return [self initWithItemName:@"Item" valueInDollars:0 serialNumber:@" "];
}
-(id)initWithItemName:(NSString *)name{
    return [self initWithItemName:name valueInDollars:0 serialNumber:@" "];
}
-(id)initWithItemName:(NSString *)name serialNumber:(NSString *)currentSerialName{
    return [self initWithItemName:name valueInDollars:0 serialNumber:currentSerialName];
}
-(id)initWithItemName:(NSString *)name valueInDollars:(int)currentValueInDollars serialNumber:(NSString *)currentSerialNumber{
    if (self=[super init]) {
        [self setItemName:name];
        [self setSerialNumber:currentSerialNumber];
        [self setValueInDollars:currentValueInDollars];
        dateCreated=[[NSDate alloc] init];
    }
    return self;
}

@end

BNRContainer.h

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSMutableArray *contents;
    NSString *containerName;
    int totalValue;
    int totalItemsCost;
}
-(void)addItemsUpto:(int)numberOfItemsInAContainer;
-(void)setContainerName:(NSString *)name;
-(NSString *)containerName;
-(id)initWithContainerName:(NSString *)name valueOfContainerInDollars:(int) value;
-(void)setTotalValue:(int)valueOfContainer;
-(int)totalValue;
@end

BNRContainer.m

#import "BNRContainer.h"

@implementation BNRContainer
-(void)addItemsUpto:(int)numberOfItemsToBeAddedInAContainer{
    contents= [[NSMutableArray alloc] init];
    for (int i=0; i<numberOfItemsToBeAddedInAContainer; i++) {
        BNRItem *item=[BNRItem randomItem];
        totalItemsCost+=[item valueInDollars];
        [contents addObject:item];
    }
    
}
-(void)setContainerName:(NSString *)name{
    containerName=name;
}
-(NSString *)containerName{
    return containerName;
}
-(void)setTotalValue:(int)valueOfContainer{
    totalValue=totalItemsCost+valueOfContainer;
}
-(int)totalValue{
    return totalValue;
}
-(id)initWithContainerName:(NSString *)name valueOfContainerInDollars:(int) value{
    [self setContainerName:name];
    return self;
}
-(NSString *)description{
    return [NSString stringWithFormat:@"Name of Container= %@, total value= %d, items contained= %@ ",containerName,totalValue,contents];
}
@end

In main function:
main.m

#import <Foundation/Foundation.h>
#import "BNRContainer.h"
int main(int argc, const char * argv[])
{
    
    @autoreleasepool {        
         BNRContainer *container=[[BNRContainer alloc] initWithContainerName:@"container1"];
        [container addItemsUpto:5];
        [container setTotalValue:100];
        NSLog(@"%@",container);
        
        BNRContainer *container2=[[BNRContainer alloc] initWithContainerName:@"container2"];
        [container2 addItemsUpto:5];
        [container2 setTotalValue:100];
        NSLog(@"%@",container2);
        NSLog(@"Net Value= %d dollars",[container totalValue]+[container2 totalValue]);
   }
return 0;
}

The total value (value of container+sum of BNRItems) is now reflecting in the output (262+100 respectively). container1 and container2 both contain some random items. I hope this is what we had to do. But what i don’t understand is where were we supposed to use overriding principles and object orientation in the code. The initialiser in BNRContainer is not calling the superclass (BNRItem) initialiser i.e “initWithItemName:(NSString *)name valueInDollars:(int)currentValueInDollars serialNumber:(NSString *)currentSerialNumber” and the method “randomItem” is not being used at all in BNRContainer(I found no use of an overriding version of it) except for creating BNRItems. That ways the self variable in “randomItem” has been rendered useless. One could have a normal instantiation- BNRItem *newItem=[[BNRItem alloc] initWithItemName:randomName valueInDollars:randomValue serialNumber:randomSerialNumber]. (Note that BNRItem replaces self variable in this line).
Please let me know if this code is relevant and appropriate as per the challenge. Is this what we were supposed to do?? If not ,please suggest something and help me course-correct this code.

Output:
2013-06-12 17:12:28.298 BNRItem[2166:303] Name of Container= container1, total value= 362, items contained= (
“Cool iPhone(K02UQ) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool iPad(X22VN) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool iPhone(S39ZR) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Snazzy iPhone(Y65VE) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool iPad(F99BH) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”
)
2013-06-12 17:12:28.299 BNRItem[2166:303] Name of Container= container2, total value= 366, items contained= (
“Sexy iPhone(Q55CR) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool Mac(X84OQ) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Sexy Mac(L26TG) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool iPad(C83IJ) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”,
“Cool iPhone(S36FV) having as cost.Date of listing: 2013-06-12 11:42:28 +0000”
)
2013-06-12 17:12:28.300 BNRItem[2166:303] Net Value=