Took a couple of tries but here's my Challenge


#1

[code]#import “Portfolio.h”

@implementation Portfolio

  • (float) currentValue
    {
    float sum = 0;
    for (StockHolding *a in assets)
    {
    sum += [a valueInDollars];
    }
    return sum;
    }

  • (void) addAsset: (StockHolding *) a

{
if (!assets) {

assets = [[NSMutableArray alloc] init];

}

[assets addObject: a];

}
@end
[/code][code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface Portfolio : NSObject
{
float currentValue;
NSMutableArray *assets;
}

  • (float) currentValue;
  • (void) addAsset: (StockHolding *) a;

@end[/code]

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

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

@autoreleasepool {
    
    // create an instane of Portfolio
    
    Portfolio *myPortfolio = [[Portfolio alloc] init];
    
    //create three instances of StockHolding
    StockHolding *stock1 = [[StockHolding alloc] init];
    StockHolding *stock2 = [[StockHolding alloc] init];
    StockHolding *stock3 = [[StockHolding alloc] init];
    
    
    //Set instance variables for each
    [stock1 setNumberOfShares:10];
    [stock1 setPurchaseSharePrice:100.00];
    [stock1 setCurrentSharePrice:101.00];
    [stock2 setNumberOfShares:20];
    [stock2 setPurchaseSharePrice:200.00];
    [stock2 setCurrentSharePrice:100.00];
    [stock3 setNumberOfShares:30];
    [stock3 setPurchaseSharePrice:300.00];
    [stock3 setCurrentSharePrice:1000.00];
    

    //Add each object to the array
    NSMutableArray *myStocks = [[NSMutableArray alloc] init];
    [myStocks addObject:stock1];
    [myStocks addObject:stock2];
    [myStocks addObject:stock3];
    
    //create three instances of subclass ForeignStoxkHolding
    ForeignStockHolding *fstock1 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *fstock2 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *fstock3 = [[ForeignStockHolding alloc] init];
    
    //Set instance variables for each
    [fstock1 setNumberOfShares:10];
    [fstock1 setPurchaseSharePrice:100.00];
    [fstock1 setCurrentSharePrice:101.00];
    [fstock1 setConversionRate:2.00];
    [fstock2 setNumberOfShares:20];
    [fstock2 setPurchaseSharePrice:200.00];
    [fstock2 setCurrentSharePrice:100.00];
    [fstock2 setConversionRate:.5];
    [fstock3 setNumberOfShares:30];
    [fstock3 setPurchaseSharePrice:300.00];
    [fstock3 setCurrentSharePrice:1000.00];
    [fstock3 setConversionRate:5.00];
    
    //Add each object to the array
    [myStocks addObject:fstock1];
    [myStocks addObject:fstock2];
    [myStocks addObject:fstock3];
    
    // add stocks to portfolio
    for (StockHolding *a in myStocks)
    { 
        [myPortfolio addAsset: a];
    }
    
    float totalValue = 0;
    //Iterate through the array and printout data
    for (StockHolding *thisStock in myStocks)
    {
        totalValue = (totalValue + [thisStock valueInDollars]);
       NSLog(@"This stock has %d shares purchased for $%.2f each at a cost of $%.2f", [thisStock numberOfShares], [thisStock purchaseSharePrice], [thisStock costInDollars]);
       NSLog(@"It's current share price is $%.2f and is currently worth $%.2f.", [thisStock currentSharePrice], [thisStock valueInDollars]);
    
    }
    
      NSLog(@"The current value of my holdings is %.2f.", totalValue);
    NSLog(@"The current value of my portfolio is %.2f.", [myPortfolio currentValue]);//should be equal to my holdings
    
    
    
}
return 0;

}
[/code]

with results:

22011-12-08 10:43:27.323 Stocks[2874:707] This stock has 10 shares purchased for $100.00 each at a cost of $1000.00
2011-12-08 10:43:27.325 Stocks[2874:707] It’s current share price is $101.00 and is currently worth $1010.00.
2011-12-08 10:43:27.325 Stocks[2874:707] This stock has 20 shares purchased for $200.00 each at a cost of $4000.00
2011-12-08 10:43:27.325 Stocks[2874:707] It’s current share price is $100.00 and is currently worth $2000.00.
2011-12-08 10:43:27.326 Stocks[2874:707] This stock has 30 shares purchased for $300.00 each at a cost of $9000.00
2011-12-08 10:43:27.326 Stocks[2874:707] It’s current share price is $1000.00 and is currently worth $30000.00.
2011-12-08 10:43:27.326 Stocks[2874:707] This stock has 10 shares purchased for $100.00 each at a cost of $2000.00
2011-12-08 10:43:27.327 Stocks[2874:707] It’s current share price is $101.00 and is currently worth $2020.00.
2011-12-08 10:43:27.327 Stocks[2874:707] This stock has 20 shares purchased for $200.00 each at a cost of $2000.00
2011-12-08 10:43:27.328 Stocks[2874:707] It’s current share price is $100.00 and is currently worth $1000.00.
2011-12-08 10:43:27.328 Stocks[2874:707] This stock has 30 shares purchased for $300.00 each at a cost of $45000.00
2011-12-08 10:43:27.328 Stocks[2874:707] It’s current share price is $1000.00 and is currently worth $150000.00.
2011-12-08 10:43:27.329 Stocks[2874:707] The current value of my holdings is $186030.00.
2011-12-08 10:43:27.329 Stocks[2874:707] The current value of my portfolio is $186030.00.
Program ended with exit code: 0


#2

Dude… this really helped me out. Thank you!