Feedback on my version of the Gold Challenge?


#1

Below follows my version of the BNRContainer class. Any comments for improvements or things I’ve missed would be appreciated.

BNRContainer.h

[code]#import “BNRItem.h”

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

  • (id)initWithContainerName:(NSString *)containerName andSubitems:(NSMutableArray *)subitemArray;

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

  • (NSMutableArray *)subitems;

@end
[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (id)initWithContainerName:(NSString *)name andSubitems:(NSMutableArray *)subitemArray
    {
    self = [super initWithItemName:@"" valueInDollars:0 serialNumber:@""];

    if (self) {
    containerName = name;
    subitems = [NSMutableArray arrayWithArray:subitemArray];
    int contentsValue;

      for (int i = 0; i < [subitemArray count]; i++) {
          contentsValue += [[subitemArray objectAtIndex:i] valueInDollars];
      }
      
      valueInDollars = contentsValue + 10000;
    

    }

    return self;
    }

  • (id)initWithContainerName:(NSString *)name
    {
    return [self initWithContainerName:name andSubitems:[[NSMutableArray alloc] init]];
    }

  • (id)init
    {
    return [self initWithContainerName:@“Unnamed Container” andSubitems:[[NSMutableArray alloc] init]];
    }

  • (NSMutableArray *)subitems
    {
    return subitems;
    }

  • (NSString *)description
    {
    NSString *descriptionString = [NSString stringWithFormat:@“Container: %@ - Value: $%d Contents: %@”, containerName, valueInDollars, subitems];

    return descriptionString;
    }

@end
[/code]

The only odd thing is the output in my console, when a container is inside a container, doesn’t display as I’d expect. It turns out like this -

2012-05-10 22:13:48.332 RandomPossesions[2050:403] Container: Container - Value: $10262 Contents: (
“Rusty Spork (8Q2U8): Worth $73, recorded on 2012-05-10 21:13:48 +0000”,
“Shiny Spork (5Y2V3): Worth $40, recorded on 2012-05-10 21:13:48 +0000”,
“Rusty Spork (2F9Z7): Worth $40, recorded on 2012-05-10 21:13:48 +0000”,
“Rusty Bear (8G5V6): Worth $99, recorded on 2012-05-10 21:13:48 +0000”,
“Shiny Spork (3P9B1): Worth $10, recorded on 2012-05-10 21:13:48 +0000”,
“Container: Inner Container - Value: $10262 Contents: (\n “Rusty Spork (8Q2U8): Worth $73, recorded on 2012-05-10 21:13:48 +0000”,\n “Shiny Spork (5Y2V3): Worth $40, recorded on 2012-05-10 21:13:48 +0000”,\n “Rusty Spork (2F9Z7): Worth $40, recorded on 2012-05-10 21:13:48 +0000”,\n “Rusty Bear (8G5V6): Worth $99, recorded on 2012-05-10 21:13:48 +0000”,\n “Shiny Spork (3P9B1): Worth $10, recorded on 2012-05-10 21:13:48 +0000”\n)”
)

Is that something I can fix?


#2
  1. NSLog does this, you can try using printf instead. See the LogIt function here: http://borkware.com/quickies/one?topic=NSString. No promises there, if it doesn’t work, let me know, I can dig a bit deeper and figure it out.

  2. Your version has a flaw: what happens if you add an item to the container and ask for the valueInDollars of the container? The value wouldn’t be updated. Hint: a getter method doesn’t have to access an instance variable…


#3

Thanks Joe.

I’ve made a couple changes to calculate the value of a containers contents. I now override the valueInDollars method and calculate a containers value when it’s called. I also removed the code that makes any changes to the valueInDollars variable when adding creating a container. I’ve just included the changed methods here. Let me know if it’s more useful to just repost the complete file rather than a couple methods.

[code]

  • (id)initWithContainerName:(NSString *)name andSubitems:(NSMutableArray *)subitemArray
    {
    self = [super initWithItemName:@"" valueInDollars:0 serialNumber:@""];

    if (self) {
    containerName = name;
    subitems = [NSMutableArray arrayWithArray:subitemArray];
    }

    return self;
    }

  • (int)valueInDollars
    {
    int contentsValue = 0;
    int containerValue = 1000;

    for (int i = 0; i < [[self subitems] count]; i++) {
    contentsValue += [[[self subitems] objectAtIndex:i] valueInDollars];
    }

    contentsValue += containerValue;

    return contentsValue;
    }[/code]

The LogIt function gave me the same results when logging containers within containers. I’m happy to just accept it as a quirk of NSLog, thanks for trying to help sort it out.