Holding Portfolio Solution

Main.m

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        BNRPortfolio *myStockPortfolio = [[BNRPortfolio alloc]init];
        BNRStockHolding *stockTXRH = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockGSAT = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockAAPL = [[BNRStockHolding alloc]init];
        
         // Made this stock foreign, although not really foreign
        BNRForeignStockHolding *stockZNGA = [[BNRForeignStockHolding alloc]init];
        
         
 /*******************************************************************/
 
        stockTXRH.companyName = (@"Texas Roadhouse");
        stockTXRH.symbol = @"TXRH";
        stockTXRH.purchaseSharePrice = 26.62;
        stockTXRH.currentSharePrice = 28.31;
        stockTXRH.numberOfShares = 2556;
        [myStockPortfolio addStock:stockTXRH];
        
/*******************************************************************/
        
        stockGSAT.companyName = (@"GlobalStar");
        stockGSAT.symbol = @"GSAT";
        stockGSAT.purchaseSharePrice = 2.30;
        stockGSAT.currentSharePrice = 4.50;
        stockGSAT.numberOfShares = 40;
        [myStockPortfolio addStock:stockGSAT];
        
        
/*******************************************************************/
        
        stockAAPL.companyName = (@"Apple");
        stockAAPL.symbol = @"AAPL";
        stockAAPL.purchaseSharePrice = 10.50;
        stockAAPL.currentSharePrice = 99.62;
        stockAAPL.numberOfShares = 500;
        [myStockPortfolio addStock:stockAAPL];
        
/*******************************************************************/
        
        // Made this stock foreign, although not really foreign
        
        stockZNGA.companyName = (@"Zynga");
        stockZNGA.symbol = @"ZNGA";
        stockZNGA.purchaseSharePrice = 2.30;
        stockZNGA.currentSharePrice = 4.50;
        stockZNGA.numberOfShares = 40;
        stockZNGA.conversionRate = 0.94;
        [myStockPortfolio addStock:stockZNGA];
        
    for (BNRStockHolding *stock in myStockPortfolio.holdings)
    {
        // Looks at the overridden version of the description methods
        // Both BNRStockHolding and BNRForeignStockholding have their own description method
        NSLog(@"%@", stock);
        
        
    }
        
     NSLog(@"\n\nThe current value of your portfolio is: $%.2f", [myStockPortfolio valueOfPortfolio]);
        
       
    }
 return 0;
}

BNRPortfolio.h

#import <Foundation/Foundation.h>
@class BNRStockHolding;

@interface BNRPortfolio : NSObject

{
    NSMutableArray *_holdings;
}

@property (nonatomic, copy) NSArray *holdings;

- (float) valueOfPortfolio;
- (void) addStock:(BNRStockHolding *)stock;

@end

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"



@implementation BNRPortfolio

- (void) setHoldings:(NSArray *)h
{
    _holdings = [h mutableCopy];
}

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

- (void) addStock:(BNRStockHolding *)stock
{
    if (_holdings == nil)
    {
        _holdings = [[NSMutableArray alloc]init];
    }
    
    [_holdings addObject:stock];
    
}


- (float) valueOfPortfolio
{
    float sum = 0;
    
    for (BNRStockHolding *stock in self.holdings)
    {
        sum += [stock valueInDollars];
    }
    
    return sum;
}


@end

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *companyName;
@property (nonatomic) NSString *symbol;
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int   numberOfShares;


- (float) costInDollars; // purchaseSharePrice * numberOfShares
- (float) valueInDollars; // currentSharePrice * numberOfShares

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding

              
- (float) costInDollars
{
 
    return self.purchaseSharePrice * self.numberOfShares;

}
              
- (float) valueInDollars
{
    return self.currentSharePrice * self.numberOfShares;
}

// Description is overridden

- (NSString *)description
{
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %@\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n",
            self.companyName, self.symbol, self.numberOfShares,
            self.purchaseSharePrice, self.currentSharePrice,
            [self costInDollars], [self valueInDollars]];
    
}

@end

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
{
    return (self.purchaseSharePrice * self.conversionRate) * self.numberOfShares;
}

- (float) valueInDollars
{
    return (self.currentSharePrice * self.conversionRate) * self.numberOfShares;
}

// Description is overriden

- (NSString *)description
{
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %@\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n     Conversion Rate: %.2f\n",
          self.companyName, self.symbol, self.numberOfShares,
          (self.purchaseSharePrice * self.conversionRate) , (self.currentSharePrice * self.conversionRate),
          [self costInDollars], [self valueInDollars], self.conversionRate];
    
}

@end

Thank You Okay Great. This was very helpful

Hey thanks for the solution.

All makes sense to me, except for one line:

why myStockPortfolio.holdings and not just myStockPortfolio?

Cheers!

Thanks! Off to study your codeā€¦