Gold Challenge - my solution


#1

Hello,

So I’m a bit lazy and also very literal, therefore I designed my solution to do pretty much exactly what the requirements said, specifically:
[ul]
[li] BNRContainer should be a subclass of BNRItem[/li]
[li] BNRContainer should have an array of subitems that contains instances of BNRItem[/li]
[li] Not specified, but implied: BNRContainer should not have any other properties besides the above array and the properties inherited from BNRItem[/li]
[li] BNRContainer’s description should show the name of the container (the itemName property, inherited from BNRItem), its value in dollars (a computed property – see below) and a list of every BNRItem it contains[/li]
[li] BNRContainer’s valueInDollars property should return the sum of all items in the container plus the value of the container itself[/li]
[li] Not specified, but implied: setting a container’s valueInDollars property only sets the value of the container itself and has no impact on the value of its contained items[/li]
[li] BNRContainer can contain instances of BNRContainer items as well and will duly report back its full value and every contained item properly[/li]
[li] Not specified, but implied: the requirements say nothing about adding or removing individual items from a container, therefore I will only provide the ability to get and set the subitems list as a whole[/li]
[li] Although not specifically requested, I will also add a convenience +randomItems class method that returns a fully populated list of random items[/li][/ul]

With the above in mind, here is my minimalistic implementation of a BNRContainer:

BNRContainer.h

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

@interface BNRContainer : BNRItem
{
    NSMutableArray *subitems;
}

+ (id)randomItems;

