Gold Challenge: Self Generating Container


#1

Had a lot of fun on this project. You can initialize a container with a set number of random items.

Main.m

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        CMKContainer *lunchBox = [[CMKContainer alloc]initWithNumRandItems:10
                                                                  ItemName:@"Lunch Box"
                                                              serialNumber:@"C1Z2S"
                                                            valueInDollars:100];
        
        NSLog(@"%@",lunchBox);
        
        CMKContainer *paperBag = [CMKContainer new];
        
        [lunchBox addItem];
        
        NSLog(@"%@",lunchBox);
        
    }
    return 0;
}

OutPut

Lunch Box: item number C1Z2S is worth $100 and was created on 2014-10-10 19:25:34 +0000.It's contents are worth $1236. The total value is $1336. It's contents are (
    "Foamy Candy: item number L1Q8J is worth $108 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Underwear: item number S6B0O is worth $90 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Kitten: item number P6T3N is worth $165 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Underwear: item number B0T7V is worth $40 and was created on 2014-10-10 19:25:34 +0000.",
    "Foamy Underwear: item number Z8P1P is worth $124 and was created on 2014-10-10 19:25:34 +0000.",
    "Jiggly Candy: item number B8E1R is worth $164 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Candy: item number F1M6I is worth $162 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Underwear: item number O1L9U is worth $45 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Soap: item number F6O7A is worth $187 and was created on 2014-10-10 19:25:34 +0000.",
    "Foamy Kitten: item number Z3F0J is worth $151 and was created on 2014-10-10 19:25:34 +0000."
)
// added a container to the array
 Lunch Box: item number C1Z2S is worth $100 and was created on 2014-10-10 19:25:34 +0000.It's contents are worth $1236. The total value is $1336. It's contents are (
    "Foamy Candy: item number L1Q8J is worth $108 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Underwear: item number S6B0O is worth $90 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Kitten: item number P6T3N is worth $165 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Underwear: item number B0T7V is worth $40 and was created on 2014-10-10 19:25:34 +0000.",
    "Foamy Underwear: item number Z8P1P is worth $124 and was created on 2014-10-10 19:25:34 +0000.",
    "Jiggly Candy: item number B8E1R is worth $164 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Candy: item number F1M6I is worth $162 and was created on 2014-10-10 19:25:34 +0000.",
    "Glowing Underwear: item number O1L9U is worth $45 and was created on 2014-10-10 19:25:34 +0000.",
    "Gooey Soap: item number F6O7A is worth $187 and was created on 2014-10-10 19:25:34 +0000.",
    "Foamy Kitten: item number Z3F0J is worth $151 and was created on 2014-10-10 19:25:34 +0000.",
    "CMK Corp. Item: item number 01011 is worth $0 and was created on 2014-10-10 19:25:34 +0000.It's contents are worth $0. The total value is $0. It's contents are (null)\n"
)
Program ended with exit code: 0

CMKContainer.h

#import "CMKItem.h"

@interface CMKContainer : CMKItem

@property (nonatomic, readonly)NSMutableArray *containedItems;

- (instancetype)initWithNumRandItems:(int)amount
                            ItemName:(NSString *)itemName
                        serialNumber:(NSString *)serialNumber
                      valueInDollars:(int)valueInDollars;

- (int)totalInclusiveValue;
- (int)totalItemValue;
- (NSArray *)contents;

- (void)addItem:(CMKItem *)item;



@end

CMKContainer.m

#import "CMKContainer.h"

@implementation CMKContainer

- (instancetype)initWithNumRandItems:(int)amount
                            ItemName:(NSString *)itemName
                        serialNumber:(NSString *)serialNumber
                      valueInDollars:(int)valueInDollars
{
    self = [super initWithItemName:itemName
                      serialNumber:serialNumber
                    valueInDollars:valueInDollars];
    
    if (self) {
        _containedItems = [[NSMutableArray alloc]init];
        
        for (int i = 0; i < amount; i++) {
            CMKItem *newItem = [CMKItem randomItem];
            [self addItem:newItem];
        }
    }
    
    return self;
    
}

#pragma mark - Overridden Methods
- (NSString *)description
{
    return [[super description] stringByAppendingFormat:@"It's contents are worth $%d. The total value is $%d. It's contents are %@\n",self.totalItemValue, self.totalInclusiveValue, self.contents];
}

- (NSArray *)contents
{
    return [_containedItems copy];
}
- (int)totalItemValue
{
    int sum = 0;
    
    for (CMKItem *n in _containedItems) {
        sum += n.valueInDollars;
    }
    
    return sum;
}
- (int)totalInclusiveValue
{
    int sum = self.totalItemValue;
    
    sum += self.valueInDollars;
    
    return sum;
}

- (void)addItem:(CMKItem *)item
{
    [_containedItems addObject:item];
}



@end