Challenge BNRStockHolding. Please validate solution for me

:geek:

I’m not sure about this, I did it the best way I understood how. Please correct me if I’m wrong and point out the flaws. Thank you.

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

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

@autoreleasepool {
    
    // StockHolding first instance
    BNRStockHolding *newStock1 = [[BNRStockHolding alloc] init];
    
    // Asigning values to first instance
    newStock1.purchaseSharePrice = 2.30;
    newStock1.currentSharePrice = 4.50;
    newStock1.numberOfShares = 40;
    
    // StockHolding second instance
    BNRStockHolding *newStock2 = [[BNRStockHolding alloc] init];
    
    // Asigning values to second instance
    newStock2.purchaseSharePrice = 12.19;
    newStock2.currentSharePrice = 10.56;
    newStock2.numberOfShares = 90;
    
    // StockHolding third instance
    BNRStockHolding *newStock3 = [[BNRStockHolding alloc] init];
    
    // Asigning values to third instance
    newStock3.purchaseSharePrice = 45.10;
    newStock3.currentSharePrice = 49.51;
    newStock3.numberOfShares = 210;
    
    // Initiating an NSMutableArray to hold above
    // created stock instances
    NSMutableArray *stockHolder = [[NSMutableArray alloc] init];
    
    // Adding StockHolding instance objects to
    // the stockHolder mutable array
    [stockHolder addObject:newStock1];
    [stockHolder addObject:newStock2];
    [stockHolder addObject:newStock3];
    
    // Looping through the stockHolder array
    for(BNRStockHolding *holderArray in stockHolder) {
        
        // Displaying cost and value in dollars.
        NSLog(@"Cost In Dollars is:%f\n", holderArray.costInDollars);
        NSLog(@"Value In Dollars is:%f\n", holderArray.valueInDollars);
    }
    
}
return 0;

}[/code]

Good but …

[quote] ... NSMutableArray *stockHolder = [[NSMutableArray alloc] init]; ... // Looping through the stockHolder array for (BNRStockHolding *holderArray in stockHolder) { // Displaying cost and value in dollars. NSLog(@"Cost In Dollars is:%f\n", holderArray.costInDollars); NSLog(@"Value In Dollars is:%f\n", holderArray.valueInDollars); }[/quote]
Variable names are a bit confusing for humans.

Try using variable names that are clear:

... NSMutableArray *stocksArray = [[NSMutableArray alloc] init]; ... // Looping through the stocksArray for (BNRStockHolding *stock in stocksArray) { // Displaying cost and value in dollars. NSLog (@"Cost In Dollars is:%f\n", stock.costInDollars); NSLog (@"Value In Dollars is:%f\n", stock.valueInDollars); }

Hi all, this was my approach to solve the BNRStockHolding Challenge, hope it can helps you! Also thanks to share your approaches and code! Have a great day!

For the main.m is

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

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

@autoreleasepool {
    //Creating and initializing the array for holding the stocks
    
    NSMutableArray *stockArray = [[NSMutableArray alloc] init];
    
    //Creating and initializing the BNRStockHolding Object
    
    BNRStockHolding *stock = [[BNRStockHolding alloc] init];
    [stock setPurchaseSharePrice:10.0];
    [stock setCurrentSharePrice:5.0];
    [stock setNumberOfShares:2];
    [stockArray addObject:stock];
    
    //I used the same object, with this approach, the object values goes to another memory block and you can re use it.
    
    stock = [[BNRStockHolding alloc] init];
    [stock setPurchaseSharePrice:15.0];
    [stock setCurrentSharePrice:10.0];
    [stock setNumberOfShares:3];
    [stockArray addObject:stock];
    
    //Also Here
    
    stock = [[BNRStockHolding alloc] init];
    [stock setPurchaseSharePrice:20.0];
    [stock setCurrentSharePrice:15.0];
    [stock setNumberOfShares:4];
    [stockArray addObject:stock];
    
    for (int i = 0; i < [stockArray count]; i++) {
        
        //Getting the values in S for every BNRStockHolding in the Array
        //I could use fast enumeration but i was trying with for-loop.
        
        BNRStockHolding * s = [stockArray objectAtIndex:i];
        NSLog(@"The cost in dolars for the %d stock is %.2f\n",i,[s costInDollars]);
        NSLog(@"The cost in dolars for the %d stock is %.2f\n",i,[s valueInDollars]);
    }
    
    
}
return 0;

}
[/code]

For the BNRStockHolding.h:

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

@interface BNRStockHolding : NSObject
{
//Instance Variables

float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;

}

//Getters and Setters

-(float)purchaseSharePrice;
-(void)setPurchaseSharePrice:(float)purchaseSharePrice;
-(float)currentSharePrice;
-(void)setCurrentSharePrice:(float)currentSharePrice;
-(int)numberOfShares;
-(void)setNumberOfShares:(int)numberOfShares;

//Instance Methods

-(float)costInDollars; // PurchaseSharePrice * numberOfShares
-(float)valueInDollars; //currentSharePrice * numberOfShares

@end
[/code]

And for the BNRStockHolding.m is

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

-(float)purchaseSharePrice
{
return _purchaseSharePrice;
}

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

-(float)currentSharePrice
{
return _currentSharePrice;
}

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

-(int)numberOfShares
{
return _numberOfShares;
}

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

-(float)costInDollars
{
return [self purchaseSharePrice] * [self numberOfShares];
}

-(float)valueInDollars
{
return [self currentSharePrice] * [self numberOfShares];
}

@end
[/code]

[quote=“jdecuirm”]Hi all, this was my approach to solve the BNRStockHolding Challenge, hope it can helps you! Also thanks to share your approaches and code! Have a great day!

Yes, your approach worked. However, please keep in mind that once you reuse the stock object you have removed the original reference to the memory heap, leaving only stockArray holding a reference to that memory space. nil the stockArray and all references to the 3 instances of the stock object are gone.

Mark H