My Version of the Gold Challenge


#1

Ok; here’s my version of the Gold Challenge. I believe this follows the letter of the law. It creates a container which is a subclass of “BNRItem” (JGCItem in my case), that can contain BNRItems or BNRContainers. The BNRContainer implementation of the description method is recursive, so it can handle any level of BNRContainers.

Please take a look and let me know what you think.

[code]//
// JGCItem.h
// RandomPossessions
//
// Created by Joel Gerber on 2013-06-09.
// Copyright © 2013 Joel Gerber Co. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface JGCItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}

  • (id)randomItem;

// Designated Initializer.

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

  • (void)setItemName:(NSString *)str;

  • (NSString *)itemName;

  • (void)setSerialNumber:(NSString *)str;

  • (NSString *)serialNumber;

  • (void)setValueInDollars:(int)i;

  • (int)valueInDollars;

  • (NSDate *)dateCreated;

@end[/code]

[code]//
// JGCItem.m
// RandomPossessions
//
// Created by Joel Gerber on 2013-06-09.
// Copyright © 2013 Joel Gerber Co. All rights reserved.
//

#import “JGCItem.h”

@implementation JGCItem

  • (id)randomItem
    {
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”, @“Rusty”, @“Shiny”, nil];
    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”, @“Spork”, @“Mac”, nil];

    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    [randomAdjectiveList objectAtIndex:adjectiveIndex],
    [randomNounList objectAtIndex:nounIndex]];

    int randomValue = rand() % 100;

    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10];

    JGCItem *newItem =
    [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];

    return newItem;
    }

  • (id)init
    {
    return [self initWithItemName:@“Item"
    valueInDollars:0
    serialNumber:@”"];
    }

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

    if (self) {
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];

      dateCreated = [[NSDate alloc] init];
    

    }

    return self;
    }

  • (void)setItemName:(NSString *)str
    {
    itemName = str;
    }

  • (NSString *)itemName
    {
    return itemName;
    }

  • (void)setSerialNumber:(NSString *)str
    {
    serialNumber = str;
    }

  • (NSString *)serialNumber
    {
    return serialNumber;
    }

  • (void)setValueInDollars:(int)i
    {
    valueInDollars = i;
    }

  • (int)valueInDollars
    {
    return valueInDollars;
    }

  • (NSDate *)dateCreated
    {
    return dateCreated;
    }

  • (NSString *)description
    {
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
    itemName, serialNumber, valueInDollars, dateCreated];

    return descriptionString;
    }

  • (id)copyWithZone:(NSZone *)zone
    {
    id copy = [[[self class] alloc] init];

    if (copy) {
    [copy setItemName:[itemName copyWithZone:zone]];
    [copy setSerialNumber:[serialNumber copyWithZone:zone]];
    [copy setValueInDollars:self.valueInDollars];
    }

    return copy;
    }

@end[/code]

[code]//
// JGCContainer.h
// RandomPossessions
//
// Created by Joel Gerber on 2013-06-10.
// Copyright © 2013 Joel Gerber Co. All rights reserved.
//

#import “JGCItem.h”

@interface JGCContainer : JGCItem
{
NSString *containerName; // setter method creates a copy.
NSMutableArray *containerItems; // setter method creates a copy.
}

// Factory/Convenience Method.

  • (id)randomContainer:(NSUInteger)count;

// Initializers

  • (id)init;
  • (id)initWithContainerName:(NSString *)name;
  • (id)initWithContainerName:(NSString *)name
    andItem:(id)item;

// - Designated Initializer.

  • (id)initWithContainerName:(NSString *)name
    andItems:(NSArray *)array;

// Array Manipulators

  • (void)addItems:(NSArray *)items;
  • (void)addItem:(id)item;
  • (void)addRandomItem;

// Setters/Getters

  • (NSMutableArray *)containerItems;

  • (void)setContainerItems:(NSArray *)items;

  • (NSString *)containerName;

  • (void)setContainerName:(NSString *)name;

  • (NSUInteger)totalCost;

@end[/code]

[code]//
// JGCContainer.m
// RandomPossessions
//
// Created by Joel Gerber on 2013-06-10.
// Copyright © 2013 Joel Gerber Co. All rights reserved.
//

#import “JGCContainer.h”

@implementation JGCContainer

// Factory/Convenience Methods

  • (id)randomContainer:(NSUInteger)count
    {
    id result = [[self alloc] initWithContainerName:@“Random Container”];

    for (; count > 0; count–) {
    [result addRandomItem];
    }

    return result;
    }

// Initializers

  • (id)init
    {
    return [self initWithContainerName:nil andItems:nil];
    }

  • (id)initWithContainerName:(NSString *)name
    {
    return [self initWithContainerName:name andItems:nil];
    }

  • (id)initWithContainerName:(NSString *)name andItem:(id)item
    {
    NSArray *items = [NSArray arrayWithObject:item];
    return [self initWithContainerName:name andItems:items];
    }

