Challenge - I really am stumped!


#1

Ive created the Portfolio Class, but I have no idea how I would implement it. For example how do I instantiate 3 portfolios in order to add a selecction of the stocks to the portfolios.

Have I created the Portfolio class correctly?

I think the next step is to instantiate 3 Portfolios and add pointers to the instances of the stockholding objects to them. But how?

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


@interface Portfolio
{
    int portfolioID;
    NSString *porfolioName;
    NSMutableArray *stockHoldings;
}

@property int portfolioID;
@property (strong) NSString *porfolioName;

- (void) addStockHoldingsObject:(StockHolding *)s;
- (float)valueOfStockHoldings;
@end[/code]

[code]#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio

@synthesize portfolioID = _portfolioID;
@synthesize porfolioName = _portfolioName;


- (void) addStockHoldingsObject:(StockHolding *)s
{
    // Is stockholdings nil?
    if (!stockHoldings) {
        
        // Create an the array
        stockHoldings = [[NSMutableArray alloc] init];
    }
    [stockHoldings addObject:s];
}

- (float)valueOfStockHoldings
{
    // sum up the value of all stockholdings in the portfolio
    float sum = 0;
    for (StockHolding *s in stockHoldings) {
        sum += [s valueInDollars];
    }
    return sum;
}
@end[/code]

[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    // It has two instance variables
    NSString *stockName;
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}
@property (strong) NSString *stockName;
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;



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

@end

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice = _purchaseSharePrice,
currentSharePrice = _currentSharePrice,
numberOfShares = _numberOfShares,
stockName = _stockName;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
    {
    return [self purchaseSharePrice] * [self numberOfShares];
    }

  • (float)valueInDollars; // currentSharePrice * numberOfSahres
    {
    return [self currentSharePrice] * [self numberOfShares];
    }

@end
[/code]

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

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}
@property float conversionRate;

@end[/code]

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
    {
    return (([self purchaseSharePrice] * [self conversionRate]) * [self numberOfShares]);
    }

  • (float)valueInDollars; // currentSharePrice * numberOfSahres
    {
    return (([self currentSharePrice] * [self conversionRate]) * [self numberOfShares]);
    }

@end[/code]

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

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

