My Solution to the Challenge


#1

Hey guys, this is my first post! I just wanted to see what you guys thought about my solution to the chapter 17 challenge. Let me know if I could do anything more efficiently or in a better way!

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; //purchase * numberOfShares
{
return [self numberOfShares] * [self purchaseSharePrice];
}
-(float) valueInDollars; //currentSharePrice * numberOfShares
{
return [self numberOfShares] * [self currentSharePrice];
}

@end
[/code]

main.m:

[code]
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    //Create 3 instances of StockHolding
    StockHolding * stock1 = [[StockHolding alloc] init];
    StockHolding * stock2 = [[StockHolding alloc] init];
    StockHolding * stock3 = [[StockHolding alloc] init];
    
    //Give the 3 instances values
    [stock1 setPurchaseSharePrice:2.30];
    [stock1 setCurrentSharePrice:4.50];
    [stock1 setNumberOfShares:40];
    
    [stock2 setPurchaseSharePrice:12.19];
    [stock2 setCurrentSharePrice:10.56];
    [stock2 setNumberOfShares:90];
    
    [stock3 setPurchaseSharePrice:45.10];
    [stock3 setCurrentSharePrice:49.51];
    [stock3 setNumberOfShares:210];
    
    
    
    //Throw those instances into an array
    int i = 1;
    NSMutableArray * myStocks = [[NSMutableArray alloc] initWithObjects:stock1,stock2,stock3, nil];
    for (StockHolding *sh in myStocks){
        NSLog(@"The purchase share price of stock %i is %.2f", i, [sh purchaseSharePrice]);
        NSLog(@"The current share price of stock %i is %.2f", i, [sh currentSharePrice]);
        NSLog(@"The number of shares of stock %i is %i", i, [sh numberOfShares]);
        NSLog(@"The cost in dollars of stock %i is %.2f", i, [sh costInDollars]);
        NSLog(@"The value in dollars of stock %i is %.2f", i, [sh valueInDollars]);
        
        i++;
            }
return 0;

}
}[/code]

And, finally, my output:

2012-06-26 15:40:12.486 Stocks[671:403] The purchase share price of stock 1 is 2.30 2012-06-26 15:40:12.488 Stocks[671:403] The current share price of stock 1 is 4.50 2012-06-26 15:40:12.489 Stocks[671:403] The number of shares of stock 1 is 40 2012-06-26 15:40:12.490 Stocks[671:403] The cost in dollars of stock 1 is 92.00 2012-06-26 15:40:12.490 Stocks[671:403] The value in dollars of stock 1 is 180.00 2012-06-26 15:40:12.491 Stocks[671:403] The purchase share price of stock 2 is 12.19 2012-06-26 15:40:12.491 Stocks[671:403] The current share price of stock 2 is 10.56 2012-06-26 15:40:12.492 Stocks[671:403] The number of shares of stock 2 is 90 2012-06-26 15:40:12.492 Stocks[671:403] The cost in dollars of stock 2 is 1097.10 2012-06-26 15:40:12.493 Stocks[671:403] The value in dollars of stock 2 is 950.40 2012-06-26 15:40:12.493 Stocks[671:403] The purchase share price of stock 3 is 45.10 2012-06-26 15:40:12.494 Stocks[671:403] The current share price of stock 3 is 49.51 2012-06-26 15:40:12.495 Stocks[671:403] The number of shares of stock 3 is 210 2012-06-26 15:40:12.495 Stocks[671:403] The cost in dollars of stock 3 is 9471.00 2012-06-26 15:40:12.496 Stocks[671:403] The value in dollars of stock 3 is 10397.10

I look forward to your input and thanks for reading my post!


#2

thats clever! thank you :slight_smile:


#3

I agree :slight_smile:
I added each instance of StockHolding seperately to my NSMutableArray, but your solution is much smarter than mine :slight_smile:


#4

Agreed, I like this solution. :slight_smile:
Good job.

I also added each instance separately but this logic is way more efficient.


#5

I’m having a little trouble understanding this portion of the code.

[code]//Create 3 instances of StockHolding
StockHolding * stock1 = [[StockHolding alloc] init];
StockHolding * stock2 = [[StockHolding alloc] init];
StockHolding * stock3 = [[StockHolding alloc] init];

    //Give the 3 instances values
    [stock1 setPurchaseSharePrice:2.30];
    [stock1 setCurrentSharePrice:4.50];
    [stock1 setNumberOfShares:40];
   
    [stock2 setPurchaseSharePrice:12.19];
    [stock2 setCurrentSharePrice:10.56];
    [stock2 setNumberOfShares:90];
   
    [stock3 setPurchaseSharePrice:45.10];
    [stock3 setCurrentSharePrice:49.51];
    [stock3 setNumberOfShares:210];[/code]

I understand that it is setting the values up but I’m not quite understanding how it’s doing this. I think i’ve missed something here. It could be late too but any “simple” explanation would help. :smiley:


#6

In response to Tyeron’s question about how the code is initializing the variables, all the initializations are variations of the following two code statements:

        StockHolding * stock1 = [[StockHolding alloc] init];
        [stock1 setPurchaseSharePrice:2.30];

The first statement creates a variable called stock1 and initializes all its properties to zeros. You now have a variable that includes properties called purchaseSharePrice, currentSharePrice and numberOfShares, all of which are currently set to 0.

Functions that get and set the values of those properties have been created by the compiler because of the earlier @synthesize purchaseSharePrice, currentSharePrice, numberOfShares statement. The functions that get a property’s current value have the same names as the properties themselves, as in purchaseSharePrice; the set functions are the property names with a prefix of “set,” as in setPurchaseSharePrice. The second code statement above is calling the set function created by @synthesize (i.e., setPurchaseSharePrice) to change the property’s value to 2.30.

There is an alternative syntax that can be used to assign property values. The alternative is called “dot notation” or “dot syntax” and is demonstrated below.

stock1.purchaseSharePrice = 2.30;

This syntax is equivalent to the original syntax in every way; neither syntax produces more efficient code than the other.

Hope this helps.