Tough Challenge


#1

I think this challenge has been the toughest one for me - I tried to keep the code in the main as simple as possible, hope I covered the minimum requirement for this challenge

#import <Foundation/Foundation.h>
#import "StockHolding.h"
#import "Portfolio.h"

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

    @autoreleasepool {
        
        
        Portfolio *myPortfolio = [[Portfolio alloc]init];
        
        [myPortfolio setName:@"My Stocks"];
       
        
        StockHolding *a = [[StockHolding alloc] init];
        StockHolding *b = [[StockHolding alloc] init];
        StockHolding *c = [[StockHolding alloc] init];
        
        // regular stock holding
        
        [a setPurchaseSharePrice:2.30];
        [a setCurrentSharePrice:4.50];
        [a setNumberOfShares:40];
        [a costInDollars];
        [a valueInDollars];
        
        [b setPurchaseSharePrice:12.19];
        [b setCurrentSharePrice:10.56];
        [b setNumberOfShares:90];
        [b costInDollars];
        [b valueInDollars];
        
        [c setPurchaseSharePrice:45.10];
        [c setCurrentSharePrice:49.51];
        [c setNumberOfShares:110];
        [c costInDollars];
        [c valueInDollars];
      
        [myPortfolio addStockHoldingObject:a];
        [myPortfolio addStockHoldingObject:b];
        [myPortfolio addStockHoldingObject:c];
        NSLog(@"The value of portfolio %@ is %f", [myPortfolio name],  [myPortfolio portfolioValue]);
        
        
    }
    return 0;
}

This is my Portfolio Class

#import "Portfolio.h"
#import "StockHolding.h"


@implementation Portfolio
@synthesize name;

-(void) addStockHoldingObject:(StockHolding *)a
{
    //check if stockholdings are nil
    if(!StockHoldings)
    {
        //create the array
        StockHoldings = [[NSMutableArray alloc] init];
    }
    [StockHoldings addObject:a];
    
}

-(float) portfolioValue
{
    //sum up the value in dollars for all stockings in the array
    float sum = 0.0;
    for(StockHolding *a in StockHoldings)
    {
        sum += [a valueInDollars];
    }
    return sum;
    }
    
@end