Aturin's challenge solution


#1

StockHolding.h

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
    
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(void)addYourselfToArray:(NSMutableArray *)theArray;
-(float)costInDollars;
-(float)valueInDollars;

@end

StockHolding.m

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice;
@synthesize currentSharePrice;
@synthesize numberOfShares;

-(void)addYourselfToArray:(NSMutableArray *)theArray
{
    [theArray addObject:self];
}

-(float)costInDollars
{
    return [self purchaseSharePrice] * [self numberOfShares];
}

-(float)valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares];
}

@end

main.m

#import <Foundation/Foundation.h>
#import "StockHolding.h"

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

    @autoreleasepool {
        NSMutableArray *stockArray = [NSMutableArray array];
        StockHolding *AAPL = [[StockHolding alloc] init];
        StockHolding *GOOG = [[StockHolding alloc] init];
        StockHolding *INTC = [[StockHolding alloc] init];
        [AAPL setPurchaseSharePrice:2.30];
        [AAPL setCurrentSharePrice:4.50];
        [AAPL setNumberOfShares:40];
        [AAPL addYourselfToArray:stockArray];

        [GOOG setPurchaseSharePrice:12.19];
        [GOOG setCurrentSharePrice:10.56];
        [GOOG setNumberOfShares:90];
        [GOOG addYourselfToArray:stockArray];
        
        [INTC setPurchaseSharePrice:45.10];
        [INTC setCurrentSharePrice:49.51];
        [INTC setNumberOfShares:210];
        [INTC addYourselfToArray:stockArray];
        
        int i = 0;
        for(StockHolding *sh in stockArray) {
            NSLog(@"Stock %d: cost is $%.2f, value is $%.2f.", i++, [sh costInDollars], [sh valueInDollars]);
        }
    }
    return 0;
}

NSLog:

2012-09-02 18:18:26.548 Stocks[8559:403] Stock 0: cost is $92.00, value is $180.00.
2012-09-02 18:18:26.550 Stocks[8559:403] Stock 1: cost is $1097.10, value is $950.40.
2012-09-02 18:18:26.550 Stocks[8559:403] Stock 2: cost is $9471.00, value is $10397.10.