Challenge - A bit different approach


#1
//
//  BNRStockHolding.h
//  Stocks
//
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

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

}

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

    - (float) purchaseSharePrice;
    - (float) currentSharePrice;
    - (int)   numberOfShares;

    - (void) setPurchaseSharePrice:(float)psp;
    - (void) setCurrentSharePrice:(float)csp;
    - (void) setNumberOfShares:(int)numshares;
@end
//
//  BNRStockHolding.m
//  Stocks
//
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import "BNRStockHolding.h"

@implementation BNRStockHolding


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

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

- (float) purchaseSharePrice;
{
    return _purchaseSharePrice;
}

- (float) currentSharePrice;
{
    return _currentSharePrice;
}

- (int)   numberOfShares;
{
    return _numberOfShares;
}

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

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

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


@end

main…

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

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

    @autoreleasepool {
 
        NSMutableArray  *portfolio = [[NSMutableArray alloc] init];
        
        BNRStockHolding *stock = [[BNRStockHolding alloc] init];
        [stock setPurchaseSharePrice:2.30];
        [stock setCurrentSharePrice:4.50];
        [stock setNumberOfShares:40];
        [portfolio addObject:stock];

        stock = nil;
        stock = [[BNRStockHolding alloc] init];
        [stock setPurchaseSharePrice:12.19];
        [stock setCurrentSharePrice:10.56];
        [stock setNumberOfShares:90];
        [portfolio addObject:stock];
        
        stock = nil;
        stock = [[BNRStockHolding alloc] init];
        [stock setPurchaseSharePrice:45.10];
        [stock setCurrentSharePrice:49.51];
        [stock setNumberOfShares:210];
        [portfolio addObject:stock];
        
        int i = 0;
        
        for (BNRStockHolding *s in portfolio ){
            i++;
            NSLog(@"Stock: %i- Cost: %.2f Shares: %i Value: %.2f", i, [s costInDollars], [s numberOfShares], [s valueInDollars] );
        }
        
        
    }
    return 0;
}

I approached it a bit differently then some of the others. I created only one instance of BNRStockHolding, set the values, added it to the array, then wiped it clean and reallocated it. It achieved the same thing but was just trying something different


#2

I too used a similar approach of reusing a single object. I don’t believe that “stock = nil;” is needed. By executing “stock = [[BNRStockHolding alloc] init];” you are creating a new object instance. The old one is still valid because the array is referencing it.

I tested it and it works but I would appreciate a confirmation that this is correct form.

Thanks!


#3

Question for richfrag:
why does placing the int i =0 outside the enumeration code make a difference? I know that it does, but can’t wrap my head around why? The i++ is still inside the loop, so why does it work correctly only when i=0 is outside?
thx


#4

[quote=“szeezild”]Question for richfrag:
why does placing the int i =0 outside the enumeration code make a difference? I know that it does, but can’t wrap my head around why? The i++ is still inside the loop, so why does it work correctly only when i=0 is outside?
thx[/quote]

If you place it inside the loop, it will initialize back to 0 on each iteration then increment to 1 again.