My solution to the problem with stocks

in PDRStockHolding.h

#import <Foundation/Foundation.h>

@interface PDRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

- (float)costInDollars; //purchaseSharePrice * number of shares
- (float)valueInDollars; //currentSharePrice * numberOfShares


@end

in PDRStockHolding.M

[code]#import “PDRStockHolding.h”

@implementation PDRStockHolding
@synthesize purchaseSharePrice;
@synthesize numberOfShares;
@synthesize currentSharePrice;

  • (float)costInDollars{
    float cost = purchaseSharePrice * numberOfShares;
    return cost;
    }

  • (float)valueInDollars{
    float value = currentSharePrice * numberOfShares;
    return value;
    }
    @end
    [/code]

and the hard part in main.m

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

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

    NSMutableArray *holdings = [[NSMutableArray alloc] init];
    
    
    PDRStockHolding *one = [[PDRStockHolding alloc]init];
    one.purchaseSharePrice = 2.30;
    one.currentSharePrice = 4.50;
    one.numberOfShares = 40;
    [holdings addObject:one];
    
    PDRStockHolding *two = [[PDRStockHolding alloc]init];
    two.purchaseSharePrice = 12.19;
    two.currentSharePrice = 10.56;
    two.numberOfShares = 90;
    [holdings addObject:two];
    
    PDRStockHolding *three = [[PDRStockHolding alloc] init];
    three.purchaseSharePrice = 45.10;
    three.currentSharePrice = 49.51;
    three.numberOfShares = 210;
    [holdings addObject:three];
    
    for(PDRStockHolding *number in holdings) {
        NSLog(@"current Price is %f, purchased at %f and %i number of shares", [number currentSharePrice], [number purchaseSharePrice], [number numberOfShares] );
    }
   
    
}
return 0;

}
[/code] very interesting I have to say that I had to think things over to really understand the challenge I was doing. especially the hardest part for me must have been the iterating part, and how to include all the values of my objects.

Forgot to add that I included the [number costInDollars] message and the [number valueInDollars] afterwards, and of course the types in the NSLog.