- (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber
              subitems:(NSArray *)items;

- (void)setSubitems:(NSArray *)items;
- (NSArray *)subitems;

@end

BNRContainer.m

#import "BNRContainer.h"

@implementation BNRContainer

+ (id)randomItems
{
    // Create an array large enough to contain 10 subitems
    NSMutableArray *subitems =
        [[NSMutableArray alloc] initWithCapacity:10];
    
    // Fill the array with our random subitems
    for (int i = 0; i < 10; i++) {
        BNRItem *p = [BNRItem randomItem];
        [subitems addObject:p];
    }
    
    // Create a temporary random BNRItem
    BNRItem *tempItem = [BNRItem randomItem];
    
    // Use its properties to initialize a new BNRContainer with our subitems array
    BNRContainer *container =
        [[BNRContainer alloc] initWithItemName:[tempItem itemName]
                                valueInDollars:[tempItem valueInDollars]
                                  serialNumber:[tempItem serialNumber]
                                      subitems];
    
    // We no longer need the temporary BNRItem, so discard it
    tempItem = nil;
    
    // Return our initialized BNRContainer
    return container;
}

- (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber
              subitems:(NSArray *)items
{
    // Call our superclass initializer
    self = [super initWithItemName:name
                    valueInDollars:value
                      serialNumber:sNumber];
    
    if (self) {
        // If successful, initialize our subitems property as well
        [self setSubitems:items];
    }
    
    return self;
}

- (void)setSubitems:(NSArray *)items
{
    subitems = items;
}

- (NSArray *)subitems
{
    return subitems;
}

- (int)valueInDollars
{
    // Will contain the sum value of all subitems
    int valueOfSubitems = 0;
    
    for (BNRItem *item in subitems) {
        valueOfSubitems += [item valueInDollars];
    }
    
    // Return the sum of all items in the container plus the value of the container itself
    return valueOfSubitems + valueInDollars;
}

- (NSString *)description
{
    NSString *descriptionString =
        [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@, "
                                          "Subitems: %@, Full Worth $%d",
                            itemName,
                            serialNumber,
                            valueInDollars,
                            dateCreated,
                            subitems,
                            [self valueInDollars]];
    
    return descriptionString;
}

@end

You can proceed to test the above implementation by replacing the main.m example code with the following:

main.m

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

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

    @autoreleasepool {
        
        BNRContainer *c = [BNRContainer randomItems];
        
        NSLog(@"%@", c);

        c = nil;
    }
    return 0;
}

On my system, this returns the following output:

2012-08-30 09:20:09.797 RandomPossessions[73387:303] Fluffy Spork (1P5F4): Worth $29, recorded on 2012-08-30 13:20:09 +0000, Subitems: (
    "Rusty Spork (8Q2U8): Worth $73, recorded on 2012-08-30 13:20:09 +0000",
    "Shiny Spork (5Y2V3): Worth $40, recorded on 2012-08-30 13:20:09 +0000",
    "Rusty Spork (2F9Z7): Worth $40, recorded on 2012-08-30 13:20:09 +0000",
    "Rusty Bear (8G5V6): Worth $99, recorded on 2012-08-30 13:20:09 +0000",
    "Shiny Spork (3P9B1): Worth $10, recorded on 2012-08-30 13:20:09 +0000",
    "Rusty Mac (6R5C1): Worth $93, recorded on 2012-08-30 13:20:09 +0000",
    "Fluffy Spork (3E4O0): Worth $1, recorded on 2012-08-30 13:20:09 +0000",
    "Fluffy Mac (3A6T4): Worth $30, recorded on 2012-08-30 13:20:09 +0000",
    "Shiny Spork (8S3I1): Worth $77, recorded on 2012-08-30 13:20:09 +0000",
    "Rusty Spork (4F6F9): Worth $65, recorded on 2012-08-30 13:20:09 +0000"
), Full Worth $557

#2

Hi erictrepanier, I attach my version:

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem

{
NSMutableArray *subitems;
}

  • (id)randomItems;
  • (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber subitems:(NSMutableArray *)items;

  • (void)setSubitems:(NSMutableArray *)items;

  • (NSMutableArray *)subitems;

  • (void)addItem:(BNRItem *)item;

  • (BNRItem *)removeLastItem;

@end
[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (id)randomItems
    {
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:0];

    for (int i = 0; i < 10; i++)
    {
    BNRItem *i = [BNRItem randomItem];

      [items addObject:i];
    

    }

    return items;
    }

  • (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber subitems:(NSMutableArray *)items
    {
    self = [super initWithItemName:name valueInDollars:value serialNumber:sNumber];

    if (self)
    {
    [self setSubitems:items];
    }

    return self;
    }

  • (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    return [self initWithItemName:name valueInDollars:value serialNumber:sNumber subitems:[[NSMutableArray alloc] initWithCapacity:0]];
    }

  • (void)setSubitems:(NSMutableArray *)items
    {
    subitems = items;
    }

  • (NSMutableArray *)subitems
    {
    return subitems;
    }

  • (NSString *)description
    {
    NSString *descriptionString = @"\n\nCONTAINER INFO";

    descriptionString = [descriptionString stringByAppendingFormat:@"\n\n Name: %@", [self itemName]];
    descriptionString = [descriptionString stringByAppendingFormat:@"\nValue: %d\n\nItems:\n", [super valueInDollars]];

    for (BNRItem *item in [self subitems])
    {
    descriptionString = [descriptionString stringByAppendingFormat:@"\n%@", item];
    }

    descriptionString = [descriptionString stringByAppendingFormat:@"\n\nTotal Value: %d", [self valueInDollars]];

    return descriptionString;
    }

  • (int)valueInDollars
    {
    int totalValue = 0;

    totalValue += [super valueInDollars];

    for (BNRItem *item in [self subitems])
    {
    totalValue += [item valueInDollars];
    }

    return totalValue;
    }

  • (void)addItem:(BNRItem *)item
    {
    [[self subitems] addObject:item];
    }

  • (BNRItem *)removeLastItem
    {
    BNRItem *lastItem = [[self subitems] lastObject];

    [[self subitems] removeLastObject];

    return lastItem;
    }

@end[/code]

main.m

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

int main(int argc, const char * argv[])
{
@autoreleasepool {
BNRContainer *container = [[BNRContainer alloc] initWithItemName:@“Container Name” valueInDollars:50 serialNumber:@“12345” subitems:[BNRContainer randomItems]];

    if (container) {
        NSLog(@"%@", container);
        
        [container addItem:[BNRItem randomItem]];
        
        NSLog(@"\n\n************* ITEM ADDED *************%@", container);
        
        [container removeLastItem];
        [container removeLastItem];
        
        NSLog(@"\n\n************* 2 ITEMS REMOVED *************%@", container);
    }
    
    container = nil;
}

return 0;

}[/code]

Output:

[code]2012-09-25 16:14:45.593 RandomPossessions[11046:403]

CONTAINER INFO

Name: Container Name
Value: 50

Items:

Rusty Spork (8Q2U8): Worth $73, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (5Y2V3): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Spork (2F9Z7): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Bear (8G5V6): Worth $99, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (3P9B1): Worth $10, recorded on 2012-09-25 14:14:45 +0000
Rusty Mac (6R5C1): Worth $93, recorded on 2012-09-25 14:14:45 +0000
Fluffy Spork (3E4O0): Worth $1, recorded on 2012-09-25 14:14:45 +0000
Fluffy Mac (3A6T4): Worth $30, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (8S3I1): Worth $77, recorded on 2012-09-25 14:14:45 +0000
Rusty Spork (4F6F9): Worth $65, recorded on 2012-09-25 14:14:45 +0000

Total Value: 578
2012-09-25 16:14:45.597 RandomPossessions[11046:403]

************* ITEM ADDED *************

CONTAINER INFO

Name: Container Name
Value: 50

Items:

Rusty Spork (8Q2U8): Worth $73, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (5Y2V3): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Spork (2F9Z7): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Bear (8G5V6): Worth $99, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (3P9B1): Worth $10, recorded on 2012-09-25 14:14:45 +0000
Rusty Mac (6R5C1): Worth $93, recorded on 2012-09-25 14:14:45 +0000
Fluffy Spork (3E4O0): Worth $1, recorded on 2012-09-25 14:14:45 +0000
Fluffy Mac (3A6T4): Worth $30, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (8S3I1): Worth $77, recorded on 2012-09-25 14:14:45 +0000
Rusty Spork (4F6F9): Worth $65, recorded on 2012-09-25 14:14:45 +0000
Fluffy Spork (1P5F4): Worth $29, recorded on 2012-09-25 14:14:45 +0000

Total Value: 607
2012-09-25 16:14:45.601 RandomPossessions[11046:403]

************* 2 ITEMS REMOVED *************

CONTAINER INFO

Name: Container Name
Value: 50

Items:

Rusty Spork (8Q2U8): Worth $73, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (5Y2V3): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Spork (2F9Z7): Worth $40, recorded on 2012-09-25 14:14:45 +0000
Rusty Bear (8G5V6): Worth $99, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (3P9B1): Worth $10, recorded on 2012-09-25 14:14:45 +0000
Rusty Mac (6R5C1): Worth $93, recorded on 2012-09-25 14:14:45 +0000
Fluffy Spork (3E4O0): Worth $1, recorded on 2012-09-25 14:14:45 +0000
Fluffy Mac (3A6T4): Worth $30, recorded on 2012-09-25 14:14:45 +0000
Shiny Spork (8S3I1): Worth $77, recorded on 2012-09-25 14:14:45 +0000

Total Value: 513
[/code]