My Gold Challenge Solution


#1

It took a while to wrap my head around
what was going on but I got something
to work.
How would you add the 'value in dollars’
of all items in the array so that you don’t
have to type it in manually? Or is there a
more simple way?

main.m

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

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

@autoreleasepool {
    
    // Items information for input
    BNRItem *itemOne = [[BNRItem alloc] init];
    itemOne.electronicName = @"Mac Pro";
    itemOne.valueInDollars = 5000;
    
    BNRItem *itemTwo = [[BNRItem alloc] init];
    itemTwo.electronicName = @"Monitor";
    itemTwo.valueInDollars = 700;

    BNRItem *itemThree = [[BNRItem alloc] init];
    itemThree.electronicName = @"Speakers";
    itemThree.valueInDollars = 200;

    BNRItem *itemFour = [[BNRItem alloc] init];
    itemFour.electronicName = @"MacBook";
    itemFour.valueInDollars = 1100;

    BNRItem *itemFive = [[BNRItem alloc] init];
    itemFive.electronicName = @"Wacom Tablet";
    itemFive.valueInDollars = 100;
    
    // Create a mutable array object, store its address in items variable
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:itemOne];
    [items addObject:itemTwo];
    [items addObject:itemThree];
    [items addObject];
    [items addObject];
    
    // Creating container and adding information to it
    BNRContainer *containerOne = [[BNRContainer alloc] init];
    containerOne.cName = @"Electronics";
    containerOne.cValue = 7100;
    containerOne.contents = items;
    containerOne.itemNumber = [items count];
    
    // Displaying results
    NSLog(@"%@", containerOne);
    
    // Destroy the mutable array object
    items = nil;
}
return 0;

}

[/code]

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSString *_cName;
int _cValue;
NSMutableArray *_contents;
int _itemNumber;
}

  • (instancetype)initWithName:(NSString *)cName cValue:(int)cValue itemNumber:(int) itemNumber contents:(NSMutableArray *)contents;

  • (void)setCName:(NSString *)n;

  • (NSString *)cName;

-(void)setCValue:(int)cv;
-(int)cValue;

  • (void)setContents:(NSMutableArray *)c;
    -(NSMutableArray *)contents;

-(void)setItemNumber:(int)i;
-(int)itemNumber;

@end
[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

  • (instancetype)initWithName:(NSString *)cName cValue:(int)cValue itemNumber:(int) itemNumber contents:(NSMutableArray *)contents
    {
    self = [super init];

    if (self) {
    _cName = cName;
    _cValue = cValue;
    _contents = contents;
    }
    return self;
    }

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"\nThe %@ container is worth $%d\nThe container has %d items inside\n The items include:\n%@", self.cName, self.cValue, self.itemNumber, self.contents];

    return descriptionString;
    }

  • (void)setCName:(NSString *)n;
    {
    _cName = n;
    }

  • (NSString *)cName;
    {
    return _cName;
    }

-(void)setCValue:(int)cv;
{
_cValue = cv;
}
-(int)cValue;
{
return _cValue;
}

  • (void)setContents:(NSMutableArray *)c;
    {
    _contents = c;
    }

-(NSMutableArray *)contents;
{
return _contents;
}

-(void)setItemNumber:(int)i;
{
_itemNumber = i;
}
-(int)itemNumber;
{
return _itemNumber;
}

@end
[/code]

Ouput result:

[quote]2014-03-05 15:13:34.459 RandomItems[18893:303]
The Electronics container is worth $7100
The container has 5 items inside
The items include:
(
“Item:Mac Pro Worth:$5000”,
“Item:Monitor Worth:$700”,
“Item:Speakers Worth:$200”,
“Item:MacBook Worth:$1100”,
“Item:Wacom Tablet Worth:$100”
)
Program ended with exit code: 0[/quote]


#2

[quote]How would you add the 'value in dollars’
of all items in the array so that you don’t
have to type it in manually? Or is there a
more simple way?[/quote]
Here is how I would do it:

struct ItemInfo
{
    char *name;
    long value;
};

struct ItemInfo myItems [] = {
    {"Foo",    11},
    {"Bar",    13},
    {"FooBar", 17}
};

const unsigned long MY_NUM_ITEMS = sizeof (myItems)/sizeof (myItems [0]);

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        // Create and store items
        NSMutableArray *items = [[NSMutableArray alloc] init];
        long totalValue = 0;
        for (unsigned long x = 0; x < MY_NUM_ITEMS; ++x)
        {
            BNRItem *item = [[BNRItem alloc] init];
            item.electronicName = [NSString stringWithUTF8String:myItems [x].name];
            item.valueInDollars = myItems [x].value;
            [items addObject:item];
            totalValue += item.valueInDollars;
        }
        // Creating container and adding information to it
        BNRContainer *containerOne = [[BNRContainer alloc] init];
        containerOne.cName = @"Electronics";
        containerOne.cValue = totalValue;
        containerOne.contents = items;
        containerOne.itemNumber = [items count];
        
        // Displaying results
        NSLog(@"%@", containerOne);
        
        // Destroy the mutable array object
        items = nil;
    }
    return 0;
}

Or even better, rather than setting the total value manually, you could ask the BNRContainer to compute it (on demand) when you access the cValue property.

@implementation BNRContainer
...
- (int)cValue;
{
    _cValue = 0;
    for (BNRItem *item in self.contents)
    {
        _cValue += [item valueInDollars];
    }
    return _cValue;
}
...

#3

Thanks ibex10!

The 2nd suggestion is what I had in mind :slight_smile:

Cheers!