My solution


#1

I guess I kinda cheated a little, in that I’d come across properties before elsewhere, so I already knew how to use them to an extent.
Rewriting with proper setters and getters wouldn’t be difficult, just time consuming :slight_smile:

Here’s my solution:

BNRStockHolding.h

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

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic) NSString *nameOfStock;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares

@end
[/code]

BNRStockHolding.m

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

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

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

@end
[/code]

main.m

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

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

@autoreleasepool {

    BNRStockHolding *csco = [[BNRStockHolding alloc]init];
    [csco setNameOfStock:@"CSCO"];
    [csco setPurchaseSharePrice:22.57];
    [csco setCurrentSharePrice:25.24];
    [csco setNumberOfShares:8000];
    BNRStockHolding *aapl = [[BNRStockHolding alloc]init];
    [aapl setNameOfStock:@"AAPL"];
    [aapl setPurchaseSharePrice:83.45];
    [aapl setCurrentSharePrice:97.89];
    [aapl setNumberOfShares:35];
    BNRStockHolding *bbry = [[BNRStockHolding alloc]init];
    [bbry setNameOfStock:@"BBRY"];
    [bbry setPurchaseSharePrice:12.23];
    [bbry setCurrentSharePrice:7.24];
    [bbry setNumberOfShares:100];

    NSMutableArray *stocks = [[NSMutableArray alloc]init];
    [stocks addObject:csco];
    [stocks addObject:aapl];
    [stocks addObject:bbry];

    for (BNRStockHolding *stock in stocks) {
        NSLog(@"\n%@ stocks cost %.2f$, now worth %.2f$. Value changed by %.2f$.", stock.nameOfStock, stock.costInDollars, stock.valueInDollars, (stock.valueInDollars - stock.costInDollars));
    }
   
}
return 0;

}
[/code]


#2

The challenge read:
“In main(), fill an array with three instances of BNRStockHolding.
Then iterate through the array printing out the value of each”

ur solution is ok, but u r printing only costInDollars: and valueInDollars.

regards