Golden Challenge: my solution


#1

Hi guys,

It takes me some time to understand and solve the question, but I finally accomplished it my way, don’t know is too complicated but works. hope this help others too.

BNR Container.h

#import <Foundation/Foundation.h>
#import "BNRItem.h"

@interface BNRContainer : NSObject
{
    NSString *containerName;
    NSString *containerItemName;
    NSString *containerItemSerialNumber;
    int containerItemValueInDollars;
    NSDate *containerItemDateCreated;
}
-(id) initWithContainerName: (NSString *) containerName
          containerItemName: (NSString *) containerItemName
  containerItemSerialNumber: (NSString *) containerItemSerialNumber
containerItemValueInDollars:(int) Value;

-(void) setcontainerName: (NSString *) str;
-(NSString *) containerName;

-(void) setcontainerItemName: (NSString *) str;
-(NSString *) containerItemName;

-(void) setcontainerItemSerialNumber : (NSString *) str;
-(NSString *) containerItemSerialNumber;

-(void) setcontainerItemValueInDollars : (int)i;
-(int) containerItemValueInDollars;

-(NSDate *) containerItemDateCreated;

// Display data
-(void) print;

@end

BNRContainer.m

#import "BNRContainer.h"

@implementation BNRContainer

-(id) init
{
    return [self initWithContainerName:@"Box"
                     containerItemName:@"Container"
             containerItemSerialNumber:@"Not Available"
           containerItemValueInDollars:25];
}

-(id) initWithContainerName: (NSString *) name
          containerItemName: (NSString *) itemName
  containerItemSerialNumber: (NSString *) serialNumber
containerItemValueInDollars:(int) Value;
{
    
    if (self) {
        [self setcontainerName: name];
        [self setcontainerItemName];
        [self setcontainerItemSerialNumber:serialNumber];
         [self setcontainerItemValueInDollars:Value];
        containerItemDateCreated = [[NSDate alloc]init];
    }

    return self;
}

// Setters:
-(void) setcontainerName: (NSString *) str;
{
    containerName = str;
}
-(NSString *) containerName
{
    return containerName;
}

-(void) setcontainerItemName: (NSString *) str;
{
    containerItemName = str;
}
-(NSString *) containerItemName
{
    return containerItemName;
}


-(void) setcontainerItemSerialNumber : (NSString *) str;
{
    containerItemSerialNumber = str;
}
-(NSString *) containerItemSerialNumber
{
    return containerItemSerialNumber;
}


-(void) setcontainerItemValueInDollars : (int)i;
{
    containerItemValueInDollars = i;
}
-(int) containerItemValueInDollars
{
    return containerItemValueInDollars;
}

-(NSDate *) containerItemDateCreated
{
    return containerItemDateCreated;
}
// Display exit for Container

-(void) print
{
    NSLog(@"The container %@ includes: -%@, SN(%@), Worth $%i, Recorded: %@ ",
          containerName,
          containerItemName,
          containerItemSerialNumber,
          containerItemValueInDollars,
          containerItemDateCreated);
}
@end

main.m

#import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
         int sum=0;
        
        for (int j=0; j<4; j++) {
            
            int total=0;
            int contValue =25;
           NSMutableArray *containerItems=[NSMutableArray new];
            [containerItems addObject:[BNRContainer new]];
        
        for (int i=0; i<6;i++){
            BNRItem *p = [BNRItem randomItem];

            BNRContainer *q = [[BNRContainer new]
                               initWithContainerName:[NSString stringWithFormat:@"Container %i",j+1]
                               containerItemName:[p itemName]
                               containerItemSerialNumber:[p serialNumber]
                               containerItemValueInDollars:[p valueInDollars]];

            [containerItems addObject:q];
            total = total + [p valueInDollars];
        }
        sum = sum + total + contValue;
        for (BNRContainer *container in containerItems)
        {
            [container print];
        }
            NSLog(@"\n");
        NSLog(@"Total value of objects container included: $%d", total + contValue);
        NSLog(@"Total value of Items contained: $%d", total);
        NSLog(@"---------------------------------------------------------------------------------------------------------------------");
            
        }
        
        NSLog(@"Total worth of all objects:$%d",sum);
            }  
    return 0;
}

Console output:

