My Challenge Solution


#1

Here it is :slight_smile:

Portfolio.h:

//  Portfolio.h
//  NerdCp19Challenge
//

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

@interface Portfolio : NSObject
{
    NSMutableArray *stockHoldings;
    unsigned int portfolioStockId;
}
@property (strong) NSMutableArray *stockHoldings;
@property unsigned int portfolioStockId;

-(void) addStockHoldingsObject:(StockHolding *)s;

-(unsigned int) currentValueOfPortfolio;
@end

Portfolio.m

//  Portfolio.m
//  NerdCp19Challenge
//

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

@implementation Portfolio

@synthesize stockHoldings;
@synthesize portfolioStockId;


-(void) addStockHoldingsObject:(StockHolding *)s
{

    if (!stockHoldings)
        stockHoldings = [NSMutableArray array];
    
    [stockHoldings addObject:s];
}

-(unsigned int) currentValueOfPortfolio
{
    unsigned int sum = 0; 
        for (StockHolding *stock in stockHoldings)
            sum += [stock valueInDollars];
       
    return sum;
}

-(NSString *) description
{
    return [NSString stringWithFormat:@"<Portfolio %i %@:>", [self portfolioStockId],[self stockHoldings]];
}

-(void) dealloc
{
    NSLog(@"deallocating (Portfolio) %@", self);
}
@end

StockHolding.h:

//  StockHolding.h
//  NerdCp17Challenge
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

@property float purchaseSharePrice,currentSharePrice;
@property int numberOfShares;


-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n;

-(float) costInDollars;
-(float) valueInDollars;
@end

StockHolding.m:

//  StockHolding.m
//  NerdCp17Challenge
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n
{
    purchaseSharePrice = p;
    currentSharePrice = c;
    numberOfShares = n;
}

-(float) costInDollars
{
    return purchaseSharePrice * numberOfShares;
}
-(float) valueInDollars
{
    return currentSharePrice * numberOfShares;
}

-(NSString *) description
{
    return [NSString stringWithFormat:@"<Stock: Purchase of Price: %.2f Current Share Price: %.2f   Number of Shares: %i>", 
            [self purchaseSharePrice], [self currentSharePrice], [self numberOfShares]];
}

-(void) dealloc
{
    NSLog(@"deallocating (StockHolding) %@", self);
}
@end

main.m

//
//  main.m
//  NerdCp19Challenge
//

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

#define STOCK_HOLDING_SIZE 10

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

    @autoreleasepool {
        
        Portfolio * portfolio = [[Portfolio alloc] init];
        
        for (int i = 0; i < STOCK_HOLDING_SIZE; ++i) {
            StockHolding *stock = [[StockHolding alloc] init];
            
            [stock setPurchaseSharePrice:random() % 100 andCurrentSharePrice:random() % 120 andNumberOfShares:(int)(random() % 200)];
            
            [portfolio addStockHoldingsObject:stock];
        }
        
        unsigned int i = 0;
        for (StockHolding *st in [portfolio stockHoldings] ) {
            NSLog(@"Stock %i: \nPurchase of Price: %.2f \nCurrent Share Price: %.2f \nNumber of Shares: %i\n\n\n", 
                  i++, [st purchaseSharePrice], [st currentSharePrice], [st numberOfShares]);
        }
        
        NSLog(@"The Current Value of Portfolio is $%i", [portfolio currentValueOfPortfolio]);
        
        NSLog(@"Portfolio: %@", portfolio);
        
        NSLog(@"Giving up ownership of one stock from portfolio");
        
        [[portfolio stockHoldings] removeObjectAtIndex:9];
        
        NSLog(@"Giving up ownership of array");
        
        portfolio = nil;
        
        
    }
    return 0;
}

Output:

2012-08-14 03:06:27.830 NerdCp19Challenge[8021:4503] Stock 0: 
Purchase of Price: 83.00 
Current Share Price: 46.00 
Number of Shares: 177


2012-08-14 03:06:27.833 NerdCp19Challenge[8021:4503] Stock 1: 
Purchase of Price: 15.00 
Current Share Price: 113.00 
Number of Shares: 135


2012-08-14 03:06:27.833 NerdCp19Challenge[8021:4503] Stock 2: 
Purchase of Price: 86.00 
Current Share Price: 12.00 
Number of Shares: 49


2012-08-14 03:06:27.834 NerdCp19Challenge[8021:4503] Stock 3: 
Purchase of Price: 21.00 
Current Share Price: 2.00 
Number of Shares: 27


2012-08-14 03:06:27.834 NerdCp19Challenge[8021:4503] Stock 4: 
Purchase of Price: 90.00 
Current Share Price: 19.00 
Number of Shares: 163


2012-08-14 03:06:27.835 NerdCp19Challenge[8021:4503] Stock 5: 
Purchase of Price: 26.00 
Current Share Price: 60.00 
Number of Shares: 26


