Tried something different


#1

So… I tried something different. I tired to use addYourselfToArray, but it didn’t work how I wanted it to. But, it semi worked so I just kept it in there. I was hoping to simplify it without having to create 3 instances of StockHolding and just add them to the array that way. But… when I changed values of holding for the 2nd or 3rd instance, it’d change the values of instance 1. In any case, all just a learning experience, eh? =)

StockHolding.h

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

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

- (void)addYourselfToArray:(NSMutableArray *)theArray;

@end

StockHolding.m

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice;
@synthesize currentSharePrice;
@synthesize numberOfShares;

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

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

- (void)addYourselfToArray:(NSMutableArray *)theArray
{
    [theArray addObject:self];
}


@end

main.m


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

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

    @autoreleasepool {
        
        NSMutableArray *theArray = [NSMutableArray array];
        int i = 1;
        
        StockHolding *holding = [[StockHolding alloc] init];
        StockHolding *holding2 = [[StockHolding alloc] init];
        StockHolding *holding3 = [[StockHolding alloc] init];
        
        [holding setPurchaseSharePrice:2.30];
        [holding setCurrentSharePrice:4.50];
        [holding setNumberOfShares:40];
        [holding addYourselfToArray];
        
        [holding2 setPurchaseSharePrice:12.19];
        [holding2 setCurrentSharePrice:10.56];
        [holding2 setNumberOfShares:90];
        [holding2 addYourselfToArray];
        
        [holding3 setPurchaseSharePrice:45.10];
        [holding3 setCurrentSharePrice:49.51];
        [holding3 setNumberOfShares:210];
        [holding3 addYourselfToArray];
        
        for(StockHolding *n in theArray) {
            NSLog(@"Stock %d", i);
            NSLog(@"Purchase Price: %.2f", [n purchaseSharePrice]);
            NSLog(@"Current Price: %.2f", [n currentSharePrice]);
            NSLog(@"Number of Shares: %d", [n numberOfShares]);
            NSLog(@"Cost in Dollars: %.2f", [n costInDollars]);
            NSLog(@"Value in Dollars: %.2f", [n valueInDollars]);
            i++;
        }
        
    }
    return 0;
}

#2

I just asked the same question in this thread: http://forums.bignerdranch.com/viewtopic.php?f=149&t=4868

I definitely think that you are barking up the right tree, but there is something in here that I’m just not quite getting.

Anybody figure out how to use the “add object” in the StockHolding class?


#3

To JeremiahFyffe. You can use the addObject in the StockHolding class. You define the method addYourselfToArray in the header and implementation file. Then you can call the method from main.m. No problem, just like you call any other method from main.m. This is my main.m

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

@autoreleasepool {
    
    NSMutableArray *stockHoldings = [[NSMutableArray alloc]init];
    StockHolding *stockOne = [[StockHolding alloc]init];
    [stockOne setPurchaseSharePrice:90];
    [stockOne setCurrentSharePrice:100];
    [stockOne setNumberOfShares:100];
    [stockOne addYourselfToArray:stockHoldings];
    [stockOne setName:@"StockOne"];
    
    StockHolding *stockTwo = [[StockHolding alloc]init];
    [stockTwo setPurchaseSharePrice:290];
    [stockTwo setCurrentSharePrice:2100];
    [stockTwo setNumberOfShares:2100];
    [stockTwo addYourselfToArray:stockHoldings];
    [stockTwo setName:@"StockTwo"];
    
    StockHolding *stockThree = [[StockHolding alloc]init];
    [stockThree setPurchaseSharePrice:390];
    [stockThree setCurrentSharePrice:3100];
    [stockThree setNumberOfShares:3100];
    [stockThree addYourselfToArray:stockHoldings];
    [stockThree setName:@"StockThree"];
    
    for (StockHolding *s in stockHoldings) {
        NSLog(@"%@ has a value of: %f \n and the cost was: %f", [s name], [s valueInDollars], [s costInDollars]);
    }       
}
return 0;

}
[/code]