My Gold Challenge Approach


#1

Hi everyone!

Hope you can give me some trouble talking about the pros and the cons of my code :slight_smile:
I used properties to speed it up a little bit, and all my classes start by MRC instead of BNR.

[size=150]My MRCContainer.h:[/size]

[code]//
// MRCContainer.h
// RandomPossessions
//
// Created by Miguel Rojas Cortés on 12/25/13.
// Copyright © 2013 MiguelRojasCortes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “MRCItem.h”

@interface MRCContainer : MRCItem

@property (copy, nonatomic) NSString *containerName;
@property (copy, nonatomic) NSString *containerID;
@property (nonatomic) int containerValue;
@property (strong, nonatomic) NSArray *containerItems;

  • (id)initWithContainerID:(NSString *)aContainerID
    containerName:(NSString *)aContainerName
    containerValue:(int)aContainerValue
    containerItems:(NSArray *)aContainerItems;

  • (void)obtainContainerValueForContainer:(MRCContainer *)container;

@end
[/code]

[size=150]My MRCContainer.m[/size]

[code]//
// MRCContainer.m
// RandomPossessions
//
// Created by Miguel Rojas Cortés on 12/25/13.
// Copyright © 2013 MiguelRojasCortes. All rights reserved.
//

#import “MRCContainer.h”

@implementation MRCContainer

  • (id)initWithContainerID:(NSString *)aContainerID
    containerName:(NSString *)aContainerName
    containerValue:(int)aContainerValue
    containerItems:(NSArray *)aContainerItems{

    if (self = [super init]) {

      int fullContainerValue = 0;
      
      for (MRCItem *item in aContainerItems) {
          fullContainerValue += [item valueInDollars];
      }
      
      _containerID = aContainerID;
      _containerName = aContainerName;
      _containerValue = fullContainerValue + aContainerValue;
      _containerItems = aContainerItems;
    

    }
    return self;
    }

  • (void)obtainContainerValueForContainer:(MRCContainer *)container{

    NSLog(@“The container worths: %d for the following items %@”,
    [self containerValue],[self containerItems]);
    }

-(NSString *)description{

NSString *containerDescription = [NSString stringWithFormat:@"Container (%@) worths:%d for items: %@",[self containerName],[self containerValue],[self containerItems]];

return containerDescription;

}

@end
[/code]

[size=150]My Main.m[/size]

[code]//
// main.m
// RandomPossessions
//
// Created by Miguel Rojas Cortés on 12/23/13.
// Copyright © 2013 MiguelRojasCortes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “MRCItem.h”
#import “MRCContainer.h”

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

@autoreleasepool {
    
    //Create an array of MRCItems
    NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
    
    //Populate the array with MRCItems
    for (int i=0; i < 20; i++) {
        [itemsArray addObject:[MRCItem randomItem]];
    }
    
    //Create a container of MRCItems
    MRCContainer *container = [[MRCContainer alloc] initWithContainerID:@"ZXC121"
                                                          containerName:@"Nice Stuff"
                                                         containerValue:1450
                                                         containerItems:itemsArray];
    NSLog(@"%@", container);
    
    //Report its full value and its content
    [container obtainContainerValueForContainer:container];
    
}
return 0;

}

[/code]

[size=150]The Outcome[/size]

2013-12-25 19:18:59.969 RandomPossessions[13087:303] Container (Nice Stuff) worths:2546 for items: ( "Rusty Spork (WQQUW): Worth: $73, recorded on 2013-12-26 00:18:59 +0000", "Shiny Spork (TYQVR): Worth: $40, recorded on 2013-12-26 00:18:59 +0000", "Rusty Spork (QFXZV): Worth: $40, recorded on 2013-12-26 00:18:59 +0000", "Rusty Bear (WGTVU): Worth: $99, recorded on 2013-12-26 00:18:59 +0000", "Shiny Spork (RPXBP): Worth: $10, recorded on 2013-12-26 00:18:59 +0000", "Rusty Mac (URTCP): Worth: $93, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Spork (RESOO): Worth: $1, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Mac (RAUTS): Worth: $30, recorded on 2013-12-26 00:18:59 +0000", "Shiny Spork (WSRIP): Worth: $77, recorded on 2013-12-26 00:18:59 +0000", "Rusty Spork (SFUFX): Worth: $65, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Spork (PPTFS): Worth: $29, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Bear (RRQQU): Worth: $88, recorded on 2013-12-26 00:18:59 +0000", "Rusty Bear (SXVPW): Worth: $38, recorded on 2013-12-26 00:18:59 +0000", "Shiny Mac (VESLP): Worth: $77, recorded on 2013-12-26 00:18:59 +0000", "Shiny Bear (WKXYQ): Worth: $35, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Spork (WJWTO): Worth: $94, recorded on 2013-12-26 00:18:59 +0000", "Rusty Bear (TEQZP): Worth: $42, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Spork (PKPGV): Worth: $71, recorded on 2013-12-26 00:18:59 +0000", "Rusty Mac (XXUCW): Worth: $40, recorded on 2013-12-26 00:18:59 +0000", "Fluffy Bear (RHORU): Worth: $54, recorded on 2013-12-26 00:18:59 +0000" )

[size=200]HAPPY HOLIDAYS![/size]