So I’m sure it’s something obvious that I’ve missed but for the foreign stock holding portfolio challenge I keep getting a strange result in my NSLog.
stockHolding.h
#import <Foundation/Foundation.h>
@interface StockHolding : NSObject
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numbOfShares
@end
stockHolding.m
#import "StockHolding.h"
@implementation StockHolding
- (float) costInDollars
{
return _purchaseSharePrice * _numberOfShares;
}
- (float) valueInDollars
{
return _currentSharePrice * _numberOfShares;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"Value: %f \nCost: %f \nNumber of shares: %d \nCurrent Share Price: %f \nOriginal Share Price: %f\n\n", [self valueInDollars], [self costInDollars], [self numberOfShares], [self currentSharePrice], [self purchaseSharePrice]];
}
foreignStockHolding.h
#import "StockHolding.h"
@interface ForeignStockHolding : StockHolding
@property (nonatomic) float conversionRate;
@end
foreignStockHolding.m
#import "ForeignStockHolding.h"
@class StockHolding;
@implementation ForeignStockHolding
- (float) costInDollars
{
if (self.conversionRate) {
return super.costInDollars * self.conversionRate;}
else {
return self.purchaseSharePrice * self.numberOfShares;}
}
- (float) valueInDollars
{
if (self.conversionRate) {
return super.valueInDollars * self.conversionRate;}
else {
return self.currentSharePrice * self.numberOfShares;
}
}
@end
main.m
#import <Foundation/Foundation.h>
#import "StockHolding.h"
#import "ForeignStockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
StockHolding *dowJones = [[StockHolding alloc]init];
dowJones.purchaseSharePrice = 5.4;
dowJones.currentSharePrice = 7.6;
dowJones.numberOfShares = 12;
StockHolding *easyJet = [[StockHolding alloc] init];
easyJet.purchaseSharePrice = 3.3;
easyJet.currentSharePrice = 4.9;
easyJet.NumberOfShares = 15;
StockHolding *btGroup = [[StockHolding alloc] init];
btGroup.PurchaseSharePrice = 6.4;
btGroup.CurrentSharePrice = 4.1;
btGroup.NumberOfShares = 13;
ForeignStockHolding *sony = [[ForeignStockHolding alloc]init];
sony.PurchaseSharePrice = 6.4;
sony.CurrentSharePrice = 4.1;
sony.NumberOfShares = 11;
sony.ConversionRate = 2.1;
ForeignStockHolding *seneca = [[ForeignStockHolding alloc]init];
sony.PurchaseSharePrice = 3.2;
sony.CurrentSharePrice = 6.3;
sony.NumberOfShares = 47;
sony.ConversionRate = 2.1;
NSArray *portfolio = @[dowJones, easyJet, btGroup, seneca, sony];
// ERROR - sony showing up all zero in in arrays and out of order
for (StockHolding *currentHolding in portfolio) {
NSLog(@"%@", currentHolding);
}
}
return 0;
}
The NSLog is as follows:
2014-07-11 16:48:28.367 StocksRedo[404:303] Value: 91.199997
Cost: 64.800003
Number of shares: 12
Current Share Price: 7.600000
Original Share Price: 5.400000
2014-07-11 16:48:28.370 StocksRedo[404:303] Value: 73.500000
Cost: 49.500000
Number of shares: 15
Current Share Price: 4.900000
Original Share Price: 3.300000
2014-07-11 16:48:28.370 StocksRedo[404:303] Value: 53.299999
Cost: 83.200005
Number of shares: 13
Current Share Price: 4.100000
Original Share Price: 6.400000
2014-07-11 16:48:28.371 StocksRedo[404:303] Value: 0.000000
Cost: 0.000000
Number of shares: 0
Current Share Price: 0.000000
Original Share Price: 0.000000
2014-07-11 16:48:28.371 StocksRedo[404:303] Value: 621.809998
Cost: 315.839996
Number of shares: 47
Current Share Price: 6.300000
Original Share Price: 3.200000
As you can see the sony holding is showing up out of order and with its values all at zero. I’ve tried rebuilding the whole thing a couple of times and keep hitting on the same error. I must be missing something fundament. If anyone can shine some light on this for me I’d be immensely grateful!
Thanks so much for your time.