Need help with my code, please


#1

My code is below. It produces:
2012-11-29 18:20:54.105 StocksChallenge[4625:303] Value is $0.00

I’ve been beating my head on this for days. This is my fifth attempt. I’d appreciate any help anyone can be. Thanks much.
Ed

[code]//
// StockHolding.m

#import “StockHolding.h”

@implementation StockHolding

@synthesize purchasePrice, currentPrice, numberOfShares;

-(float)costInDollars
{
return (purchasePrice * numberOfShares);
}

-(float)valueInDollars
{
return (currentPrice * numberOfShares);
}

@end
[/code]

[code]//
// StockHolding.h
//

#import <Foundation/Foundation.h>

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

@property float purchasePrice;
@property float currentPrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end
[/code]

[code]//
// Portfolio.m
//

#import “Portfolio.h”

@implementation Portfolio
@synthesize stocksInPortfolio;

-(float)valueOfPortfolio
{
float sum = 0.0;
for (int i = 0; i < [stocksInPortfolio count]; i++) {
sum += [[stocksInPortfolio objectAtIndex:i] valueInDollars];
}
return sum;
}
@end
[/code]

[code]//
// Portfolio.h
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface Portfolio : StockHolding
{
NSMutableArray *stocksInPortfolio;
}

@property (strong) NSMutableArray *stocksInPortfolio;

-(float)valueOfPortfolio;

@end
[/code]

[code]//
// main.m
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “Portfolio.h”

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

@autoreleasepool {
    
    Portfolio *myPortfolio = [[Portfolio alloc] init];
    
    StockHolding *edStock = [[StockHolding alloc]init];
    StockHolding *ermaStock = [[StockHolding alloc]init];
    StockHolding *boscoeStock = [[StockHolding alloc]init];
    
    Portfolio *stocksInPortfolio = [[Portfolio alloc] init];
            
    [edStock setPurchasePrice:2.30];
    [edStock setCurrentPrice:4.50];
    [edStock setNumberOfShares:40];
    [myPortfolio.stocksInPortfolio addObject:edStock];
    
    [ermaStock setPurchasePrice:12.19];
    [ermaStock setCurrentPrice:10.56];
    [ermaStock setNumberOfShares:90];
    [myPortfolio.stocksInPortfolio addObject:ermaStock];
    
    [boscoeStock setPurchasePrice:45.10];
    [boscoeStock setCurrentPrice:49.51];
    [boscoeStock setNumberOfShares:210];
    [myPortfolio.stocksInPortfolio addObject:boscoeStock];
    
    NSLog(@"Value is $%.2f", [stocksInPortfolio valueOfPortfolio]);
    
     }

return 0;

}

[/code]


#2

It is empty, add some stocks to stocksInPortfolio first and then print the value of it. Currently, you are adding the stocks to and printing myPortfolio.stocksInPortfolio.


#3

Thanks very much. I have now tried to do that, but now I’m getting a build error that “no visible @interface for Portfolio declares the selector addObject”. I know I’m really missing something here.
Here’s what I did:
In Portfolio.h I added the declaration of the addObjects method:

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

@interface Portfolio : StockHolding
{
NSMutableArray *stocksInPortfolio;
}

@property (strong) NSMutableArray *stocksInPortfolio;

-(float)valueOfPortfolio;

-(void)addObjects:(Portfolio *)a;

@end
[/code]

In Portfolio.m I defined it:

[code]#import “Portfolio.h”

@implementation Portfolio

@synthesize stocksInPortfolio;

-(float)valueOfPortfolio
{
float sum = 0.0;
for (int i = 0; i < [stocksInPortfolio count]; i++) {
sum += [[stocksInPortfolio objectAtIndex:i] valueInDollars];
}
return sum;
}

-(void)addObjects:(StockHolding *)a
{
[stocksInPortfolio addObject:a];
}
@end
[/code]
I Main.m I changed the lines where I tried to add the stock:

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

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

@autoreleasepool {
    
    Portfolio *myPortfolio = [[Portfolio alloc] init];
    
    StockHolding *edStock = [[StockHolding alloc]init];
    StockHolding *ermaStock = [[StockHolding alloc]init];
    StockHolding *boscoeStock = [[StockHolding alloc]init];
    
    Portfolio *stocksInPortfolio = [[Portfolio alloc] init];
            
    [edStock setPurchasePrice:2.30];
    [edStock setCurrentPrice:4.50];
    [edStock setNumberOfShares:40];
    [stocksInPortfolio addObject:edStock];
    
    [ermaStock setPurchasePrice:12.19];
    [ermaStock setCurrentPrice:10.56];
    [ermaStock setNumberOfShares:90];
    [stocksInPortfolio addObject:ermaStock];
    
    [boscoeStock setPurchasePrice:45.10];
    [boscoeStock setCurrentPrice:49.51];
    [boscoeStock setNumberOfShares:210];
    [stocksInPortfolio addObject:boscoeStock];
    
    NSLog(@"Value is $%.2f", [stocksInPortfolio valueOfPortfolio]);
   
}

return 0;

[/code]


#4

On stocksInPortfolio you are invoking the method addObject:, which does not exist in Portfolio.
You should then declare and define that exact method in Portfolio.

Can you see what is wrong in the method declaration -(void)addObject[color=#FF0000]s[/color]:(Portfolio *)a in Portfolio?

Change -(void)addObject[color=#FF0000]s[/color]:(Portfolio *)a to -(void)addObject:(Portfolio *)a


#5

thank you so much. Ah, the details…
Ed