Challenge - Am I using "Self" correctly?


#1

Here’s my solution. It works fine and I enjoyed playing around with the formatting of the output in main, but…

I believe I have ‘self’ used correctly in my .m file, but I’m still not certain I understand the point. Please compare the method .m file to the usage in main to tell me if I’m doing this right…

[code]//
// StockHolding.h
// Stocks
//
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
// Insert instance variable floats purchaseSharePrice and currentSharePrice and int named numberOfShares

float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;

}

// accessor methods

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

// instance methods

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

@end
[/code]

[code]//
// StockHolding.m
// Stocks
//
//

#import “StockHolding.h”

@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

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

@end
[/code]

[code]//
// main.m
// Stocks
//
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”

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

@autoreleasepool {
    
    // fill in an array with three instances of StockHolding
   //create 3 instances
    StockHolding *stock1 = [[StockHolding alloc] init];
    StockHolding *stock2 = [[StockHolding alloc] init];
    StockHolding *stock3 = [[StockHolding alloc] init];
    
    // give values to the stocks
    
    [stock1 setPurchaseSharePrice:2.25];
    [stock1 setCurrentSharePrice:8.95];
    [stock1 setNumberOfShares:30];
    [stock2 setPurchaseSharePrice:6.66];
    [stock2 setCurrentSharePrice:3.33];
    [stock2 setNumberOfShares:5];
    [stock3 setPurchaseSharePrice:5.56];
    [stock3 setCurrentSharePrice:17.99];
    [stock3 setNumberOfShares:34];
    
    
    
    NSMutableArray *myStocks = [NSMutableArray arrayWithObjects:stock1, stock2, stock3, nil];
    
    
    
    // iterate through the array printing out each value
    
    for (StockHolding *s in myStocks) {
        float c = [s costInDollars];
        float v = [s valueInDollars];
        float p = [s purchaseSharePrice];
        float cu = [s currentSharePrice];
        int n = [s numberOfShares];
        NSUInteger i = [myStocks indexOfObject:s];
        float net = [s valueInDollars] - [s costInDollars];
        if (net >= 0) {
             NSLog(@" \n Stock %lu.) \n  Purchased at %.2f per share \n  Current price at %.2f per share \n  %i share(s) valued at %.2f \n  Orignal cost of %.2f. \n  We are in the black! \n  Net Value: %.2f \n \n", (i+1), p, cu, n, v, c, net);
        }else{
            NSLog(@" \n Stock %lu.) \n  Purchased at %.2f per share \n  Current price at %.2f per share \n  %i share(s) valued at %.2f \n  Orignal cost of %.2f. \n  We are in the red... \n  Net Value: %.2f \n \n", (i+1), p, cu, n, v, c, net);
        }
        
    }
}
return 0;

}

[/code]

Ouput to log:

[code]2013-10-20 09:31:11.149 Stocks[2888:303]
Stock 1.)
Purchased at 2.25 per share
Current price at 8.95 per share
30 share(s) valued at 268.50
Orignal cost of 67.50.
We are in the black!
Net Value: 201.00

2013-10-20 09:31:11.151 Stocks[2888:303]
Stock 2.)
Purchased at 6.66 per share
Current price at 3.33 per share
5 share(s) valued at 16.65
Orignal cost of 33.30.
We are in the red…
Net Value: -16.65

2013-10-20 09:31:11.152 Stocks[2888:303]
Stock 3.)
Purchased at 5.56 per share
Current price at 17.99 per share
34 share(s) valued at 611.66
Orignal cost of 189.04.
We are in the black!
Net Value: 422.62

Program ended with exit code: 0[/code]


#2

Yeah it’s right. I don’t know if you have some programming experience but it’s a lot like the keyword “this” used in other language such as Java.