Gold Challenge: Value of Container [Help]


#1

For the Gold Challenge, I have used a class method to generate a random set of names for a array of containers. I have also been able to give the containers a randomly generated list of items.

However, I’m having some trouble singling out the value of the items to sum them up to give the value of the container.

Am I approaching this challenge correctly?

Thanks in advance!

Main:

[code]#import <Foundation/Foundation.h>

#import “BNRItem.h”
#import “BNRContainter.h”

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

@autoreleasepool {

NSMutableArray *containers = [[NSMutableArray alloc] init];

    for (int i = 0; i <10; i++) {
        BNRContainter *c = [BNRContainter randomItem];
                            [containers addObject:c];
        
    }
                            for (BNRContainter *container in containers) {
                                NSLog(@"%@", container);
                            }


}
return 0;

}
[/code]

BNRContainer.h

#import "BNRItem.h"

@interface BNRContainter : BNRItem
{
    NSString *containerName;
    NSMutableArray *itemsList;
    int valueOfContainer;
    
}

- (id) initWithContainerName: (NSString *)cName
            valueOfContainer:(int)cValue
                   itemsList: (NSMutableArray *)cList;

- (void) setContainerName: (NSString *)str;
- (NSString *)containerName;

- (void) setItemsList: (NSMutableArray *)ary;
- (NSMutableArray *)itemsList;

- (void) setValueOfContainer: (int)i;
- (int)valueOfContainer;



@end

BNRContainer.m

#import "BNRContainter.h"

@implementation BNRContainter

- (id) initWithContainerName:(NSString *)cName
            valueOfContainer:(int)cValue
                   itemsList:(NSMutableArray *)cList
{
    self = [super init];
    if (self) {
    [self setContainerName:cName];
    [self setValueOfContainer:cValue];
    [self setItemsList:cList];
    }
        return self;
}

// class method to generate names

+ (id) randomItem
{
    NSArray *firstWord = [NSArray arrayWithObjects:@"Box", @"A sack", @"Truck of", nil];
    
    NSArray *secondWord = [NSArray arrayWithObjects:@"Tennis Balls", @"Super Hero Stuff", @"illegals", nil];
    
    NSInteger firstIndex = rand() % [firstWord count];
    NSInteger secondIndex = rand() % [secondWord count];
    
    NSString *randomContainerName = [NSString stringWithFormat:@"%@ %@", [firstWord objectAtIndex:firstIndex], [secondWord objectAtIndex:secondIndex]];
    
    
    NSMutableArray *items = [[NSMutableArray alloc]init];
    for (int i = 0; i<10; i++) {
    BNRItem *n = [BNRItem randomItem];
        [items addObject:n];
    }

    NSInteger itemsIndex = rand() % [items count];
    NSInteger itemsIndex2 = rand() % [items count];
    NSInteger itemsIndex3 = rand() % [items count];
    
    NSMutableArray *itemsInContainer = [NSString stringWithFormat:@"%@, %@, %@", [items objectAtIndex:itemsIndex], [items objectAtIndex:itemsIndex2], [items objectAtIndex:itemsIndex3]];
    
    
    
    
    
    
    BNRContainter *newContainer = [[self alloc] initWithContainerName:randomContainerName valueOfContainer:0 itemsList:itemsInContainer];
    
    return newContainer;
}


// Setting and Getting declarations here: 
- (void)setContainerName:(NSString *)str
{
    containerName = str;
}
- (NSString *)containerName
{
    return containerName;
}
- (void)setValueOfContainer:(int)i
{
    valueOfContainer = i;
}
-(int)valueOfContainer
{
    return valueOfContainer;
}
- (void)setItemsList:(NSMutableArray *)ary
{
    itemsList = ary;
}
- (NSMutableArray *)itemsList
{
    return itemsList;
}


// Overiding the description method to fit what i need it to say

- (NSString *)description
{
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ is worth a total of %d, containing %@", containerName, valueOfContainer, itemsList];
    
    return descriptionString;
}
@end

So when I run the program this is the output:

