Portfolio Value Returns 0


#1

I have been working on this for days, including going through the forum and don’t understand why my portfolio value is 0.

My portfolio.m file has several warnings including “unused variable myStocks” though it seems to be used to me.

It seems that the portfolio class would be able to sum up the values of the stocks but apparently not.

[code]// Portfolio.h
// Challenge 17 Stocks
//
// Created by Dorman John on 8/13/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface Portfolio : NSObject

{
NSMutableArray *myStocks;
float valueofAll;
}

@property NSMutableArray *myStocks;
@property float valueofAll;

  • (void) addStocktoPortfolio:(StockHolding *)stocks;

@end[/code]

[code]// Portfolio.m
// Challenge 17 Stocks
//
// Created by Dorman John on 8/13/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

@synthesize valueofAll;
@synthesize myStocks;

-(void)addStocktoPortfolio:(StockHolding *)stock;

{if (!myStocks){
NSMutableArray *myStocks = [[NSMutableArray alloc] init];
}
[myStocks addObject:stock];

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

}
@end[/code]

[code]// main.m
// Challenge 17 Stocks
//
// Created by Dorman John on 7/27/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

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

@autoreleasepool {
    
    StockHolding * stock1 = [[StockHolding alloc] init];
    StockHolding * stock2 = [[StockHolding alloc] init];
    StockHolding * stock3 = [[StockHolding alloc] init];
    
    ForeignStockHolding * stock4 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding * stock5 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding * stock6 = [[ForeignStockHolding alloc] init];
    
    Portfolio *portfolio = [[Portfolio alloc] init];
    
    [stock1 setPurchaseSharePrice:2.30];
    [stock1 setCurrentSharePrice:4.50];
    [stock1 setNumberofShares:40];
    
    [stock2 setPurchaseSharePrice:12.19];
    [stock2 setCurrentSharePrice:10.56];
    [stock2 setNumberofShares:90];
    
    [stock3 setPurchaseSharePrice:45.10];
    [stock3 setCurrentSharePrice:49.51];
    [stock3 setNumberofShares:210];
    
    [stock4 setPurchaseSharePrice:2.30];
    [stock4 setCurrentSharePrice:4.50];
    [stock4 setNumberofShares:40];
    [stock4 setConversionrate:.33];
    
    [stock5 setPurchaseSharePrice:12.19];
    [stock5 setCurrentSharePrice:10.56];
    [stock5 setNumberofShares:90];
    [stock5 setConversionrate:.33];
    
    [stock6 setPurchaseSharePrice:45.10];
    [stock6 setCurrentSharePrice:49.51];
    [stock6 setNumberofShares:210];
    [stock6 setConversionrate:.33];
    
     
    

    NSArray * stocklist = [NSArray arrayWithObjects: stock1, stock2, stock3, stock4, stock5, stock6, nil];
    
    for (ForeignStockHolding *n  in stocklist)  {
        NSLog(@"Stock cost: %.2f   Current value: %.2f", [n costInDollars], [n valueInDollars]);
    
    }
    NSLog(@"Portfolio value is: %f", [portfolio valueofAll]);
    
}
return 0;

}[/code]


#2

For better communication, please post your files without copy and paste errors; post also the StockHolding files.

There is an error in the Porfolio.m. You are declaring a local variable with the same name as a global one (myStocks), so the statement [myStocks addObject:stock] has no effect because myStocks will always be nil.

Code with error:

-(void)addStocktoPortfolio:(StockHolding *)stock
{
if (!myStocks){
   NSMutableArray *myStocks = [[NSMutableArray alloc] init];  // Error: local variable declaration overrides the global one
}
[myStocks addObject:stock];
}

Revised version:

-(void)addStocktoPortfolio:(StockHolding *)stock
{
if (!myStocks){
   myStocks = [[NSMutableArray alloc] init];
}
[myStocks addObject:stock];
}

#3

I made the changes you suggested, however the portfolio value still returns a 0.

Here are my Stockholding files, hopefully without cut and paste errors, yesterday was my first time to post.

[code]// StockHolding.h
// Challenge 17 Stocks
//
// Created by Dorman John on 7/27/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

