My StockHolding Challenge solution


#1

My solution to the Stocks challenge. It could probably be tweaked a bit here and there but I feel quite comfortable with the concepts and code. My biggest hurdle so far seems to be figuring out what goes in which file (.h and .m), I always have to double check that. After that it’s the syntax, which will just come with time. Already it’s getting better. This is a great book. The challenges and the forums are making it work for me. I’ve read other OBJ-C books but without slogging through code of my own it just doesn’t sink in deep. I hope the IOS book big nerd has is just as good!

#import <Foundation/Foundation.h>


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


- (float)costInDollars:(float)pp:(float)ns;
- (float)valueInDollars:(float)cs:(float)ns;


@end

#import "StockHolding.h"


@implementation StockHolding

@synthesize purchaseSharePrice,currentSharePrice;
@synthesize numberOfShares;

-(float)costInDollars:(float)pp:(float)ns {
    return pp * ns;
    
}

-(float)valueInDollars:(float)cs:(float)ns {
    return cs * ns;
}

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

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     StockHolding *stockholder = [[StockHolding alloc] init];
    
    stockholder.purchaseSharePrice = 2.30;
    stockholder.currentSharePrice = 4.5;
    stockholder.numberOfShares = 40;
    
    StockHolding *stockholderTwo = [[StockHolding alloc] init];
    
    stockholderTwo.purchaseSharePrice = 12.19;
    stockholderTwo.currentSharePrice = 10.56;
    stockholderTwo.numberOfShares = 90;
    
    
    StockHolding *stockholderThree = [[StockHolding alloc] init];
    
    stockholderThree.purchaseSharePrice = 45.10;
    stockholderThree.currentSharePrice = 49.51;
    stockholderThree.numberOfShares = 210;
    
    
    NSMutableArray *holder = [NSMutableArray arrayWithObjects:stockholder,stockholderTwo,stockholderThree, nil];
 
    
    for (int i=0; i < [holder count]; i++){
        StockHolding *sdata = [holder objectAtIndex:i];
        float dollarcost = [sdata costInDollars:sdata.purchaseSharePrice :sdata.numberOfShares];    
        float dollarvalue = [sdata valueInDollars:sdata.currentSharePrice :sdata.numberOfShares]; 

        NSLog(@"PurchasePrice $%.2f, Current Price $%.2f, Num Shares %i, DollarCost $%.2f, DollarValue $%.2f", sdata.purchaseSharePrice, sdata.currentSharePrice,sdata.numberOfShares,dollarcost,dollarvalue);
    }
    
    
    
    
    
    
    
    [pool drain];
    return 0;
}

#2

I think it might be better to declare your costInDollars like this:

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

#3

These are my answers. They seem to work, yet I somehow feel that I’m not doing this all too efficiently. Any comments/suggestions would be much appreciated!

StockHolding.h:

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

@interface StockHolding : NSObject

{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;
@end
[/code]

StockHolding.m:

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars
    {
    float h = [self purchaseSharePrice];
    return [self numberOfShares] * h;
    }

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

@end
[/code]

main.m:

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

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

@autoreleasepool {
    
    StockHolding *sh1 = [[StockHolding alloc] init];
    
    [sh1 setPurchaseSharePrice:2.30];
    [sh1 setCurrentSharePrice:4.50];
    [sh1 setNumberOfShares:40];
    
    StockHolding *sh2 = [[StockHolding alloc] init];
    
    [sh2 setPurchaseSharePrice:12.19];
    [sh2 setCurrentSharePrice:10.56];
    [sh2 setNumberOfShares:90];
    
    StockHolding *sh3 = [[StockHolding alloc] init];
    
    [sh3 setPurchaseSharePrice:45.10];
    [sh3 setCurrentSharePrice:49.51];
    [sh3 setNumberOfShares:210];
    
    NSArray *stocklist = [NSArray arrayWithObjects: sh1, sh2, sh3, nil];
    
    NSUInteger count = [stocklist count];
    
    for (int i=0; i<count; i++) {
        float dollarcost = [[stocklist objectAtIndex:i] costInDollars];
        float dollarvalue =[[stocklist objectAtIndex:i] valueInDollars];
        NSLog(@"The cost in dollars was %f, while the current value in dollars is %f", dollarcost, dollarvalue);
    }
    
}

}[/code]