Challenge Solution


#1

I’ve attempted to solve each challenge using only the material provided in the book thus far. I suppose because of that my solutions can be a bit “creative.” This one seems extraordinarily “creative,” though, and overly verbose. It works, but isn’t pretty. Any suggestions?

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]

m.

[code]#import “StockHolding.h”

@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars
    {
    return [self numberOfShares] * [self purchaseSharePrice];
    }
  • (float)valueInDollars
    {
    return [self numberOfShares] * [self currentSharePrice];
    }
    @end[/code]

main.c:

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

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

@autoreleasepool {
    
    StockHolding *stockHoldingOne = [[StockHolding alloc] init];
    
    [stockHoldingOne setPurchaseSharePrice:2.3];
    [stockHoldingOne setCurrentSharePrice:5.4];
    [stockHoldingOne setNumberOfShares:40];
    
    StockHolding *stockHoldingTwo = [[StockHolding alloc] init];
    
    [stockHoldingTwo setPurchaseSharePrice:12.19];
    [stockHoldingTwo setCurrentSharePrice:10.56];
    [stockHoldingTwo setNumberOfShares:90];
    
    StockHolding *stockHoldingThree = [[StockHolding alloc] init];
    
    [stockHoldingThree setPurchaseSharePrice:45.10];
    [stockHoldingThree setCurrentSharePrice:49.51];
    [stockHoldingThree setNumberOfShares:210];
    
    NSMutableArray *sHArray = [NSMutableArray array];
    
    [sHArray addObject:stockHoldingOne];
    [sHArray addObject:stockHoldingTwo];
    [sHArray addObject:stockHoldingThree];
    
    NSUInteger objectCount = [sHArray count];
    for (int i = 0; i < objectCount; i++) {
        StockHolding *s = [sHArray objectAtIndex:i];
        NSLog(@"Value In Dollars is %f. Cost In Dollars is %f", [s valueInDollars], [s costInDollars]);
    }
    
    
}
return 0;

}[/code]

Print result:
2014-03-04 21:30:06.455 Stocks[516:303] Value In Dollars is 216.000000. Cost In Dollars is 92.000000
2014-03-04 21:30:06.457 Stocks[516:303] Value In Dollars is 950.400024. Cost In Dollars is 1097.099976
2014-03-04 21:30:06.457 Stocks[516:303] Value In Dollars is 10397.099609. Cost In Dollars is 9471.000000
Program ended with exit code: 0

Concerns (in no particular order):
A. I’d like to use fast enumeration here.
B. I’d also like to create a method like the one mentioned in the chapter like “addYourselfToArray.” I haven’t figured out how to do either successfully.
C. It seems like there should be a way to set the instance variable values outside of main.
D. I also wouldn’t mind cleaning this up a lot.

Go easy on me. I’m completely new to programming. :neutral_face:


#2

Your code looks pretty good.

[quote]A. I’d like to use fast enumeration here.

NSUInteger objectCount = [sHArray count]; for (int i = 0; i < objectCount; i++) { StockHolding *s = [sHArray objectAtIndex:i]; NSLog(@"Value In Dollars is %f. Cost In Dollars is %f", [s valueInDollars], [s costInDollars]); }
[/quote]
That’s easy to do:

for (StockHolding *s in sHArray ) {
     NSLog (@"Value In Dollars is %f. Cost In Dollars is %f", [s valueInDollars], [s costInDollars]);
}

[quote]B. I’d also like to create a method like the one mentioned in the chapter like “addYourselfToArray.” I haven’t figured out how to do either successfully.
[/quote]
That too is easy - just add a new method:

@interface StockHolding : NSObject
...
- (void)addToHoldings:(NSMutableArray *)holdings;
@end

Now implement that method:

@implementation StockHolding
...
- (void)addToHoldings:(NSMutableArray *)holdings
{
   [holdings addObject:self];
}
...
@end

[quote]C. It seems like there should be a way to set the instance variable values outside of main.

[code]StockHolding *stockHoldingThree = [[StockHolding alloc] init];

[stockHoldingThree setPurchaseSharePrice:45.10];
[stockHoldingThree setCurrentSharePrice:49.51];
[stockHoldingThree setNumberOfShares:210];[/code][/quote]
You mean something like this?

StockHolding *stockHoldingThree = [[StockHolding alloc] initWithPurchasePrice:45.10 currentPrice:49.51 numberOfShares:210];

#3

@ibex10 Thanks! I was trying to implement the method without adding it. :blush: