Gold challenge solution(?)


#1

Hi,

I’m not sure that I understood correctly the problem so maybe it’s wrong.

Main.m

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

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

@autoreleasepool {
    
    // create first container & setValue to 250
    BNRContainer *items = [[BNRContainer alloc] initWithName:@"Jardin"];
    [items setValue:250];
    
    // create second container & setValue to 350 (container will be include in the first one
    BNRContainer *items2 = [[BNRContainer alloc] initWithName:@"Parc"];
    [items2 setValue:350];
    
    
   
   // Create BNDItem and add it to the first container
    for(int i = 0; i < 2; i++) {
        
        BNDItem *p = [BNDItem randomItem];
        
        [items addBNRItem:p];
        
        p = nil;
        
    }
    
    // create BNDItem and add it to the second container
    for(int i = 0; i < 2; i++) {
        
        BNDItem *p = [BNDItem randomItem];
        
        [items2 addBNRItem:p];
        
        p = nil;
        
    }
    
    // add second container to the first one
    [items addBNRItem:items2];
    
    
    
    NSLog(@"%@", items);
    
    // free containers
    items = nil;
    items2 = nil;
    
}

return 0;

}
[/code]

BNRContainer.h

[code]#import “BNDItem.h”

@interface BNRContainer : BNDItem
{

NSString *name;
NSMutableArray *subitems;
int value;                  // container value
int valueTotal;             // total value of the contant

}

@property NSString *name;
@property int value, valueTotal;

-(id)initWithName:(NSString*)n;

-(int)returnValueContainer:(int)v;

-(void)addBNRItem:(BNDItem*)item;

@end[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

@synthesize name, value, valueTotal;

// init with the name n and value 0
-(id)initWithName:(NSString*)n
{

self = [super init];
[self setName:n];
[self setValue:0];


return self;

}

// add BNDItem to the container
-(void)addBNRItem:(BNDItem*)item
{

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

[subitems addObject:item];

}

// calculate the Totalvalue of the contant + value of the container
-(int)returnValueContainer:(int)v
{

valueTotal = v;

for(int i = 0; i < [subitems count]; i++) {
    

    id objet = [subitems objectAtIndex:i];

    
    if([objet isMemberOfClass:[BNDItem class]]) {
        
        BNDItem *p = [subitems objectAtIndex:i];
        
        valueTotal += [p valueDollar];
        
    } else if ([objet isMemberOfClass:[BNRContainer class]]) {
        
        BNRContainer *c = [subitems objectAtIndex:i];
        
        
        
        valueTotal = [c returnValueContainer:valueTotal] + [c value];
            
    }
    
}

return valueTotal;

}

-(NSString*)description
{
// begin of the description
NSString *desc = [[NSString alloc] initWithFormat:@"\n%@\nValeur :%d\nValeur Total :%d\nIl contient : {\n", [self name], [self value], [self returnValueContainer:[self value]]];

// call the description of each BNDItem or the container itself
for(BNDItem *p in subitems) {
        
        NSString *sp = [[NSString alloc] initWithFormat:@"%@\n", p];
        desc = [desc stringByAppendingString:sp];
    
}

// end of the description
desc = [desc stringByAppendingString:@"}"];

return desc;

}

@end
[/code]

Output

[code]2014-03-13 22:51:36.825 RandomPossessions[3618:303]
Jardin
Valeur :250
Valeur Total :852
Il contient : {
Petit lamentin (8Q2U8): valeur 73$, enregistré le 2014-03-13 21:51:36 +0000
Vieux lamentin (5Y2V3): valeur 40$, enregistré le 2014-03-13 21:51:36 +0000

Parc
Valeur :350
Valeur Total :489
Il contient : {
Petit lamentin (2F9Z7): valeur 40$, enregistré le 2014-03-13 21:51:36 +0000
Petit cheval (8G5V6): valeur 99$, enregistré le 2014-03-13 21:51:36 +0000
}
}
Program ended with exit code: 0[/code]

Any comments would be appreciated :slight_smile:

Thanks