@autoreleasepool {
    
    // Create 2 Instances of Portfolio
   
    
    // Create 3 instances of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    ForeignStockHolding *stockholding4 =  [[ForeignStockHolding alloc] init];
    ForeignStockHolding *stockholding5 =  [[ForeignStockHolding alloc] init];
    
    // Create an empty array
    //NSMutableArray *stockList = [NSMutableArray array];
    
    // Give the instance variables interesting values
    [stockholding1 setStockName:@"BAML"];
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //[stockList addObject:stockholding1];
            
    // Give the instance variables interesting values
    [stockholding2 setStockName:@"BARCAP"];
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //[stockList addObject:stockholding2];
    
    // Give the instance variables interesting values
    [stockholding3 setStockName:@"UKCL"];
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    // Give the instance variables interesting values
    [stockholding4 setStockName:@"SPPW"];
    [stockholding4 setPurchaseSharePrice:10.50];
    [stockholding4 setCurrentSharePrice:150.00];
    [stockholding4 setNumberOfShares:100];
    [stockholding4 setConversionRate:1.00];
    
    // Give the instance variables interesting values
    [stockholding5 setStockName:@"BBC"];
    [stockholding5 setPurchaseSharePrice:20.00];
    [stockholding5 setCurrentSharePrice:10.00];
    [stockholding5 setNumberOfShares:100];
    [stockholding5 setConversionRate:2.00];
    
    //[stockList addObject:stockholding3];
    
    // Create an array with objects
    NSMutableArray *stockList = [NSMutableArray arrayWithObjects:stockholding1, stockholding2, stockholding3, stockholding4, stockholding5, nil];


    // Call the stockholding methods
    
    for(StockHolding *n in stockList)
        {
        //Call the methods 
        float cost = [n costInDollars];
        float value = [n valueInDollars];
        NSLog(@"Bought stock %@ for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
             [n stockName], [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
    }
}
return 0;

}[/code]


#2

I cracked it, could someone please check me and comment on my solution:

MAIN:

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

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

@autoreleasepool {
    
   
    // Create 3 instances of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    //Create 2 instances of ForeignStockHolding
    ForeignStockHolding *stockholding4 =  [[ForeignStockHolding alloc] init];
    ForeignStockHolding *stockholding5 =  [[ForeignStockHolding alloc] init];
    
    // Create 3 Instances of Portfolio
    Portfolio *portfolio1 = [[Portfolio alloc] init];
    Portfolio *portfolio2 = [[Portfolio alloc] init];
    Portfolio *portfolio3 = [[Portfolio alloc] init];
    
    // Give the Portfolio a name
    [portfolio1 setPorfolioName:@"Blue"];
    [portfolio2 setPorfolioName:@"Red"];
    [portfolio3 setPorfolioName:@"Green"];
    
    // Add the stock holdings
    [portfolio1 addStockHoldingsObject:stockholding1];
    [portfolio1 addStockHoldingsObject:stockholding2];
    
    [portfolio2 addStockHoldingsObject:stockholding3];
    [portfolio2 addStockHoldingsObject:stockholding4];
    
    [portfolio3 addStockHoldingsObject:stockholding5];
    
    
    // Create an empty array
    //NSMutableArray *stockList = [NSMutableArray array];
    
    // Give the instance variables interesting values
    [stockholding1 setStockName:@"BAML"];
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //[stockList addObject:stockholding1];
            
    // Give the instance variables interesting values
    [stockholding2 setStockName:@"BARCAP"];
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //[stockList addObject:stockholding2];
    
    // Give the instance variables interesting values
    [stockholding3 setStockName:@"UKCL"];
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    // Give the instance variables interesting values
    [stockholding4 setStockName:@"SPPW"];
    [stockholding4 setPurchaseSharePrice:10.50];
    [stockholding4 setCurrentSharePrice:150.00];
    [stockholding4 setNumberOfShares:100];
    [stockholding4 setConversionRate:1.00];
    
    // Give the instance variables interesting values
    [stockholding5 setStockName:@"BBC"];
    [stockholding5 setPurchaseSharePrice:20.00];
    [stockholding5 setCurrentSharePrice:10.00];
    [stockholding5 setNumberOfShares:100];
    [stockholding5 setConversionRate:2.00];
    
    //[stockList addObject:stockholding3];
    
    // Create an array with objects
    NSMutableArray *stockList = [NSMutableArray arrayWithObjects:stockholding1, stockholding2, stockholding3, stockholding4, stockholding5, nil];
    NSMutableArray *portfolioList= [NSMutableArray arrayWithObjects:portfolio1, portfolio2, portfolio3, nil];
    

    // Call the stockholding methods
    
    for(StockHolding *n in stockList)
        {
        //Call the methods 
        float cost = [n costInDollars];
        float value = [n valueInDollars];
        NSLog(@"Bought stock %@ for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
             [n stockName], [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
    }
    
    for(Portfolio *p in portfolioList)
    {
        //Call the methods 
    
        float portfolioValue = [p valueOfStockHoldings];
        NSLog(@"Value of portfolio %@ is $%.2f",
              [p porfolioName], portfolioValue);
    }
}
return 0;

}[/code]


#3

I went with something a little more simple.

Portfolio.h

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

@interface Portfolio : NSObject {
    NSMutableArray *stocks;
}

-(void) addStockObject:(StockHolding *)s;
-(float) valueOfStocks;

@end

Portfolio.m

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

@implementation Portfolio

-(void)addStockObject:(StockHolding *)s{
    if(!stocks){
        stocks = [[NSMutableArray alloc] init];
    }
    [stocks addObject:s];
}

-(float)valueOfStocks {
    float sum = 0;
    for(StockHolding *s in stocks){
        sum += [s valueInDollars];
    }
    return sum;
}

@end

In main.m

        Portfolio *portfolio = [[Portfolio alloc] init];
        [portfolio addStockObject:s1];
        [portfolio addStockObject:s2];
        [portfolio addStockObject:s3];
        NSLog(@"Portfolio value: %.2f", [portfolio valueOfStocks]);

There’s no one right answer though. :smiley: