Challenge Solution: My version


#1

Portfolio.h

[code]#import <Foundation/Foundation.h>
#import “foreignStockHolding.h” // Subclass of StockHolding which already imports it’s parent class

@interface Portfolio : NSObject
{

NSMutableArray *portfolio;

}

@property float valueOfPortfolio;

-(void) addStockToPortfolio: (StockHolding *) holding;
-(void) displayPortfolio;
-(void) displayValueOfPortfolio;
-(void) displayNumberOfStocksInPortfolio;

@end
[/code]

Portfolio.m

[code]#import “Portfolio.h”

@implementation Portfolio

@synthesize valueOfPortfolio;

-(void) addStockToPortfolio:(StockHolding *) holding
{

if( !portfolio )        //  If it's a new portfolio, allocate and initialize it
{
    
    portfolio = [ [NSMutableArray alloc] init ];
            
}

[portfolio addObject: holding];


//  Keep a running value as each new stock is added

[self setValueOfPortfolio: ([self valueOfPortfolio] + [holding valueInDollars]) ];

}

-(void) displayPortfolio
{

//  Iterate through the array using fast enumeration, displaying the value of each stock as well as it's growth in $

for( StockHolding *stock in portfolio )
{
    
    NSLog(@"The current value is $%.2f, with an unrealized gain of $%.2f", [stock valueInDollars],
          [stock unrealizedGain] );
    
}

}

-(void) displayValueOfPortfolio
{

NSLog(@"The total value of the portfolio is:  $%.2f", [self valueOfPortfolio] );    

}

-(void) displayNumberOfStocksInPortfolio
{

NSLog(@"The portfolio contains %lu stocks.", [portfolio count] );

}

@end
[/code]

main.m

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

int main (int argc, const char * argv[])
{

@autoreleasepool {
    
    //  Prepare pre-defined stocks to simulate user input
    
    StockHolding        *stock1         = [ [StockHolding alloc] init];
    StockHolding        *stock2         = [ [StockHolding alloc] init];
    StockHolding        *stock3         = [ [StockHolding alloc] init];
    foreignStockHolding *foreignStock1  = [ [foreignStockHolding alloc] init];
    
    
    //  Create a portfolio to contain an array of stocks
    
    Portfolio *myPortfolio = [ [Portfolio alloc] init];
    
    
    //  Put data into the stocks
    
    [stock1 setPurchaseSharePrice: 2.30];
    [stock1 setCurrentSharePrice: 4.50];
    [stock1 setNumberOfShares: 40];
    
    [stock2 setPurchaseSharePrice: 12.19];
    [stock2 setCurrentSharePrice: 10.56];
    [stock2 setNumberOfShares: 90];
    
    [stock3 setPurchaseSharePrice: 45.10];
    [stock3 setCurrentSharePrice: 49.51];
    [stock3 setNumberOfShares: 210];
    
    [foreignStock1 setPurchaseSharePrice: 2.30];
    [foreignStock1 setCurrentSharePrice: 4.50];
    [foreignStock1 setNumberOfShares: 40];
    [foreignStock1 setConversionRate: 0.94];
    
    
    //  Add each stock into the myPortfolio array
    
    [myPortfolio addStockToPortfolio: stock1];
    [myPortfolio addStockToPortfolio: stock2];
    [myPortfolio addStockToPortfolio: stock3];
    [myPortfolio addStockToPortfolio: foreignStock1];
    
    
    //  Display the entire portfolio with details of each stock contained therein
    
    [myPortfolio displayPortfolio];
    
    
    //  Display the number of stocks contained in the portfolio as well as it's total value
    
    [myPortfolio displayNumberOfStocksInPortfolio];
    [myPortfolio displayValueOfPortfolio];
    
    
            
}
return 0;

}

[/code]

The challenges are getting more fun. Great work on the book guys!