My challange solution but confused with fig. 18.3 page 11


#1

So i’ve completed the challange in slightly different way (I used the init method to initialise my ivars and then used a random function to give random values). Would really appreciate any comments on this.

Anyway, I’m confused with figure 18.3 because it shows a total of 5 objects contained in a NSMutableArray at only 3 indexes ? Why is it not 5 objects at 5 indexes ?
My solution starts with 3 objects created from the StockHolding class and placed in portfolio (NSMutableArray). I then sub classed StockHolding with ForeignStockHolding and created 2 objects and placed them in my portfolio. That gives a total of 5 objects in 5 indexes (0 - 4). I thought I had completed this challenge but figure 18.3 tells me I’m not understanding the question - please help !

[code]

//StockHolding H

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject {

float purchaseSharePrice;
float currentSharePrice;
int numberOfShare;

}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShare;

-(float)costInDollars;
-(float)valueInDollars;
-(void)profitMade;

@end[/code]


//StockHolding M

#import "StockHolding.h"

@implementation StockHolding

@synthesize  purchaseSharePrice;
@synthesize  currentSharePrice;
@synthesize  numberOfShare;

- (id)init
{
    self = [super init];
    if (self) {
        
        //super init ivars get random floats and one int.
        // arc4random() % 100 generates a random number between 0 - 100 ...cool ! 
        
          self.purchaseSharePrice = arc4random() % 100;
          self.currentSharePrice =  arc4random() % 100;
          self.numberOfShare =  arc4random() % 100;
         
        
    }
    
    return self;
}


-(float)costInDollars{


    return self.purchaseSharePrice * self.numberOfShare;
}

-(float)valueInDollars{

    return self.numberOfShare * self.currentSharePrice;
}

// This method simply prints out a profit or loss message
- (void)profitMade{
    
    float x = self.currentSharePrice - self.purchaseSharePrice ;
    
    if (x * self.numberOfShare >= 3) {
        NSLog(@"Nice ! You made a profit of $%.0f",x * self.numberOfShare);
    }
    
    if (x * self.numberOfShare <= 0) {
        NSLog(@"Bad times ! You made a loss of $%.0f",x * self.numberOfShare);
    }
   
}

@end

// ForeignStockHolding H

#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding {

    float conversionRate;

}

@property float conversionRate;

@end

// ForeignStockHolding M


#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

- (id)init
{
    self = [super init];
    if (self) {
       
        // super init conversionRate (converts UK£ - US$)
        
        self.conversionRate = 0.63;
    }
    
    return self;
}

// Added conversionRate 
-(float)costInDollars{
    
    
    return self.purchaseSharePrice * self.numberOfShare / self.conversionRate;
}

// Added conversionRate
-(float)valueInDollars{
    
    
    return self.numberOfShare * self.currentSharePrice / self.conversionRate;
    
}

@end

// main

#import "StockHolding.h"
#import "ForeignStockHolding.h"

#import <Foundation/Foundation.h>

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

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

  
    
    NSMutableArray *portfolio = [[NSMutableArray alloc]initWithCapacity:20];
    
    for (int x = 0; x < 3; x++) {
     
         StockHolding *stockholder = [[StockHolding alloc]init];
        [portfolio insertObject:stockholder atIndex:x];
        
    }
   
    
     for (int x = 0; x < 3; x++) {
    
     NSLog(@"                       STOCK HOLDER AT INDEX %i.",x);
     NSLog(@"Original share price     =  $%.0f", [[portfolio objectAtIndex:x]purchaseSharePrice]);
     NSLog(@"Number of shares purchased  %i", [[portfolio objectAtIndex:x]numberOfShare]);
     NSLog(@"Total cost               =  $%.0f", [[portfolio objectAtIndex:x]costInDollars ]);
     NSLog(@"The current share price  =  $%.0f", [[portfolio objectAtIndex:x]currentSharePrice]);
     NSLog(@"Today your shares value is  $%.0f",[[portfolio objectAtIndex:x]valueInDollars]);
    [[portfolio objectAtIndex:x] profitMade];
     NSLog(@" ");
     NSLog(@" ");
    
     }
    
   
    
for (int x = 3; x < 5; x++) {
        
        ForeignStockHolding *foreignStockholder = [[ForeignStockHolding alloc]init];
        [portfolio insertObject:foreignStockholder atIndex:x];
        
      
        
        NSLog(@"                       FOREIGN STOCK HOLDER AT INDEX %i.",x);
        NSLog(@"Original share price     =  £%.0f UK Pounds", [[portfolio objectAtIndex:x]purchaseSharePrice]);
        NSLog(@"Number of shares purchased  %i", [[portfolio objectAtIndex:x]numberOfShare]);
        NSLog(@"The current share price  =  £%.0f", [[portfolio objectAtIndex:x]currentSharePrice]);
        NSLog(@"The conversion rate         %.2f",[foreignStockholder conversionRate]);
        NSLog(@"Total cost in US Dollars =  $%.0f", [[portfolio objectAtIndex:x]costInDollars ]);
        NSLog(@"Today your shares value is  $%.0f",[[portfolio objectAtIndex:x]valueInDollars]);
        [[portfolio objectAtIndex:x] profitMade];
        NSLog(@" ");
        NSLog(@" ");   
       
    }
    [pool drain];
    return 0;
}