"Shares" Challenge chapter 18


#1

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!!


#2

Would really appreciate some thoughts “BigNerdRanch”…?


#3

When I run your code using my class files I get the following results…

2014-08-06 22:01:48.219 Stocks[3163:303] 1.50
2014-08-06 22:01:48.221 Stocks[3163:303] 15.00
2014-08-06 22:01:48.221 Stocks[3163:303] Value 16.00

2014-08-06 22:01:48.222 Stocks[3163:303] 1.60
2014-08-06 22:01:48.222 Stocks[3163:303] 32.00
2014-08-06 22:01:48.223 Stocks[3163:303] Value 30.00

2014-08-06 22:01:48.223 Stocks[3163:303] 0.32
2014-08-06 22:01:48.223 Stocks[3163:303] 0.32
2014-08-06 22:01:48.224 Stocks[3163:303] Value 0.43

Program ended with exit code: 0