Portfolio solution

This took me three days. I was pretty confused with this chapter, and then the way the portfolio was described, it seemed simpler to do it as a standard NSMutableArray, like the stockList is. In fact, I had already created a NSString for the stock name, so I didn’t bother with the ticker variable.

However, I stuck with it to try to do it similar to the Asset class in the chapter. I’m not sure if this is actually correct, I’m still a bit fuzzy on the allocation and deallocation of the array, and I had several errors that I eventually cleared, but I’m not sure now exactly how I did it. Yet, here is the code.

BNRPortfolio.h

[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockholding.h”

@interface BNRPortfolio : NSObject

{
NSMutableArray *_holdings;
}

@property (nonatomic, copy) NSArray *holdings;

-(void) addStock: (BNRStockHolding*)a;
//-(void) addForeignStock: (BNRForeignStockholding*)b;
-(float) valueOfPortfolio;

@end
[/code]

BNR Portfolio.m

[code]#import “BNRPortfolio.h”

@implementation BNRPortfolio

//Accessors for portfolio properties
-(void) setPortfolio:(NSArray *)p
{
_holdings = [p mutableCopy];
}

-(NSArray*) holdings
{
return [_holdings copy];
}

-(void)addStock:(BNRStockHolding *)a
{
if(!_holdings){
//create the portfolio array
_holdings = [[NSMutableArray alloc] init];
}
[_holdings addObject:a];
}

-(float)valueOfPortfolio
{
float sum=0;
for (BNRStockHolding *a in _holdings) {
sum += [a valueInDollars];
}
return sum;
}

@end
[/code]

the main.m

[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockholding.h”
#import “BNRPortfolio.h”

int main(int argc, const char * argv[]) {
@autoreleasepool {
//Create three instance of BNRStockholding
BNRStockHolding *stock1 = [[BNRStockHolding alloc] init];
BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
BNRStockHolding *stock3 = [[BNRStockHolding alloc] init];

    //populate the stocks with data
    //stock1
    stock1.stockName = @"Organge Computers";
    stock1.numberOfShares=40;
    stock1.purchaseSharePrice= 2.30;
    stock1.currentSharePrice=4.50;
    [stock1 costInDollars];
    [stock1 valueInDollars];
    
    //stock2
    stock2.stockName = @"MacroHard Computers";
    stock2.numberOfShares= 90;
    stock2.purchaseSharePrice= 12.19;
    stock2.currentSharePrice= 10.56;
    [stock2 costInDollars];
    [stock2 valueInDollars];
    
    //stock3
    stock3.stockName = @"AlephNull";
    stock3.numberOfShares= 210;
    stock3.purchaseSharePrice= 45.10;
    stock3.currentSharePrice=49.51;
    [stock3 costInDollars];
    [stock3 valueInDollars];
    
    // foreign stock
    BNRForeignStockholding *stock4 = [[BNRForeignStockholding alloc] init];
    BNRForeignStockholding *stock5 = [[BNRForeignStockholding alloc] init];
    
    stock4.stockName = @"Alibabylon";
    stock4.numberOfShares = 15;
    stock4.purchaseSharePrice= 92.0;
    stock4.conversionRate= 0.95;
    stock4.currentSharePrice= 100;
    [stock4 costInDollars];
    [stock4 valueInDollars];
    
    stock5.stockName = @"Electric Sheep";
    stock5.numberOfShares= 25;
    stock5.purchaseSharePrice= 120;
    stock5.conversionRate= .39;
    stock5.currentSharePrice= 131.0;
    [stock5 costInDollars];
    [stock5 valueInDollars];
    
    //Create the NSMutable array
    NSMutableArray *stockList = [NSMutableArray array];
    [stockList addObject:stock1];//add the stocks to the array
    [stockList addObject:stock2];
    [stockList addObject:stock3];
    [stockList addObject:stock4];//add the foreign stocks
    [stockList addObject:stock5];

    //Create the portfolio
    BNRPortfolio *holdings = [[BNRPortfolio alloc] init];
    for (BNRStockHolding *s in stockList) {
        //put the stocks in the portfolio
        [holdings addStock:s];
        NSLog(@"The stock %@ is in the portfolio.", s);
        NSLog(@"The value of %@ is %.2f", s, [s valueInDollars]);
        
        }
    //Display the total stock value of the portfolio
    float t = [holdings valueOfPortfolio];
    NSLog(@"The total stock value of the portfolio is: $%f", t);

    
        
        //NSLog(@"The cost of %@ is %.2f", s, [s costInDollars]);
        //NSLog(@"The value of %@ is %.2f", s, [s valueInDollars]);
    
            
}
return 0;

}
[/code]

And here is the output:

2015-04-25 15:55:39.624 Stocks[1505:91784] The stock Organge Computers is in the portfolio.
2015-04-25 15:55:39.627 Stocks[1505:91784] The value of Organge Computers is 180.00
2015-04-25 15:55:46.215 Stocks[1505:91784] The stock MacroHard Computers is in the portfolio.
2015-04-25 15:55:46.215 Stocks[1505:91784] The value of MacroHard Computers is 950.40
2015-04-25 15:55:50.839 Stocks[1505:91784] The stock AlephNull is in the portfolio.
2015-04-25 15:55:50.839 Stocks[1505:91784] The value of AlephNull is 10397.10
2015-04-25 15:55:52.422 Stocks[1505:91784] The stock Alibabylon is in the portfolio.
2015-04-25 15:55:52.423 Stocks[1505:91784] The value of Alibabylon is 1425.00
2015-04-25 15:55:53.425 Stocks[1505:91784] The stock Electric Sheep is in the portfolio.
2015-04-25 15:55:53.425 Stocks[1505:91784] The value of Electric Sheep is 1277.25
2015-04-25 15:55:54.910 Stocks[1505:91784] The total stock value of the portfolio is: $14229.750000

I didn’t put the accessors in the BNRStockHolding files as the Asset accessors and such were in the BNRPerson. It didn’t work for me, and I believe it is because the Assets end up in an array underneath (pointed at) from the BNRPerson, while the stocks are under (pointed at) from the portfolio. This is where I got confused, because I couldn’t see why, in both cases, it couldn’t have just been a standard NSMutableArray for both classes.