{
float purchaseSharePrice;
float currentSharePrice;
int numberofShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberofShares;

  • (float)costInDollars;
  • (float)valueInDollars;

@end[/code]

[code]// StockHolding.m
// Challenge 17 Stocks
//
// Created by Dorman John on 7/27/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberofShares;

  • (float) costInDollars
    {
    return purchaseSharePrice * numberofShares;

}

  • (float) valueInDollars

{
return currentSharePrice * numberofShares;

}

@end[/code]


#4

Let’s wipe the slate clean; please post all you files.


#5

Here are the 2 remaining files, ForeignStockholding.h and ForeignStockholding.m. The other five have been posted .

Thank you,
John

[quote]// ForeignStockHolding.h
// Challenge 17 Stocks
//
// Created by Dorman John on 7/30/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

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

@interface ForeignStockHolding : StockHolding

@property float conversionrate;

@end
[/quote]

[quote]// ForeignStockHolding.m
// Challenge 17 Stocks
//
// Created by Dorman John on 7/30/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionrate;

  • (float)costInDollars

{
float FDinD = [super costInDollars];
return FDinD * conversionrate;
}

  • (float)valueInDollars

{
float FVinD = [super valueInDollars];
return FVinD * conversionrate;
}

@end
[/quote]


#6

Oops!

I am sorry that I missed this in your first post.

The reason you are getting zero is that you forgot to add the stocks to the portfolio object.

Here is the revised code (look inside the for loop):

//  main.m
//  Challenge 17 Stocks
//
//  Created by Dorman John on 7/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

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


int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
        StockHolding * stock1 = [[StockHolding alloc] init];
        StockHolding * stock2 = [[StockHolding alloc] init];
        StockHolding * stock3 = [[StockHolding alloc] init];
        
        ForeignStockHolding * stock4 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding * stock5 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding * stock6 = [[ForeignStockHolding alloc] init];
        
        Portfolio *portfolio = [[Portfolio alloc] init];
        
        [stock1 setPurchaseSharePrice:2.30];
        [stock1 setCurrentSharePrice:4.50];
        [stock1 setNumberofShares:40];
        
        [stock2 setPurchaseSharePrice:12.19];
        [stock2 setCurrentSharePrice:10.56];
        [stock2 setNumberofShares:90];
        
        [stock3 setPurchaseSharePrice:45.10];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberofShares:210];
        
        [stock4 setPurchaseSharePrice:2.30];
        [stock4 setCurrentSharePrice:4.50];
        [stock4 setNumberofShares:40];
        [stock4 setConversionrate:.33];
        
        [stock5 setPurchaseSharePrice:12.19];
        [stock5 setCurrentSharePrice:10.56];
        [stock5 setNumberofShares:90];
        [stock5 setConversionrate:.33];
        
        [stock6 setPurchaseSharePrice:45.10];
        [stock6 setCurrentSharePrice:49.51];
        [stock6 setNumberofShares:210];
        [stock6 setConversionrate:.33];
        
        NSArray * stocklist = [NSArray arrayWithObjects: stock1, stock2, stock3, stock4, stock5, stock6, nil];
        
        for (ForeignStockHolding *n  in stocklist)  {
            NSLog(@"Stock cost: %.2f   Current value: %.2f", [n costInDollars], [n valueInDollars]);
            
            [portfolio addStocktoPortfolio:n];   // <--- You forgot to add this line of code!
            
        }
        NSLog (@"Portfolio value is: %f", [portfolio valueofAll]);
        
    }
    return 0;
}

The output:

2012-08-21 13:05:44.389 jkdorman[22860:403] Stock cost: 92.00   Current value: 180.00
2012-08-21 13:05:44.391 jkdorman[22860:403] Stock cost: 1097.10   Current value: 950.40
2012-08-21 13:05:44.391 jkdorman[22860:403] Stock cost: 9471.00   Current value: 10397.10
2012-08-21 13:05:44.392 jkdorman[22860:403] Stock cost: 30.36   Current value: 59.40
2012-08-21 13:05:44.392 jkdorman[22860:403] Stock cost: 362.04   Current value: 313.63
2012-08-21 13:05:44.392 jkdorman[22860:403] Stock cost: 3125.43   Current value: 3431.04
2012-08-21 13:05:44.393 jkdorman[22860:403] Portfolio value is: 15331.575195

#7

Many thanks, the program finally runs, one small question, in my portfolio.m file there is a warning which appears at the line

  • (float) valueofAll;

It states:
"Writable atomic property ‘ValueofAll’ cannot pair a synthesized setter with a user defined getter.

What am I doing wrong?