2013-02-20 12:16:04.598 RandomPossessions[668:303] The container Box includes: -Container, SN(Not Available), Worth $25, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.599 RandomPossessions[668:303] The container Container 1 includes: -Rusty Spork, SN(8Q2U8), Worth $73, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.600 RandomPossessions[668:303] The container Container 1 includes: -Shiny Spork, SN(5Y2V3), Worth $40, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.601 RandomPossessions[668:303] The container Container 1 includes: -Rusty Spork, SN(2F9Z7), Worth $40, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.601 RandomPossessions[668:303] The container Container 1 includes: -Rusty Bear, SN(8G5V6), Worth $99, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.602 RandomPossessions[668:303] The container Container 1 includes: -Shiny Spork, SN(3P9B1), Worth $10, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.602 RandomPossessions[668:303] The container Container 1 includes: -Rusty Mac, SN(6R5C1), Worth $93, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.602 RandomPossessions[668:303] 
2013-02-20 12:16:04.603 RandomPossessions[668:303] Total value of objects container included: $380
2013-02-20 12:16:04.603 RandomPossessions[668:303] Total value of Items contained: $355
2013-02-20 12:16:04.604 RandomPossessions[668:303] ---------------------------------------------------------------------------------------------------------------------
2013-02-20 12:16:04.604 RandomPossessions[668:303] The container Box includes: -Container, SN(Not Available), Worth $25, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.605 RandomPossessions[668:303] The container Container 2 includes: -Fluffy Spork, SN(3E4O0), Worth $1, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.605 RandomPossessions[668:303] The container Container 2 includes: -Fluffy Mac, SN(3A6T4), Worth $30, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.606 RandomPossessions[668:303] The container Container 2 includes: -Shiny Spork, SN(8S3I1), Worth $77, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.606 RandomPossessions[668:303] The container Container 2 includes: -Rusty Spork, SN(4F6F9), Worth $65, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.606 RandomPossessions[668:303] The container Container 2 includes: -Fluffy Spork, SN(1P5F4), Worth $29, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.607 RandomPossessions[668:303] The container Container 2 includes: -Fluffy Bear, SN(3R2Q6), Worth $88, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.607 RandomPossessions[668:303] 
2013-02-20 12:16:04.607 RandomPossessions[668:303] Total value of objects container included: $315
2013-02-20 12:16:04.608 RandomPossessions[668:303] Total value of Items contained: $290
2013-02-20 12:16:04.608 RandomPossessions[668:303] ---------------------------------------------------------------------------------------------------------------------
2013-02-20 12:16:04.609 RandomPossessions[668:303] The container Box includes: -Container, SN(Not Available), Worth $25, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.609 RandomPossessions[668:303] The container Container 3 includes: -Rusty Bear, SN(4X7P8), Worth $38, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.609 RandomPossessions[668:303] The container Container 3 includes: -Shiny Mac, SN(7E4L1), Worth $77, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.610 RandomPossessions[668:303] The container Container 3 includes: -Shiny Bear, SN(8K9Y2), Worth $35, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.610 RandomPossessions[668:303] The container Container 3 includes: -Fluffy Spork, SN(8J8T0), Worth $94, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.611 RandomPossessions[668:303] The container Container 3 includes: -Rusty Bear, SN(5E2Z1), Worth $42, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.611 RandomPossessions[668:303] The container Container 3 includes: -Fluffy Spork, SN(1K1G7), Worth $71, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.612 RandomPossessions[668:303] 
2013-02-20 12:16:04.612 RandomPossessions[668:303] Total value of objects container included: $382
2013-02-20 12:16:04.613 RandomPossessions[668:303] Total value of Items contained: $357
2013-02-20 12:16:04.613 RandomPossessions[668:303] ---------------------------------------------------------------------------------------------------------------------
2013-02-20 12:16:04.614 RandomPossessions[668:303] The container Box includes: -Container, SN(Not Available), Worth $25, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.614 RandomPossessions[668:303] The container Container 4 includes: -Rusty Mac, SN(9X6C8), Worth $40, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.614 RandomPossessions[668:303] The container Container 4 includes: -Fluffy Bear, SN(3H0R6), Worth $54, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.615 RandomPossessions[668:303] The container Container 4 includes: -Fluffy Mac, SN(0A4S4), Worth $13, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.615 RandomPossessions[668:303] The container Container 4 includes: -Rusty Spork, SN(4J8M0), Worth $26, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.616 RandomPossessions[668:303] The container Container 4 includes: -Shiny Bear, SN(7F7U9), Worth $23, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.616 RandomPossessions[668:303] The container Container 4 includes: -Shiny Bear, SN(5T0K8), Worth $96, Recorded: 2013-02-20 18:16:04 +0000 
2013-02-20 12:16:04.616 RandomPossessions[668:303] 
2013-02-20 12:16:04.617 RandomPossessions[668:303] Total value of objects container included: $277
2013-02-20 12:16:04.617 RandomPossessions[668:303] Total value of Items contained: $252
2013-02-20 12:16:04.617 RandomPossessions[668:303] ---------------------------------------------------------------------------------------------------------------------
2013-02-20 12:16:04.619 RandomPossessions[668:303] Total worth of all objects:$1354