BNR Stockhold challenge printing all items in the array


#1

//
// BNRStockHolding.h
// Stocks

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

@interface BNRStockHolding : NSObject

//BNRStockholding has two instance variables
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;


//BNRStockholding has a method that caluclates cost in dollars and value in dollars

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

@end

// BNRStockHolding.m
// Stocks

#import “BNRStockHolding.h”

@implementation BNRStockHolding

-(float) costInDollars
{
float p = [self purchaseSharePrice];
float n = [self numberOfShares];
return p * n;
}
-(float) valueInDollars
{
float c = [self currentSharePrice];
float n = [self numberOfShares];
return c * n;
}
@end


//
// main.m
// Stocks

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

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

@autoreleasepool {

//Create an instance of the stocks
BNRStockHolding *firstStock = [[BNRStockHolding alloc] init];
BNRStockHolding *secondStock = [[BNRStockHolding alloc] init];
BNRStockHolding *thirdStock = [[BNRStockHolding alloc] init];

//Populate the 1st Stock
firstStock.purchaseSharePrice = 2.3;
firstStock.currentSharePrice = 4.5;
firstStock.numberOfShares = 40;

//Populate the 2nd Stock
secondStock.purchaseSharePrice = 12.19;
secondStock.currentSharePrice = 10.56;
secondStock.numberOfShares = 90;

//Populate the 3rd stock
thirdStock.purchaseSharePrice = 45.1;
thirdStock.currentSharePrice = 49.51;
thirdStock.numberOfShares = 210;

//Create an array to hold the stocks
NSMutableArray *BNRStock = [[NSMutableArray alloc] init];

//put the info from stocks into our array
[BNRStock addObject:firstStock];
[BNRStock addObject:secondStock];
[BNRStock addObject:thirdStock];

//show the contents of the array
for (BNRStockHolding *d in BNRStock)
{
NSLog(@“PSP: %.2f, CSP: %.2f, #of Shares: %d, Current Value: %.2f Original Cost: %.2f \n”, [d purchaseSharePrice], [d currentSharePrice], [d numberOfShares], [d valueInDollars], [d costInDollars]);
}

}
return 0;

}