Challenge NSMutableArray

Hi, i’m new working with this gorgeous book and, I like if you have time to review if my code is well developed and according with the proposal of the challenge.

[code]#import <Foundation/Foundation.h>

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

  • (float)purchaseSharePrice;

  • (void)setPurchaseSharePrice:(float)p;

  • (float)currentSharePrice;

  • (void)setCurrentSharePrice:(float)c;

  • (int)numberOfShares;

  • (void)setNumberOfShares:(int)n;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares

  • (float)valueInDollars; // currentSharePrice * numberOFShare

@end[/code]

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)purchaseSharePrice
    {
    return _purchaseSharePrice;
    }

  • (void)setPurchaseSharePrice:(float)p
    {
    _purchaseSharePrice = p;
    }

  • (float)currentSharePrice
    {
    return _currentSharePrice;
    }

  • (void)setCurrentSharePrice:(float)c
    {
    _currentSharePrice = c;
    }

  • (int)numberOfShares
    {
    return _numberOfShares;
    }

  • (void)setNumberOfShares:(int)n
    {
    _numberOfShares = n;
    }

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

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

@end[/code]

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

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

@autoreleasepool {
    
    BNRStockHolding *almacen1 = [[BNRStockHolding alloc] init];
    BNRStockHolding *almacen2 = [[BNRStockHolding alloc] init];
    BNRStockHolding *almacen3 = [[BNRStockHolding alloc] init];
    
    // Mount of products on almacen1
    [almacen1 setPurchaseSharePrice:2.30];
    [almacen1 setCurrentSharePrice:4.50];
    [almacen1 setNumberOfShares:40];
    [almacen1 costInDollars];
    [almacen1 numberOfShares];
    
    // Mount of productos on almacen2
    [almacen2 setPurchaseSharePrice:12.19];
    [almacen2 setCurrentSharePrice:10.56];
    [almacen2 setNumberOfShares:90];
    [almacen2 costInDollars];
    [almacen2 numberOfShares];
    
    // Mount of products on almacen3
    [almacen3 setPurchaseSharePrice:45.10];
    [almacen3 setCurrentSharePrice:49.51];
    [almacen3 setNumberOfShares:210];
    [almacen3 costInDollars];
    [almacen3 numberOfShares];
    
    NSMutableArray *temp = [NSMutableArray arrayWithObjects:almacen1, almacen2, almacen3, nil];
    
    for(BNRStockHolding *s in temp){
        float a = [s purchaseSharePrice];
        float b = [s currentSharePrice];
        int c = [s numberOfShares];
        float d = [s costInDollars];
        float e = [s valueInDollars];
        
        NSLog(@"\n Purchase Prices: %.2f \n Current Share Price: %.2f \n Number Shares: %d \n Cost in dollars: %.2f \n Values in dollars: %.2f", a, b, c, d, e);
    }
    
}
return 0;

}[/code]

// The result 2014-02-26 18:34:47.308 Stock[466:303] Purchase Prices: 2.30 Current Share Price: 4.50 Number Shares: 40 Cost in dollars: 92.00 Values in dollars: 180.00 2014-02-26 18:34:47.309 Stock[466:303] Purchase Prices: 12.19 Current Share Price: 10.56 Number Shares: 90 Cost in dollars: 1097.10 Values in dollars: 950.40 2014-02-26 18:34:47.309 Stock[466:303] Purchase Prices: 45.10 Current Share Price: 49.51 Number Shares: 210 Cost in dollars: 9471.00 Values in dollars: 10397.10 Program ended with exit code: 0

Everything looks pretty standard except the part in your code where you’re making method calls for no reason. When you’re creating your stock objects, you’re calling the two methods that calculate the stock prices:

// Mount of products on almacen1 [almacen1 setPurchaseSharePrice:2.30]; [almacen1 setCurrentSharePrice:4.50]; [almacen1 setNumberOfShares:40]; [almacen1 costInDollars]; //THIS ONE [almacen1 numberOfShares]; //AND THIS ONE

This won’t throw an error but it would make your code and your application inefficient. You’re already making these method calls in your for loop when you are actually going to use them.