//
// BNRStockHolding.h
// Stocks
//
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}
- (float)costInDollars;
- (float)valueInDollars;
- (float) purchaseSharePrice;
- (float) currentSharePrice;
- (int) numberOfShares;
- (void) setPurchaseSharePrice:(float)psp;
- (void) setCurrentSharePrice:(float)csp;
- (void) setNumberOfShares:(int)numshares;
@end
//
// BNRStockHolding.m
// Stocks
//
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
#import "BNRStockHolding.h"
@implementation BNRStockHolding
- (float)costInDollars;
{
return [self purchaseSharePrice] * [self numberOfShares];
}
- (float)valueInDollars;
{
return [self currentSharePrice] * [self numberOfShares];
}
- (float) purchaseSharePrice;
{
return _purchaseSharePrice;
}
- (float) currentSharePrice;
{
return _currentSharePrice;
}
- (int) numberOfShares;
{
return _numberOfShares;
}
- (void) setPurchaseSharePrice:(float)psp;
{
_purchaseSharePrice = psp;
}
- (void) setCurrentSharePrice:(float)csp;
{
_currentSharePrice = csp;
}
- (void) setNumberOfShares:(int)numshares;
{
_numberOfShares = numshares;
}
@end
main…
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *portfolio = [[NSMutableArray alloc] init];
BNRStockHolding *stock = [[BNRStockHolding alloc] init];
[stock setPurchaseSharePrice:2.30];
[stock setCurrentSharePrice:4.50];
[stock setNumberOfShares:40];
[portfolio addObject:stock];
stock = nil;
stock = [[BNRStockHolding alloc] init];
[stock setPurchaseSharePrice:12.19];
[stock setCurrentSharePrice:10.56];
[stock setNumberOfShares:90];
[portfolio addObject:stock];
stock = nil;
stock = [[BNRStockHolding alloc] init];
[stock setPurchaseSharePrice:45.10];
[stock setCurrentSharePrice:49.51];
[stock setNumberOfShares:210];
[portfolio addObject:stock];
int i = 0;
for (BNRStockHolding *s in portfolio ){
i++;
NSLog(@"Stock: %i- Cost: %.2f Shares: %i Value: %.2f", i, [s costInDollars], [s numberOfShares], [s valueInDollars] );
}
}
return 0;
}
I approached it a bit differently then some of the others. I created only one instance of BNRStockHolding, set the values, added it to the array, then wiped it clean and reallocated it. It achieved the same thing but was just trying something different