BNRPortfolio challenge bug

I’m not really sure what I’m doing wrong. I can build and run just fine but the issue is my array is empty and my valueOfPrice is 0. I’ve also excluded BNRForeignStock for time being. If anyone can look through my code and see where my pitfall is or just any advice I would really, really appreciate it. Thanks!

BNRPortfolio.h

[code]
#import <Foundation/Foundation.h>
@class BNRStockHolding;

@interface BNRPortfolio : NSObject
{
NSMutableArray *_holdings;
float _totalValue;
}

@property (nonatomic, copy) NSArray *_holdings;
@property (nonatomic) float totalValue;

  • (void)addHoldings:(BNRStockHolding *)holdings;
  • (float)valueOfPortfolio;

@end[/code]

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"

@implementation BNRPortfolio

- (float)totalValue
{
    return _totalValue;
}

- (void)setHoldings:(NSArray *)holdings
{
    _holdings = [holdings mutableCopy];
}

- (NSArray *)holdings
{
    return [_holdings copy];
}

- (void)addHoldings:(BNRStockHolding *)holdings
{
    if (!holdings) {
        _holdings = [[NSMutableArray alloc] init];
    }
    [_holdings addObject];
}

- (float)valueOfPortfolio
{
    float sum = 0;
    
    for (BNRStockHolding *h in self.holdings) {
        sum += [h valueInDollars];
    }
    
    return sum;
}

@end

BNRSockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject
{
    float _purchaseSharePrice;
    float _currentSharePrice;
    int _numberOfShares;
}

//getters and setters
- (float)purchaseSharePrice;
- (void)setPurchaseSharePrice:(float)sharePrice;
- (float)currentSharePrice;
- (void)setCurrentSharePrice:(float)currentPrice;
- (int)numberOfShares;
- (void)setNumberOfShares:(int)shares;

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

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding

- (float)purchaseSharePrice
{
    return _purchaseSharePrice;
}

- (void)setPurchaseSharePrice:(float)sharePrice
{
    _purchaseSharePrice = sharePrice;
}

- (float)currentSharePrice
{
    return _currentSharePrice;
}

- (void)setCurrentSharePrice:(float)currentPrice
{
    _currentSharePrice = currentPrice;
}

- (int)numberOfShares
{
    return _numberOfShares;
}

- (void)setNumberOfShares:(int)shares
{
    _numberOfShares = shares;
}

- (float)costInDollars
{
    return _purchaseSharePrice * _numberOfShares;
}

- (float)valueInDollars
{
    return _currentSharePrice * _numberOfShares;
}

@end

main.m

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //NSMutableArray *stocks = [NSMutableArray array];
        BNRPortfolio *portfolio = [[BNRPortfolio alloc] init];
        
        BNRStockHolding *one = [[BNRStockHolding alloc] init];
        [one setPurchaseSharePrice:2.30];
        [one setCurrentSharePrice:4.50];
        [one setNumberOfShares:40];
        //[one setConversionRate:0.94];
        //[stocks addObject:one];
        [portfolio addHoldings:one];

//        BNRStockHolding *two = [[BNRStockHolding alloc] init];
//        [two setPurchaseSharePrice:12.19];
//        [two setCurrentSharePrice:10.56];
//        [two setNumberOfShares:90];
//        [stocks addObject:two];
//
//        BNRStockHolding *three = [[BNRStockHolding alloc] init];
//        [three setPurchaseSharePrice:45.10];
//        [three setCurrentSharePrice:49.51];
//        [three setNumberOfShares:210];
//        [stocks addObject:three];
        
        BNRStockHolding *two = [[BNRStockHolding alloc] init];
        [two setPurchaseSharePrice:2.30];
        [two setCurrentSharePrice:4.50];
        [two setNumberOfShares:40];
        //[stocks addObject:two];
        [portfolio addHoldings:two];
        
        BNRStockHolding *three = [[BNRStockHolding alloc] init];
        [three setPurchaseSharePrice:2.30];
        [three setCurrentSharePrice:4.50];
        [three setNumberOfShares:40];
        //[three setConversionRate:0.94];
        //[stocks addObject:three];
        [portfolio addHoldings:three];
        
        NSLog(@"%f", [portfolio valueOfPortfolio]);
        
//        NSLog(@"%@", portfolio.holdings);
//        for (BNRStockHolding *s in portfolio.holdings) {
//            //NSLog(@"cost:%.2f value:%.2f", [s costInDollars], [s valueInDollars]);
//            NSLog(@"%@", s);
//        }
//        NSLog(@"\n\nThe current value of your portfolio is: $%.2f", [portfolio totalValue]);

    }
    return 0;
}

Look harder, and you shall find it :slight_smile:

[quote]- (void)addHoldings:(BNRStockHolding *)holdings { if (!holdings) { _holdings = [[NSMutableArray alloc] init]; } [_holdings addObject]; }[/quote]
Avoid using names that sound the same but have different meanings (for example: holdings vs _holdings).

- (void)addHoldings:(BNRStockHolding *)it { if (!_holdings) { _holdings = [[NSMutableArray alloc] init]; } [_holdings addObject:it]; }

You my friend, have just saved my day. Thank you for the reply, advice, and extra set of eyes. :smiley: