Here is my code, which to my mind is correct - but clearly isn’t. At the bottom you can see the results…
#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
int main(int argc, const char * argv[])
{
@autoreleasepool {
BNRStockHolding *first = [[BNRStockHolding alloc] init];
[first setNumberOfShares:10];
[first setPurchaseSharePrice:1.50];
[first setCurrentSharePrice:1.60];
BNRStockHolding *second = [[BNRStockHolding alloc] init];
[second setNumberOfShares:20];
[second setPurchaseSharePrice:1.60];
[second setCurrentSharePrice:1.50];
BNRStockHolding *third = [[BNRStockHolding alloc] init];
[third setNumberOfShares:1];
[third setPurchaseSharePrice:0.32];
[third setCurrentSharePrice:0.43];
NSMutableArray *stocks = [[NSMutableArray alloc]init];
[stocks addObject:first];
[stocks addObject:second];
[stocks addObject:third];
for (BNRStockHolding *holdings in stocks)
{
float p = [holdings purchaseSharePrice];
float c = [holdings costInDollars];
NSLog (@"%0.2f",p);
NSLog (@"%0.2f",c);
NSLog (@“Value %0.2f\n\n”, [holdings valueInDollars]);
}
}
return 0;
}
Produces these results, which are clearly not what are expected !
2014-06-29 22:03:35.519 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:35.521 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:39.283 Ch 18: Challenge 1[11622:303] Value 0.43
2014-06-29 22:03:39.284 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:39.285 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:41.467 Ch 18: Challenge 1[11622:303] Value 0.43
2014-06-29 22:03:41.467 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:41.468 Ch 18: Challenge 1[11622:303] 0.32
2014-06-29 22:03:42.081 Ch 18: Challenge 1[11622:303] Value 0.43
Please put me out of my misery! Been looking at the code too long now!!