[code]2013-03-14 15:41:02.477 Random Possessions[15616:303] A sack of Super Hero Stuff is worth a total of 0, containing Fluffy boldozer plug (6F9L4): Worth $63, recorded on 2013-03-14 22:41:02 +0000, Fluffy Building (2V3R9): Worth $92, recorded on 2013-03-14 22:41:02 +0000, Fluffy boldozer plug (6T4D3): Worth $22, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.478 Random Possessions[15616:303] Trucks full of Tennis Balls is worth a total of 0, containing Shiny Zombies (4O5W3): Worth $57, recorded on 2013-03-14 22:41:02 +0000, Fluffy Zombies (1L0N8): Worth $12, recorded on 2013-03-14 22:41:02 +0000, Shiny Zombies (4O5W3): Worth $57, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.478 Random Possessions[15616:303] Trucks full of Tennis Balls is worth a total of 0, containing Rusty Zombies (9K5X6): Worth $93, recorded on 2013-03-14 22:41:02 +0000, Fluffy boldozer plug (0Y4X9): Worth $72, recorded on 2013-03-14 22:41:02 +0000, Rusty boldozer plug (9Z5I5): Worth $72, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.479 Random Possessions[15616:303] Boxes of Tennis Balls is worth a total of 0, containing Shiny Zombies (9H9U9): Worth $14, recorded on 2013-03-14 22:41:02 +0000, Shiny Zombies (3Q0Q0): Worth $76, recorded on 2013-03-14 22:41:02 +0000, Shiny Building (7F8W9): Worth $66, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.479 Random Possessions[15616:303] Trucks full of Super Hero Stuff is worth a total of 0, containing Fluffy Building (6U4O9): Worth $3, recorded on 2013-03-14 22:41:02 +0000, Shiny Building (0Y8D5): Worth $93, recorded on 2013-03-14 22:41:02 +0000, Shiny boldozer plug (0W2H3): Worth $6, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.480 Random Possessions[15616:303] Trucks full of illegals is worth a total of 0, containing Fluffy Building (1T8G5): Worth $25, recorded on 2013-03-14 22:41:02 +0000, Fluffy Building (1H5M8): Worth $3, recorded on 2013-03-14 22:41:02 +0000, Fluffy Building (1H5M8): Worth $3, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.481 Random Possessions[15616:303] Trucks full of Tennis Balls is worth a total of 0, containing Shiny boldozer plug (6V0I1): Worth $76, recorded on 2013-03-14 22:41:02 +0000, Shiny boldozer plug (3E3B7): Worth $11, recorded on 2013-03-14 22:41:02 +0000, Shiny boldozer plug (6V0I1): Worth $76, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.481 Random Possessions[15616:303] Trucks full of illegals is worth a total of 0, containing Rusty boldozer plug (9L6I2): Worth $22, recorded on 2013-03-14 22:41:02 +0000, Rusty boldozer plug (9L6I2): Worth $22, recorded on 2013-03-14 22:41:02 +0000, Shiny Zombies (4D7S9): Worth $41, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.482 Random Possessions[15616:303] Trucks full of illegals is worth a total of 0, containing Shiny boldozer plug (9J3X1): Worth $23, recorded on 2013-03-14 22:41:02 +0000, Shiny boldozer plug (1J2M2): Worth $61, recorded on 2013-03-14 22:41:02 +0000, Fluffy Building (4K1M2): Worth $11, recorded on 2013-03-14 22:41:02 +0000

2013-03-14 15:41:02.482 Random Possessions[15616:303] Boxes of illegals is worth a total of 0, containing Shiny boldozer plug (7X6Q9): Worth $50, recorded on 2013-03-14 22:41:02 +0000, Shiny Zombies (8L3H9): Worth $10, recorded on 2013-03-14 22:41:02 +0000, Fluffy Zombies (5P1H5): Worth $1, recorded on 2013-03-14 22:41:02 +0000[/code]


#2

In your randomItem method at the end you make an instance of BNRContainter called newContainer and you set the valueOfContainer to 0.

[code]+ (id) randomItem

BNRContainter *newContainer = [[self alloc] initWithContainerName:randomContainerName valueOfContainer:0 itemsList:itemsInContainer];

return newContainer;

}
[/code]

So in your description method you make a call to valueOfContainer which is set to 0. You need to have your description method add the value of all the items in the container together and then print that value. Here is what mine looks like…

- (NSString *) description
{
    int totalValue = 0;
    
    for (BNRItem *item in self.containerArray) {
        totalValue += item.valueInDollars;
    }

    NSString *string = [[NSString alloc] initWithFormat:@"%@: Total Value: $%d Items:%@", self.containerName, (totalValue + self.containerValue), self.containerArray];
    
    return string;
}

In mine I declare a new int called totalValue which I then go through my BNRContainer to find all the values of the objects inside the container adding them to this new totalValue. Then after getting all the totals in my description I print out the totalValue + the container value.

Hope that helps!