# Gold Challenge - My Solution - 2nd Chapter - Objective-C

#1

Guys,

Here is my Solution to the Gold Challenge for the 2nd Chapter - Objective C

## Final Output

``````2012-07-06 13:18:58.829 ModRandomPossessions[1948:403] BRN**********************
2012-07-06 13:18:58.858 ModRandomPossessions[1948:403] Red Sofaasd (ABC12asd3): Worth \$10120 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.858 ModRandomPossessions[1948:403] ***************container1***************
2012-07-06 13:18:58.859 ModRandomPossessions[1948:403] Rusty Spork (8Q2U8): Worth \$73 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.860 ModRandomPossessions[1948:403] Shiny Spork (5Y2V3): Worth \$40 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.860 ModRandomPossessions[1948:403] Value of the ***container1***= \$113
2012-07-06 13:18:58.861 ModRandomPossessions[1948:403] ***************container2***************
2012-07-06 13:18:58.862 ModRandomPossessions[1948:403] Rusty Spork (2F9Z7): Worth \$40 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.863 ModRandomPossessions[1948:403] Rusty Bear (8G5V6): Worth \$99 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.863 ModRandomPossessions[1948:403] Value of the ***container2***= \$139
2012-07-06 13:18:58.864 ModRandomPossessions[1948:403] BRN**********************
2012-07-06 13:18:58.864 ModRandomPossessions[1948:403] Red Sofa (ABC123): Worth \$100 recorded on 2012-07-06 07:48:58 +0000
2012-07-06 13:18:58.865 ModRandomPossessions[1948:403] *******************************
2012-07-06 13:18:58.865 ModRandomPossessions[1948:403] Value of the All Container = \$10472
2012-07-06 13:18:58.866 ModRandomPossessions[1948:403] *******************************``````

BNRContainer.h

[code]//
// BNRContainer.h
// ModRandomPossessions
//
// Created by Vijay K on 7/6/12.
//

#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSMutableArray* containerArray;
NSString *containerName;
}

• (NSMutableArray*) containerArray;

• (NSString*) description;

• (id) initwithContainerName:(NSString*)name;

• (NSString*) containerName;

• (void) setContainerName:(NSString*) newContainerName;

@end
[/code]

BNRContainer.m

[code]//
// BNRContainer.m
// ModRandomPossessions
//
// Created by Vijay K on 7/6/12.
//

#import “BNRContainer.h”

@implementation BNRContainer

• (id) initwithContainerName:(NSString*)name
{
[self setContainerName:name];
return self;
}

• (NSMutableArray*) containerArray
{
return containerArray;
}

• (NSString*) containerName
{
return containerName;
}

• (void) setContainerName:(NSString*) newContainerName
{
containerName = newContainerName;
}

{
if (!containerArray) {
containerArray = [[NSMutableArray alloc] init];
}

}

• (NSString*) description
{

int allContainerTotal = 0;;

for (id obj in containerArray)
{
if ([obj isMemberOfClass:[BNRItem class]])
{
NSLog(@“BRN**********************”);
NSLog(@"%@",obj);
allContainerTotal = allContainerTotal + [obj valueInDollars];
}
if ([obj isMemberOfClass:[BNRContainer class]])
{
int containerTotal=0;
NSLog(@“%@",[obj containerName]);
NSMutableArray
items = [obj containerArray];
for (BNRItem p in items)
{
containerTotal = containerTotal+ [p valueInDollars];
NSLog(@"%@",p);
}
NSLog(@“Value of the %@= %d",[obj containerName],containerTotal); allContainerTotal = allContainerTotal + containerTotal; } } NSLog(@"*******************************"); NSLog(@"Value of the All Container = %d”,allContainerTotal);
NSLog(@"
*******”);

return @"";
}
@end
[/code]

main.m

[code]//
// main.m
// ModRandomPossessions
//
// Created by Vijay K on 7/5/12.
//
#import “BNRItem.h”
#import “BNRContainer.h”
#import <Foundation/Foundation.h>

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

``````@autoreleasepool {

BNRContainer *container1 = [[BNRContainer alloc] initwithContainerName:@"***container1***"];

BNRContainer *container2 = [[BNRContainer alloc] initwithContainerName:@"***container2***"];

BNRContainer *mainContainer = [[BNRContainer alloc] initwithContainerName:@"***mainContainer***"];

BNRItem *p = [[BNRItem alloc] initWithItemName:@"Red Sofa" valueInDollar:100 serialNumber:@"ABC123"];

BNRItem *pp = [[BNRItem alloc] initWithItemName:@"Red Sofaasd" valueInDollar:10120 serialNumber:@"ABC12asd3"];

for (int i = 0; i < 2; i++)
{
BNRItem *p = [BNRItem randomItems];
}

for (int i = 0; i < 2; i++)
{
BNRItem *p = [BNRItem randomItems];
}

NSLog(@"%@", mainContainer);
}
return 0;
``````

}
[/code]

Just go thru guys, please let me know if you have any questions…

#2

If BNRContainer inherits from BNRItem, then it will already have an instance variable for name (itemName) (as well as value, and serialNumber, and dateCreated).

did you create a containerName variable to be cleaner? I was in doubt about it myself… should I re-use the itemName variable, or should a create a new one that is more descriptive?