Please validate my challenge solution


#1

I spent three days (on and off) trying to understand these 2 challenges (Chapter 21 and 22) and by far these were the most difficult concept for me to grasp.

I’m attaching my code, please verify my solution for me. Thank you!

main.m

//
//  main.m
//  Stocks
//
//  Created by Max Doumkine on 2/17/14.
//  Copyright (c) 2014 BigNerdRanch. All rights reserved.
//

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

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

    @autoreleasepool {
        // Initiating an NSMutableArray to hold
        // created stock instances
        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        
        
        // StockHolding first instance
        BNRForeignStockHolding *microsoft = [[BNRForeignStockHolding alloc] init];
        
        // Asigning values to first instance
        microsoft.purchaseSharePrice = 2.30;
        microsoft.currentSharePrice = 4.50;
        microsoft.numberOfShares = 40;
        microsoft.conversionRate = 2.0;
        microsoft.symbol = @"MIC";
        [stocks addObject:microsoft];
        
        // StockHolding second instance
        BNRForeignStockHolding *apple = [[BNRForeignStockHolding alloc] init];
        
        // Asigning values to second instance
        apple.purchaseSharePrice = 12.19;
        apple.currentSharePrice = 10.56;
        apple.numberOfShares = 90;
        apple.conversionRate = 1.2;
        apple.symbol = @"APL";
        [stocks addObject:apple];
        
        // StockHolding third instance
        BNRForeignStockHolding *redhat = [[BNRForeignStockHolding alloc] init];
        
        // Asigning values to third instance
        redhat.purchaseSharePrice = 45.10;
        redhat.currentSharePrice = 49.51;
        redhat.numberOfShares = 210;
        redhat.conversionRate = 1.5;
        redhat.symbol = @"RDH";
        [stocks addObject:redhat];
        
        // Initiating portfolio and adding stocks to it
        BNRPortfolio *stockHoldingsPortfolio = [[BNRPortfolio alloc] init];
        [stockHoldingsPortfolio addStock:stocks[0]];
        [stockHoldingsPortfolio addStock:stocks[1]];
        [stockHoldingsPortfolio addStock:stocks[2]];
        
        // Removing stocks from portfolio.
        [stockHoldingsPortfolio removeStock:stocks[2]];
        
        NSLog(@"%@", stockHoldingsPortfolio);
    }
    return 0;
}

BNRPortfolio.h

//
//  BNRPortfolio.h
//  Stocks
//
//  Created by Max Doumkine on 2/20/14.
//  Copyright (c) 2014 BigNerdRanch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRPortfolio : NSObject

@property (nonatomic) unsigned int portfolioValue;
@property (nonatomic, copy) NSArray *portfolio;

- (void)addStock:(BNRPortfolio *)p;
- (void)removeStock:(BNRPortfolio *)p;
- (unsigned int)valueOfPortfolio;
- (NSString *)stockSymbol;

@end

BNRPortfolio.m

//
//  BNRPortfolio.m
//  Stocks
//
//  Created by Max Doumkine on 2/20/14.
//  Copyright (c) 2014 BigNerdRanch. All rights reserved.
//

#import "BNRPortfolio.h"
#import "BNRForeignStockHolding.h"

@interface BNRPortfolio ()
{
    NSMutableArray *_portfolio;
}
@end

@implementation BNRPortfolio
- (NSString *)description
{
    return [NSString stringWithFormat:@"Portfolio is worth $%d dollars and has %lu stocks\n", self.valueOfPortfolio, (unsigned long)[_portfolio count]];
}


- (unsigned int)valueOfPortfolio
{
    // Sum up the value of portfolio stocks
    unsigned int sum = 0;
    
    for (BNRForeignStockHolding *company in _portfolio){
        sum += company.valueInDollars;
    }
    return sum;
}


- (void)addStock:(BNRPortfolio *)p
{
    //Is portfolio nil?
    if(!_portfolio) {
        // Create the array
        _portfolio = [[NSMutableArray alloc] init];
    }
    //Adding object to the portfolio array
    [_portfolio addObject:p];
}

- (void)removeStock:(BNRPortfolio *)p
{
    if(_portfolio) {
        [_portfolio removeObject:p];
        NSLog(@"Removing object from portfolio");
    }
}

@end

BNRForeignStockHolding.h

//
//  BNRForeignStockHolding.h
//  Stocks
//
//  Created by Max Doumkine on 2/17/14.
//  Copyright (c) 2014 BigNerdRanch. All rights reserved.
//

#import "BNRStockHolding.h"

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;
@property (nonatomic, copy) NSString *symbol;

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


@end

BNRForeignStockHolding.m

//
//  BNRForeignStockHolding.m
//  Stocks
//
//  Created by Max Doumkine on 2/17/14.
//  Copyright (c) 2014 BigNerdRanch. All rights reserved.
//

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding
- (float)costInDollars;
{
    return self.purchaseSharePrice * self.numberOfShares * self.conversionRate;
}

- (float)valueInDollars;
{
    return self.currentSharePrice * self.numberOfShares * self.conversionRate;
}

@end

#2

Some thoughts from my perspective:

Main.m:

1/ There is no reason to create an NSMutable Array here. Go ahead and set all of your stocks and then simply submit each BNRStockHolding/BNRForeignStockHolding to your addStock method in the portfolio class.

BNRPortfolio.h:

1/ You should not have your NSArray defined here. The purpose of this exercise is to hide it from public deceleration and therefore it should move to your implementation as a NSMutableArray.
2/ Not clear why you need a property for the portfolioValue. This should simple be a return value from your valueOfPortfolio method and thus a separate property is unneeded.
3/ You have a method declared called stockSymbol. I am not sure what this is for and it does not look like it is defined in the implementation detail you provided.

BNRPortfolio.m:

1/ Probably just a style thing but I declared the NSMutableArray as a property and then overrode the getter to allocate the array if it didn’t exist.
2/ Not sure why your addStock takes a BNRPortfolio object. You are adding a BNRStockHolding to your portfolio not a portfolio to your portfolio.

BNRForeignStockHolding.h/.m:

1/ You should make use of your super classes calculation for cost and value rather than writing it again here. An example:

Not only does this save retyping something already done but if later you decided to change the calculation for cost or value you would only have to do it once rather than remember to go to every class that might also need it.

2/As earlier I am not sure I understand your intentions with the symbol property you declared.

Hope this helps a little.


#3

C6silver: could you please post your answer to this challenge?

The thing that I don’t entirely get is the add/remove methods. I completed the earlier challenges of creating these methods and I declared them in BNRPortfolio.h, implemented them in the .m file. But this Ch 22 challenge asks to hid the [NSMutableArray *holdings] (which I understand) - AND the add/remove methods (which I am getting confused on). If the add/remove methods are hidden from the .h file, does that mean that they wouldn’t be callable from main.m?