My Challenge Version


#1

Here is my version: :wink:

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

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

@property float purchaseSharePrice,currentSharePrice;
@property int numberOfShares;


-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n;

-(float) costInDollars;
-(float) valueInDollars;

@end
//
//  StockHolding.m
//  NerdCp17Challenge
//
#import "StockHolding.h"

@implementation StockHolding

@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n
{
    purchaseSharePrice = p;
    currentSharePrice = c;
    numberOfShares = n;
}

-(float) costInDollars
{
    return purchaseSharePrice * numberOfShares;
}
-(float) valueInDollars
{
    return currentSharePrice * numberOfShares;
}
@end
//  main.m
//  NerdCp17Challenge
//

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

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

    @autoreleasepool {
        
        StockHolding *stock1 = [[StockHolding alloc] init];
        StockHolding *stock2 = [[StockHolding alloc] init];
        StockHolding *stock3 = [[StockHolding alloc] init];
        
        [stock1 setPurchaseSharePrice:2.30 andCurrentSharePrice:4.50 andNumberOfShares:40];
        [stock2 setPurchaseSharePrice:12.19 andCurrentSharePrice:10.56 andNumberOfShares:90];
        [stock3 setPurchaseSharePrice:45.10 andCurrentSharePrice:49.51 andNumberOfShares:210];
        
        NSMutableArray *stockHolder = [NSMutableArray arrayWithObjects:stock1, stock2, stock3, nil];
        
        NSUInteger i = 0;
        for (StockHolding *stock in stockHolder)
            NSLog(@"\nstock[%li]: \nPurchase Share Price: %.2f \nCurrent Share Price: %.2f \nNumber Of Shares: %i \nCost in Dollars: %.2f \nValue in Dollars: %.2f\n\n\n", i++, [stock purchaseSharePrice], 
                [stock currentSharePrice], [stock numberOfShares], [stock costInDollars], [stock valueInDollars]);

    }
    return 0;
}

Output:

2012-08-10 03:05:01.613 NerdCp17Challenge[3753:4503] 
stock[0]: 
Purchase Share Price: 2.30 
Current Share Price: 4.50 
Number Of Shares: 40 
Cost in Dollars: 92.00 
Value in Dollars: 180.00


2012-08-10 03:05:01.616 NerdCp17Challenge[3753:4503] 
stock[1]: 
Purchase Share Price: 12.19 
Current Share Price: 10.56 
Number Of Shares: 90 
Cost in Dollars: 1097.10 
Value in Dollars: 950.40


2012-08-10 03:05:01.617 NerdCp17Challenge[3753:4503] 
stock[2]: 
Purchase Share Price: 45.10 
Current Share Price: 49.51 
Number Of Shares: 210 
Cost in Dollars: 9471.00 
Value in Dollars: 10397.10


Program ended with exit code: 0