2012-08-14 03:06:27.836 NerdCp19Challenge[8021:4503] Stock 6: 
Purchase of Price: 72.00 
Current Share Price: 16.00 
Number of Shares: 11


2012-08-14 03:06:27.836 NerdCp19Challenge[8021:4503] Stock 7: 
Purchase of Price: 68.00 
Current Share Price: 87.00 
Number of Shares: 29


2012-08-14 03:06:27.837 NerdCp19Challenge[8021:4503] Stock 8: 
Purchase of Price: 82.00 
Current Share Price: 50.00 
Number of Shares: 62


2012-08-14 03:06:27.837 NerdCp19Challenge[8021:4503] Stock 9: 
Purchase of Price: 23.00 
Current Share Price: 67.00 
Number of Shares: 135


2012-08-14 03:06:27.838 NerdCp19Challenge[8021:4503] The Current Value of Portfolio is $43540
2012-08-14 03:06:27.839 NerdCp19Challenge[8021:4503] Portfolio: <Portfolio 0 (
    "<Stock: Purchase of Price: 83.00 Current Share Price: 46.00   Number of Shares: 177>",
    "<Stock: Purchase of Price: 15.00 Current Share Price: 113.00   Number of Shares: 135>",
    "<Stock: Purchase of Price: 86.00 Current Share Price: 12.00   Number of Shares: 49>",
    "<Stock: Purchase of Price: 21.00 Current Share Price: 2.00   Number of Shares: 27>",
    "<Stock: Purchase of Price: 90.00 Current Share Price: 19.00   Number of Shares: 163>",
    "<Stock: Purchase of Price: 26.00 Current Share Price: 60.00   Number of Shares: 26>",
    "<Stock: Purchase of Price: 72.00 Current Share Price: 16.00   Number of Shares: 11>",
    "<Stock: Purchase of Price: 68.00 Current Share Price: 87.00   Number of Shares: 29>",
    "<Stock: Purchase of Price: 82.00 Current Share Price: 50.00   Number of Shares: 62>",
    "<Stock: Purchase of Price: 23.00 Current Share Price: 67.00   Number of Shares: 135>"
):>
2012-08-14 03:06:27.839 NerdCp19Challenge[8021:4503] Giving up ownership of one stock from portfolio
2012-08-14 03:06:27.840 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 23.00 Current Share Price: 67.00   Number of Shares: 135>
2012-08-14 03:06:27.841 NerdCp19Challenge[8021:4503] Giving up ownership of array
2012-08-14 03:06:27.841 NerdCp19Challenge[8021:4503] deallocating (Portfolio) <Portfolio 0 (
    "<Stock: Purchase of Price: 83.00 Current Share Price: 46.00   Number of Shares: 177>",
    "<Stock: Purchase of Price: 15.00 Current Share Price: 113.00   Number of Shares: 135>",
    "<Stock: Purchase of Price: 86.00 Current Share Price: 12.00   Number of Shares: 49>",
    "<Stock: Purchase of Price: 21.00 Current Share Price: 2.00   Number of Shares: 27>",
    "<Stock: Purchase of Price: 90.00 Current Share Price: 19.00   Number of Shares: 163>",
    "<Stock: Purchase of Price: 26.00 Current Share Price: 60.00   Number of Shares: 26>",
    "<Stock: Purchase of Price: 72.00 Current Share Price: 16.00   Number of Shares: 11>",
    "<Stock: Purchase of Price: 68.00 Current Share Price: 87.00   Number of Shares: 29>",
    "<Stock: Purchase of Price: 82.00 Current Share Price: 50.00   Number of Shares: 62>"
):>
2012-08-14 03:06:27.842 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 83.00 Current Share Price: 46.00   Number of Shares: 177>
2012-08-14 03:06:27.842 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 15.00 Current Share Price: 113.00   Number of Shares: 135>
2012-08-14 03:06:27.843 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 86.00 Current Share Price: 12.00   Number of Shares: 49>
2012-08-14 03:06:27.844 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 21.00 Current Share Price: 2.00   Number of Shares: 27>
2012-08-14 03:06:27.844 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 90.00 Current Share Price: 19.00   Number of Shares: 163>
2012-08-14 03:06:27.845 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 26.00 Current Share Price: 60.00   Number of Shares: 26>
2012-08-14 03:06:27.846 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 72.00 Current Share Price: 16.00   Number of Shares: 11>
2012-08-14 03:06:27.846 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 68.00 Current Share Price: 87.00   Number of Shares: 29>
2012-08-14 03:06:27.847 NerdCp19Challenge[8021:4503] deallocating (StockHolding) <Stock: Purchase of Price: 82.00 Current Share Price: 50.00   Number of Shares: 62>
Program ended with exit code: 0

#2

Thanks for posting this code. I was banging my head trying to figure out how to access the array in the Portfolio class. I discovered I forgot to create a property and synthesize it!