GOLDEN CHALENGE: variable argument list


#1

Hi everybody, my solutions contains variable arguments, i hope that the code is correct :slight_smile:

//BNRContainer.h

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
}

-(void) appendObject:(BNRItem *)object;
-(void) appendObjects:(BNRItem *) firstObject, ... NS_REQUIRES_NIL_TERMINATION;

@end
//BNRContainer.m

#import "BNRContainer.h"

// Declare our instance variables
@interface BNRContainer ()
{
    NSMutableArray *bnrItems;
}

@end

@implementation BNRContainer
-(id) initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
{
    self = [super initWithItemName:name valueInDollars:value serialNumber:sNumber];
    bnrItems = [[NSMutableArray alloc]init];
    
    return self;
}

-(id) initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
{
    return [self initWithItemName:name valueInDollars:0 serialNumber:sNumber];
}

-(id) init
{
    return [self initWithItemName:@"Unnamed Container" valueInDollars:0 serialNumber:@"AAAAAA"];
}



-(void) appendObject:(BNRItem *)object
{
    [bnrItems addObject:object];
}

//variadic
-(void) appendObjects:(BNRItem *)firstObject, ...
{
    BNRItem *eachObject;
    va_list argumentList;
    
    //The first argument isnt part of the varargs list
    if (firstObject)    
    {
        //so we'll handle it separately
        [self appendObject:firstObject];    
        
        //Start scanning for argumentes after firstObject
        va_start(argumentList, firstObject);
        while ((eachObject = va_arg(argumentList, BNRItem *)))
        {
            [self appendObject:eachObject];
        }
        va_end(argumentList);
    }
}
-(NSString *) description
{
       
    for (BNRItem *item in bnrItems)
    {
        self.valueInDollars += item.valueInDollars;
    }
    
    
   // NSLog(@"Name: %@ Value: %d$",nameOfContainer,sum);
    
    for (BNRItem *item in bnrItems)
    {
        NSLog(@"%@",[item description]);
    }

    return [NSString stringWithFormat:@"\n\nContainer name: %@ \nValue: %d$ \nSerial Number: %@ \nItems: %@"
                                        ,self.itemName
                                        ,self.valueInDollars
                                        ,self.serialNumber
                                        ,bnrItems
            ];
    
}
@end
//main.m

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

    @autoreleasepool {
        
        //Creating a mutable array object, store its address in "itmes" variable
        NSMutableArray *items = [[NSMutableArray alloc]init];
       
        for (int i = 0; i < 10; i++)
        {
            BNRItem *p = [BNRItem randomItem];
            [items addObject:p];
        }
        
        for (BNRItem *item in items)
        {
            NSLog(@"%@",item);
        }
        items = nil;
        
        //Silver Challenge
        BNRItem *silverItem = [[BNRItem alloc]initWithItemName:@"IKI" serialNumber:@"A1205"];
        NSLog(@"%@",silverItem);
        
        //Gold Challenge
        //1.BASIC
        BNRItem *goldItem = [[BNRItem alloc]initWithItemName:@"GOLD" valueInDollars:100 serialNumber:@"A1C2B3"];
         BNRItem *goldItem1 = [[BNRItem alloc]initWithItemName:@"GOLD" valueInDollars:100 serialNumber:@"A1C2B3"];
        
        BNRContainer *container = [[BNRContainer alloc]init];
        [container appendObject];
        [container appendObject:goldItem1];
        
        NSLog(@"%@",[container description]);
        
        //2.ADVANCED
        BNRContainer *container2 = [[BNRContainer alloc]initWithItemName:@"CONTAINER2" serialNumber:@"A1111"];
        [container2 appendObjects:goldItem,goldItem1,nil];
        
        NSLog(@"%@",[container2 description]);
         
    }
    return 0;
}