Hey “banjochris” if you are still around. This solution is the way I would go. But I am confused. The book says “move the property declaration for the array into a class extension”. You moved the instance variable, as I was about to do until I re-read the challenge. Do you think the book stated the challenge incorrectly? Where you able to continue through the rest of the book, or should I quit now and start learning SWIFT?
Anyway, any help you can provide, assuming you are still around, would be helpful.
[quote=“BanjoChris”]Here is my solution:
BNFPortfolio.h
[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”
@interface BNRPortfolio : NSObject
@property (nonatomic) float portfolioValue;
@property (nonatomic) NSArray *portfolio;
- (void)addStock:(BNRStockHolding *)s;
- (void)removeStock:(unsigned int)i;
- (float)valueOfPortfolio;
- (unsigned long)getCount;
- (void) portfolioInfo;
@end[/code]
BNRPortfolio.m
[code]#import “BNRPortfolio.h”
@interface BNRPortfolio ()
{
NSMutableArray *_portfolio;
}
@end
@implementation BNRPortfolio
- (void)setPortfolio:(NSArray *)p;
{
_portfolio = [p mutableCopy];
}
-(NSArray *)portfolio
{
return [_portfolio copy];
}
-
(float) valueOfPortfolio
{
// sum up the value of the stocks in the portfolio
float sum = 0.0;
for (BNRStockHolding *s in _portfolio)
{
sum += [s valueInDollars];
}
return sum;
}
-
(void)addStock:(BNRStockHolding *)s
{
// Is portfolio nil?
if(!_portfolio)
{
// Create the array
_portfolio = [[NSMutableArray alloc] init];
}
[_portfolio addObject:s];
}
-
(void)removeStock:(unsigned int)i
{
[_portfolio removeObjectAtIndex:i];
}
-
(unsigned long)getCount
{
unsigned long l = [_portfolio count];
return l;
}
-
(void)portfolioInfo
{
for (BNRStockHolding *s in _portfolio)
{
NSLog (@"%@ - Purchase Price = %.2f and Current Value = %.2f\n", [s symbol], [s costInDollars], [s valueInDollars]);
}
}
@end
[/code]
BNRForeignStockHolding.h
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
@interface BNRForeignStockHolding : BNRStockHolding
@property (nonatomic) float conversionRate;
@end
BNRForeignStockHolding.m
#import "BNRForeignStockHolding.h"
@implementation BNRForeignStockHolding
- (float)costInDollars
{
float foreignCostInDollars = [super costInDollars];
return foreignCostInDollars * [self conversionRate]; // _conversionRate;
}
- (float) valueInDollars
{
float foreignValueInDollars = [super valueInDollars];
return foreignValueInDollars * [self conversionRate]; // _conversionRate;
}
@end
BNRStockHolding.h
[code]#import <Foundation/Foundation.h>
@class BNRPortfolio;
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}
- (float)purchaseSharePrice;
- (void)setPurchaseSharePrice:(float)psp;
- (float)currentSharePrice;
- (void)setCurrentSharePrice:(float)csp;
- (int)numberOfShares;
- (void)setNumberOfShares:(int)nos;
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
@property (nonatomic, copy) NSString *symbol;
@end
[/code]
BNRStockHolding.m
[code]#import “BNRStockHolding.h”
#import “BNRPortfolio.h”
@implementation BNRStockHolding
-
(float)purchaseSharePrice
{
return _purchaseSharePrice;
}
-
(void)setPurchaseSharePrice:(float)psp
{
_purchaseSharePrice = psp;
}
-
(float)currentSharePrice
{
return _currentSharePrice;
}
-
(void)setCurrentSharePrice:(float)csp
{
_currentSharePrice = csp;
}
-
(int)numberOfShares
{
return _numberOfShares;
}
-
(void)setNumberOfShares:(int)nos
{
_numberOfShares = nos;
}
-
(float)costInDollars
{
// purchaseSharePrice * numberOfShares
return [self purchaseSharePrice] * [self numberOfShares];
}
-
(float)valueInDollars
{
// currentSharePrice * numberOfShares
return [self currentSharePrice] * [self numberOfShares];
}
@end
[/code]
main.m
[code]#import <Foundation/Foundation.h>
#import “BNRForeignStockHolding.h”
#import “BNRPortfolio.h”
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Creat an array of BNRStockHolding objects
//NSMutableArray *portfolio = [[NSMutableArray alloc] init];
BNRPortfolio *holdings = [[BNRPortfolio alloc] init];
BNRStockHolding *apple = [[BNRStockHolding alloc] init];
[apple setSymbol:@"APP"];
[apple setCurrentSharePrice:4.50];
[apple setPurchaseSharePrice:2.30];
[apple setNumberOfShares:40];
[holdings addStock:apple];
BNRStockHolding *microSoft = [[BNRStockHolding alloc] init];
[microSoft setSymbol:@"MS"];
[microSoft setCurrentSharePrice:10.56];
[microSoft setPurchaseSharePrice:12.19];
[microSoft setNumberOfShares:90];
[holdings addStock:microSoft];
BNRStockHolding *bestBuy = [[BNRStockHolding alloc] init];
[bestBuy setSymbol:@"BB"];
[bestBuy setCurrentSharePrice:49.51];
[bestBuy setPurchaseSharePrice:45.10];
[bestBuy setNumberOfShares:210];
[holdings addStock:bestBuy];
BNRForeignStockHolding *bestFrenchBuy = [[BNRForeignStockHolding alloc] init];
[bestFrenchBuy setSymbol:@"BFB"];
[bestFrenchBuy setCurrentSharePrice:49.51];
[bestFrenchBuy setPurchaseSharePrice:45.10];
[bestFrenchBuy setNumberOfShares:210];
[bestFrenchBuy setConversionRate:0.50];
[holdings addStock:bestFrenchBuy];
[holdings portfolioInfo];
NSLog (@"The total portfolio value is %.2f", holdings.valueOfPortfolio);
[holdings removeStock:2];
[holdings portfolioInfo];
NSLog (@"The total portfolio value is %.2f", holdings.valueOfPortfolio);
}
return 0;
}
[/code][/quote]