My Stocks solution


#1

Here is my solution to the stocks challenge. Any comments would be appreciated. Thanks.

BNRStockHolding.h file

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

{
    float _purchasePrice;
    float _currentPrice;
    int _numberOfShares;
}

-(float)purchasePrice;
-(void)setPurchasePrice:(float)purchasePrice;

-(float)currentPrice;
-(void)setCurrentPrice:(float)currentPrice;

-(int)numberOfShares;
-(void)setNumberOfShares:(int)numberOfShares;

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

@end

BNRStockHolding.m

[code]@implementation BNRStockHolding

// setter and getter for purchase price instance variable
-(float)purchasePrice
{
return _purchasePrice;
}
-(void)setPurchasePrice:(float)purchasePrice
{
_purchasePrice = purchasePrice;
}

// setter and getter for currentPrice
-(float)currentPrice
{
return _currentPrice;
}
-(void)setCurrentPrice:(float)currentPrice
{
_currentPrice = currentPrice;
}

// setter and getter for numberOfSharess
-(int)numberOfShares
{
return _numberOfShares;
}
-(void)setNumberOfShares:(int)numberOfShares
{
_numberOfShares = numberOfShares;
}

// define the custom methods for the class
-(float)costInDollars
{
return _purchasePrice * _numberOfShares;
}
-(float)valueInDollars
{
return _currentPrice * _numberOfShares;
}

@end
[/code]

main.m file

[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

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

@autoreleasepool {
    
    // create the first instance of BNRStockHolding with unique instance variables
    BNRStockHolding *firstStock = [[BNRStockHolding alloc]init];
    [firstStock setNumberOfShares:40];
    [firstStock setPurchasePrice:2.30];
    [firstStock setCurrentPrice:4.50];
    
    // create the second instance of BNRStockHolding with unique instance variables
    BNRStockHolding *secondStock = [[BNRStockHolding alloc]init];
    [secondStock setNumberOfShares:90];
    [secondStock setPurchasePrice:12.19];
    [secondStock setCurrentPrice:10.56];
    
    // create the third instance of BNRStockHolding with unique instance variables
    BNRStockHolding *thirdStock = [[BNRStockHolding alloc]init];
    [thirdStock setNumberOfShares:210];
    [thirdStock setPurchasePrice:45.10];
    [thirdStock setCurrentPrice:49.51];
    
    NSMutableArray *myStocks = [[NSMutableArray alloc]init];
    [myStocks addObject:firstStock];
    [myStocks addObject:secondStock];
    [myStocks addObject:thirdStock];
    
    // iterate through the array
    NSUInteger stockCount = [myStocks count];
    for (int i = 0; i < stockCount; i++){
        BNRStockHolding *s = myStocks[i];
        NSLog(@"Stock number %d has the following values:", i + 1);
        NSLog(@"Purchase price: %.2f", [s purchasePrice]);
        NSLog(@"Current price: %.2f", [s currentPrice]);
        NSLog(@"Number of shares: %d", [s numberOfShares]);
        NSLog(@"Current value: %.2f", [s valueInDollars]);
        NSLog(@"Total cost: %.2f", [s costInDollars]);
        NSLog(@"Gain/Loss: %.2f", [s valueInDollars] - [s costInDollars]);
    }
    
}
return 0;

}
[/code]

I know you could do it shorter with @property but I was trying to do it they way we had learned so far in the book. Thanks!