Hi all, first post here.
Been going through the chapters and loving it so far.
Found most of it pretty easy to migrate to, coming from a fair C background.
This chapter (21 in the second edition book) has definitely been the most challenging for me, so I thought I’d post up my solution to see if there are any areas for improvement.
The BNRStockHolding and BNRForeignStockHolding classes remain the same, except for the stock ticker property.
My BNRPortfolio header looks like:
#import <Foundation/Foundation.h>
@class BNRStockHolding;
@interface BNRPortfolio : NSObject
{
NSMutableArray *_stocks;
}
@property (nonatomic, copy) NSString *owner;
@property (nonatomic, copy) NSArray *stocks;
@property (nonatomic) float totalStockValue;
-(void)addStock:(BNRStockHolding *)s;
-(float)totalStockValue;
@end
And the implementation:
#import "BNRPortfolio.h"
#import "BNRStockHolding.h"
@implementation BNRPortfolio
// Accessors for stocks properties
-(void)setStocks:(NSArray *)s
{
_stocks = [s mutableCopy];
}
-(NSArray *)stocks
{
return [_stocks copy];
}
-(void)addStock:(BNRStockHolding *)s
{
// Check if I have no stocks first
if (!_stocks) {
// If I don't then create the array
_stocks = [[NSMutableArray alloc] init];
}
[_stocks addObject:s];
}
-(float)totalStockValue
{
float sum = 0.0;
for (BNRStockHolding *s in _stocks) {
sum += [s valueInDollars];
}
return sum;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"<Owner: %@, Value: %.2f>", self.owner, self.totalStockValue];
}
@end
And my main file is as below:
#import <Foundation/Foundation.h>
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
BNRPortfolio *portfolioUK = [[BNRPortfolio alloc] init];
BNRPortfolio *portfolioUS = [[BNRPortfolio alloc] init];
BNRForeignStockHolding *lloyds = [[BNRForeignStockHolding alloc] init];
[lloyds setNumberOfShares:40];
[lloyds setPurchaseSharePrice:2.30];
[lloyds setCurrentSharePrice:4.5];
[lloyds setConversionRate:0.65];
[lloyds setSymbol:@"LLYDS"];
[portfolioUK addStock:lloyds];
BNRForeignStockHolding *bt = [[BNRForeignStockHolding alloc] init];
[bt setNumberOfShares:10];
[bt setPurchaseSharePrice:20];
[bt setCurrentSharePrice:25];
[bt setConversionRate:0.65];
[bt setSymbol:@"BT"];
[portfolioUK addStock:bt];
BNRStockHolding *google = [[BNRStockHolding alloc] init];
[google setNumberOfShares:10];
[google setPurchaseSharePrice:400];
[google setCurrentSharePrice:412.2];
[google setSymbol:@"GOOG"];
[portfolioUS addStock:google];
BNRStockHolding *apple = [[BNRStockHolding alloc] init];
[apple setNumberOfShares:15];
[apple setPurchaseSharePrice:70];
[apple setCurrentSharePrice:450];
[apple setSymbol:@"AAPL"];
[portfolioUS addStock:apple];
BNRStockHolding *fbook = [[BNRStockHolding alloc] init];
[fbook setNumberOfShares:20];
[fbook setPurchaseSharePrice:35];
[fbook setCurrentSharePrice:45];
[fbook setSymbol:@"FBK"];
[portfolioUS addStock:fbook];
NSLog(@"Portfolio for UK has a value of £%.2f", [portfolioUK totalStockValue]);
NSLog(@"Portfolio for US has a value of $%.2f", [portfolioUS totalStockValue]);
}
return 0;
}
Then to the log I get:
Portfolio for UK has a value of £279.50
Portfolio for US has a value of $11772.00
Have I got it about right?
Thanks, Adam