// - Designated Initializer

  • (id)initWithContainerName:(NSString *)name andItems:(NSArray *)array
    {
    self = [super init];

    if (self) {
    // If passed a nil, set a default value.
    if (!name) {
    [self setContainerName:@“Unspecified”];
    }
    else [self setContainerName:name];

      // If passed a nil, create an empty array.
      if (!array) {
          [self setContainerItems:[NSArray array]];
      }
      else [self setContainerItems:[NSMutableArray arrayWithArray:array]];
    

    }

    return self;
    }

// Object Methods

  • (void)addItem:(id)item
    {
    if ([item isKindOfClass:[JGCItem class]]) {
    [containerItems addObject:[item copy]];
    }
    }

  • (void)addItems:(NSArray *)items
    {
    for (id item in items) {
    // This will end up being a copy of the item.
    [self addItem:item];
    }
    }

  • (void)addRandomItem
    {
    [containerItems addObject:[JGCItem randomItem]];
    }

// Accessor Methods (Getters/Setters)

  • (NSMutableArray *)containerItems
    {
    return containerItems;
    }

  • (void)setContainerItems:(NSArray *)items
    {
    NSMutableArray *array = [NSMutableArray array];

    if (items) {
    for (id item in items) {
    if ([item isKindOfClass:[JGCItem class]]) {
    [array addObject:[item copy]];
    }
    }
    containerItems = array;
    }
    else containerItems = [NSMutableArray array];
    }

  • (NSString *)containerName
    {
    return containerName;
    }

  • (void)setContainerName:(NSString *)name
    {
    if (name) {
    containerName = name;
    }
    else containerName = @“Unspecified”;
    }

  • (NSUInteger)totalCost
    {
    NSUInteger totalCost = 0;
    for (id item in containerItems) {
    if ([item isKindOfClass:[JGCContainer class]]) {
    totalCost += [item totalCost];
    }
    else totalCost += [item valueInDollars];
    }
    return totalCost;
    }

// NSCopying Implementation

  • (id)copyWithZone:(NSZone *)zone
    {
    id copy = [super copyWithZone:zone];

    if (copy) {
    [copy setContainerName:[[self containerName] copyWithZone:zone]];
    [copy setContainerItems:[[self containerItems] copyWithZone:zone]];
    }

    return copy;
    }

  • (NSString *)description
    {
    NSMutableString *result = [NSMutableString string];

    for (id item in [self containerItems]) {
    if ([item isKindOfClass:[JGCContainer class]]) {
    [result appendFormat:@"\nContainer: %@\n", [item containerName]];
    [result appendFormat:@"%@", item];
    [result appendFormat:@“End of Container: %@\n\n”, [item containerName]];
    }
    else [result appendFormat:@"%@\n", item];
    }

    return result;
    }

@end[/code]

[code]//
// main.m
// RandomPossessions
//
// Created by Joel Gerber on 2013-06-09.
// Copyright © 2013 Joel Gerber Co. All rights reserved.
//

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

int main(int argc, const char * argv[])
{
@autoreleasepool {
JGCContainer *container = [[JGCContainer alloc] initWithContainerName:@“container”];
JGCContainer *container2 = [[JGCContainer alloc] initWithContainerName:@“container2” andItems:@[[JGCItem randomItem], [JGCContainer randomContainer:2]]];

    [container addItem:@"This should be ignored"];
    [container addItems:@[[JGCContainer randomContainer:3], [JGCItem randomItem], [JGCItem randomItem], @"Blah", [JGCItem randomItem], [JGCContainer randomContainer:2], container2]];
    
    NSLog(@"\n%@", container);
    NSLog(@"Total Cost of Contained Items: $%ld", [container totalCost]);
}
return 0;

}[/code]

Output:

[code]2013-06-11 15:56:41.365 RandomPossessions[6334:303]

Container: Random Container
Rusty Bear (8G5V6): Worth $99, recorded on 2013-06-11 19:56:41 +0000
Shiny Spork (3P9B1): Worth $10, recorded on 2013-06-11 19:56:41 +0000
Rusty Mac (6R5C1): Worth $93, recorded on 2013-06-11 19:56:41 +0000
End of Container: Random Container

Fluffy Spork (3E4O0): Worth $1, recorded on 2013-06-11 19:56:41 +0000
Fluffy Mac (3A6T4): Worth $30, recorded on 2013-06-11 19:56:41 +0000
Shiny Spork (8S3I1): Worth $77, recorded on 2013-06-11 19:56:41 +0000

Container: Random Container
Rusty Spork (4F6F9): Worth $65, recorded on 2013-06-11 19:56:41 +0000
Fluffy Spork (1P5F4): Worth $29, recorded on 2013-06-11 19:56:41 +0000
End of Container: Random Container

Container: container2
Rusty Spork (8Q2U8): Worth $73, recorded on 2013-06-11 19:56:41 +0000

Container: Random Container
Shiny Spork (5Y2V3): Worth $40, recorded on 2013-06-11 19:56:41 +0000
Rusty Spork (2F9Z7): Worth $40, recorded on 2013-06-11 19:56:41 +0000
End of Container: Random Container

End of Container: container2

2013-06-11 15:56:41.366 RandomPossessions[6334:303] Total Cost of Contained Items: $557
[/code]