Close to Challenge


#1

I think my problem is in main.m. Any Suggestions? Maybe all my code is wrong i’m just not for sure.

Portfolio.h

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

@interface Portfolio : StockHolding
{
NSMutableArray *myPortfolio;
}

@property (strong) NSMutableArray *myPortfolio;

  • (void)addStockToPortfolio:(StockHolding *)a;
  • (float)PortfolioValue;

@end[/code]

Portfolio.m

[code]#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

@synthesize myPortfolio;

  • (void)addStockToPortfolio:(StockHolding *)a
    {
    if (!myPortfolio) {
    myPortfolio = [[NSMutableArray alloc]init];
    }
    [myPortfolio addObject:a];
    }

  • (float)PortfolioValue
    {
    float sum=0.0;
    for (StockHolding *a in myPortfolio)
    {
    sum += [a valueInDollars];
    }
    return sum;
    }

@end[/code]

main.m

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

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

@autoreleasepool {
    
    //Create an array for the portfolio
    NSMutableArray *portfolios = [NSMutableArray array];
    
    
    //Adding the 3 stocks to the portfolio
    [portfolios addStockToPortfolio:     ];//I do not know what to put in
    [portfolios addStockToPortfolio:     ];//in these three
    [portfolios addStockToPortfolio:     ];//blanks.

    
    //Create 3 Instance of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    ForeignStockHolding *stockholding4 = [[ForeignStockHolding alloc] init];
    
    //Give the instance variables values for 1
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //Give the instance variables values for 2
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //Give the instance variables values for 3
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    //Give the instance variables for foreign
    [stockholding4 setPurchaseSharePrice:45.10];
    [stockholding4 setCurrentSharePrice:49.51];
    [stockholding4 setNumberOfShares:210];
    [stockholding4 setConversionRate:0.94];
    
    
    //Declare the NSMutableArray for the three sets of stocks
    NSMutableArray * stocks = [[NSMutableArray alloc] init];
    
    [stocks addObject:stockholding1];
    [stocks addObject:stockholding2];
    [stocks addObject:stockholding3];
    [stocks addObject:stockholding4];
    
    for(StockHolding *n in stocks)
        
    {
    
    
    //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 purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
        
        
    }
    float finalprice = [portfolios PortfolioValue];
    NSLog(@"Current value of all stocks in portfolio: $%.2f", finalprice);
    
    
}
return 0;

}[/code]


#2

You need to put the stocks into the portfolio after they have been created:

[code] //Create an array for the portfolio
NSMutableArray *portfolios = [NSMutableArray array];

    //Create 3 Instance of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    ForeignStockHolding *stockholding4 = [[ForeignStockHolding alloc] init];
    
    //Give the instance variables values for 1
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //Give the instance variables values for 2
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //Give the instance variables values for 3
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    //Give the instance variables for foreign
    [stockholding4 setPurchaseSharePrice:45.10];
    [stockholding4 setCurrentSharePrice:49.51];
    [stockholding4 setNumberOfShares:210];
    [stockholding4 setConversionRate:0.94];


    //Adding the 3 stocks to the portfolio
    [portfolios addStockToPortfolio: stockholding1];
    [portfolios addStockToPortfolio: stockholding2];
    [portfolios addStockToPortfolio: stockholding3];
    [portfolios addStockToPortfolio:stockholding4];

[/code]


#3

I tried this, but it is throwing me 5 errors. 4 of them are for //Adding the 3 stocks to the portfolio [portfolios addStockToPortfolio: stockholding1];//I do not know what to put in [portfolios addStockToPortfolio: stockholding2];//in these four [portfolios addStockToPortfolio: stockholding3];//blanks. [portfolios addStockToPortfolio: stockholding4];

The fifth one is from the float finalprice…float finalprice = [portfolios PortfolioValue]; NSLog(@"Current value of all stocks in portfolio: $%.2f", finalprice);

The error reads "Reciever type “NSMutableArray” for instance message does not declare a method with selector “addStockToPortfolio”

In the error bar on the left every error has the heading “Automatic Reference Counting”

I have the ARC turned on? Should I turn it off?


#4

I’m sorry, I had one line wrong:

should have been


#5

Ah hah, thanks I guess I was just thinking to hard.