How is this solution?


Here is BNRStockHolding.h

[code]#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

//This class represents a stock we have purchased
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;

//Use properties to create accessor methods
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars; //purchaseSharePrice * numberOfShares
  • (float)valueInDollars; //currentSharePrice * numberOfShares


Here is implementation

#import "BNRStockHolding.h"

@implementation BNRStockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

-(float)costInDollars {
    return purchaseSharePrice * numberOfShares;

-(float)valueInDollars {
    return currentSharePrice * numberOfShares;


Here is main()

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

@autoreleasepool {
    BNRStockHolding *stock = [[BNRStockHolding alloc]init];
    //create instance of BNRStockHolding for stock one
    BNRStockHolding *stockOne = [BNRStockHolding new];
    stockOne.purchaseSharePrice = 2.30;
    stockOne.currentSharePrice = 4.50;
    stockOne.numberOfShares = 40;
    //create instance of BNRStockHolding for stock two
    BNRStockHolding *stockTwo = [BNRStockHolding new];
    stockTwo.purchaseSharePrice = 12.19;
    stockTwo.currentSharePrice = 10.56;
    stockTwo.numberOfShares = 90;
    //create instance of BNRStockHolding for stock three
    BNRStockHolding *stockThree = [BNRStockHolding new];
    stockThree.purchaseSharePrice = 45.10;
    stockThree.currentSharePrice = 49.51;
    stockThree.numberOfShares = 210;
    // create an array
    NSArray *stockList = @[stockOne, stockTwo, stockThree];
    //Itterate through array and print out each value
    for (BNRStockHolding *sh in stockList) {
        NSLog(@"Purchase Price: %.2f Current Price: %.2f Number of Shares: %d Cost In Dollars: %.2f Value In Dollars: %.2f", sh.purchaseSharePrice, sh.currentSharePrice, sh.numberOfShares, sh.costInDollars, sh.valueInDollars);
return 0;


And the main question I have: Is there a better way to print out the the results. I iterated over each instance of BNRStockHolding in my array pointed to by *sh but when it came time to print, I had to use: sh.currentStockPrice, sh.purchaseStockPrice ect… Is it ok to do this?