Chapter 17 Challenge, different format question


#1

I have basically the same solution that everyone else had, but I formatted my code a bit different. Is there any reason why mine would be worse than the others? I like that my stocks have their parameters near them, making it easier for me to see what I might need to change later if a stock went up or down.

Also, I added some if/else for profit, loss or no change. Is there a more elegant way to do this that I should already know? The back of my brain is telling me there is, but I couldn’t think of it.

StockHolding.h

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

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

@property NSString *name;
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;

@end
[/code]

StockHolding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize name, purchaseSharePrice, currentSharePrice, numberOfShares;

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

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

@end[/code]

main.m

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

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

@autoreleasepool {
    
    //Create Array
    NSMutableArray *holdings = [[NSMutableArray alloc]init];
    
    //Create Elan, Set values
    StockHolding *eln = [[StockHolding alloc]init];
    [eln setName:@"Elan"];
    [eln setPurchaseSharePrice:12.78];
    [eln setCurrentSharePrice:11.67];
    [eln setNumberOfShares:100];
    [holdings addObject:eln];
    
    //Create Apple, Set values
    StockHolding *appl = [[StockHolding alloc]init];
    [appl setName:@"Apple"];
    [appl setPurchaseSharePrice:322.57];
    [appl setCurrentSharePrice:415.33];
    [appl setNumberOfShares:100];
    [holdings addObject:appl];
    
    //Create Microsoft, Set values
    StockHolding *ms = [[StockHolding alloc]init];
    [ms setName:@"MicroSoft"];
    [ms setPurchaseSharePrice:57.44];
    [ms setCurrentSharePrice:57.44];
    [ms setNumberOfShares:100];
    [holdings addObject:ms];
    
    //Iterate through stocks, showing name, value, cost and profit. 
    for (StockHolding *d in holdings) {
        if ([d valueInDollars] > [d costInDollars]){
        NSLog(@"I have %@ stock worth %.2f dollars that cost me %.2f dollars giving me a profit of %.2f dollars.",
              [d name],[d valueInDollars],[d costInDollars], ([d valueInDollars] - [d costInDollars]));
        }else if ([d valueInDollars] < [d costInDollars]){
            NSLog(@"I have %@ stock worth %.2f dollars that cost me %.2f dollars giving me a loss of %.2f dollars.",
                  [d name],[d valueInDollars],[d costInDollars], ([d valueInDollars] - [d costInDollars]));
        }else if ([d valueInDollars] == [d costInDollars]){
            NSLog(@"I have %@ stock worth %.2f dollars that cost me %.2f, so there is neither profit nor loss.",
                  [d name],[d valueInDollars],[d costInDollars]);
        }}
return 0;
    }

}[/code]


#2

I don’t think the compiler cares?

Remember we’re going for code readability. I prefer to do it your way “group” them up and all the instance variables close together so we can quickly see what’s going on.