Challenge Solution


#1

Hi,

here is my solution, but i think i am missing something, because my conversion rate multiplication is 0.
Here is my code …

//
//  main.m
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "StockHolding.h"
#import "ForeignStockHolding.h"

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

  @autoreleasepool {
    StockHolding *apple   = [[StockHolding alloc] init];
    StockHolding *ibm     = [[StockHolding alloc] init];
    StockHolding *oracle  = [[StockHolding alloc] init];
    
    ForeignStockHolding *foreignStockApple  = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *foreignStockIbm    = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *foreignStockOracle = [[ForeignStockHolding alloc] init];
    
    NSArray *allStocks = [NSArray arrayWithObjects:apple,ibm,oracle,foreignStockApple,foreignStockIbm,foreignStockOracle, nil];
    
    [apple setPurchaseSharePrice:2.30];
    [apple setNumberOfShares:40];
    [apple setCurrentSharePrice:4.50];
    
    [ibm setPurchaseSharePrice:5.30];
    [ibm setNumberOfShares:50];
    [ibm setCurrentSharePrice:5.50];

    [oracle setPurchaseSharePrice:8.30];
    [oracle setNumberOfShares:90];
    [oracle setCurrentSharePrice:7.50];
    
    for (StockHolding *stocks in allStocks) {
      NSLog(@"Value: %2.f€, Cost: %2.f€", [stocks valueInDollars], [stocks costInDollars]);
    }
  }
    return 0;
}
//
//  ForeignStockHolding.h
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
  float conversionRate;
}

@property float conversionRate;

- (float)costInDollars;
- (float)valueInDollars;

@end
//
//  ForeignStockHolding.m
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

- (float)costInDollars
{
  return  ((purchaseSharePrice * numberOfShares) * conversionRate);
}

- (float)valueInDollars
{
  float conversionPrice = [super valueInDollars];
  return conversionPrice * conversionRate;
  
}
@end
//
//  StockHolding.h
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import <Foundation/Foundation.h>

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

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

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

@end
//
//  StockHolding.m
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

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

@end

Regards,
Sebastian


#2

Hi,

i reworked my solution. I think this one is better the the old one …

[code]//
// main.m
// StocksChallenge
//
// Created by Sebastian on 21.10.11.
// Copyright © 2011 My_Company. All rights reserved.
//

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

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

@autoreleasepool {
StockHolding *apple = [[StockHolding alloc] init];
StockHolding *ibm = [[StockHolding alloc] init];
StockHolding *oracle = [[StockHolding alloc] init];

ForeignStockHolding *foreignStockApple  = [[ForeignStockHolding alloc] init];
ForeignStockHolding *foreignStockIbm    = [[ForeignStockHolding alloc] init];
ForeignStockHolding *foreignStockOracle = [[ForeignStockHolding alloc] init];
    
[apple setPurchaseSharePrice:2.30];
[apple setNumberOfShares:40];
[apple setCurrentSharePrice:4.50];

[ibm setPurchaseSharePrice:5.30];
[ibm setNumberOfShares:50];
[ibm setCurrentSharePrice:5.50];

[oracle setPurchaseSharePrice:8.30];
[oracle setNumberOfShares:90];
[oracle setCurrentSharePrice:6.50];

[foreignStockApple setPurchaseSharePrice:12.5];
[foreignStockApple setNumberOfShares:10];
[foreignStockApple setCurrentSharePrice:5];
[foreignStockApple setConversionRate:22.5];

[foreignStockIbm setCurrentSharePrice:10.8];
[foreignStockIbm setNumberOfShares:10];
[foreignStockIbm setCurrentSharePrice:50];
[foreignStockIbm setConversionRate:20];

[foreignStockOracle setCurrentSharePrice:10.8];
[foreignStockOracle setNumberOfShares:10];
[foreignStockOracle setCurrentSharePrice:50];
[foreignStockOracle setConversionRate:220];

NSArray *allStocks = [NSArray arrayWithObjects:apple,ibm,oracle,nil];

NSArray *allForeignStocks = [NSArray arrayWithObjects:foreignStockApple,foreignStockIbm,foreignStockOracle, nil];
    
for (StockHolding *stocks in allStocks) {
  NSLog(@"Value: %2.f€, Cost: %2.f€", [stocks valueInDollars], [stocks costInDollars]);
}

for (ForeignStockHolding *fstocks in allForeignStocks) {
  NSLog(@"Value after Conversion: %2.f€",[fstocks valueInDollars]);
}

}
return 0;
}
[/code]

//
//  ForeignStockHolding.h
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
  float conversionRate;
}

@property float conversionRate;

- (float)costInDollars;
- (float)valueInDollars;

@end
//
//  ForeignStockHolding.m
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//
#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

- (float)costInDollars
{
  return  (purchaseSharePrice * conversionRate);
}

- (float)valueInDollars
{
  return (currentSharePrice * conversionRate);
  
}
@end
//
//  StockHolding.h
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import <Foundation/Foundation.h>

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

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

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

@end
//
//  StockHolding.m
//  StocksChallenge
//
//  Created by Sebastian on 21.10.11.
//  Copyright (c) 2011 _My_Company_. All rights reserved.
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

- (float)valueInDollars
{
  return currentSharePrice * numberOfShares;
  
}
@end

Regards,
Sebastian


#3

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

-(float)costInDollars
{
return (purchaseSharePrice * numberOfShares) * conversionRate;
}

-(float)valueInDollars
{
return (currentSharePrice * numberOfShares) * conversionRate;
}

@end[/code]

You have to get the actual price of the stock before you convert it, I think thats why its messing up for you. The above should work.


#4

Looks good. I took a slightly different approach in ForeignStockHolding.m that seems to work. Interestingly, I see a slight difference in cost in dollars (hundredths of a cent) when using super costInDollars vs calculating from within the function.

[code]//
// ForeignStockHolding.m
// Stocks
//
#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

  • (float) costInDollars
    {
    return conversionRate * [super costInDollars];
    }

  • (float) valueInDollars
    {
    return conversionRate * [super valueInDollars];
    }
    @end[/code]


#5

I don’t see anywhere in your code where you’re defining the conversionRate as 0.94 (figure 18.3 on page 114 identifies the value). That should be why you’re getting a 0 (an initialized conversionRate is 0).

Still working on my solution, but wanted to point that out as I was reading their others ideas.

Thank you,
Brian.


#6

Here is my solution:

Created ForeignStockHolding.h and ForeignStockHolding.m.

ForeignStockHolding.h

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

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}
@property float conversionRate;

@end
[/code]

ForeignStockHolding.m

#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

- (float)costInDollars
{
    return purchaseSharePrice * numberOfShares * conversionRate;
}

- (float)valueInDollars
{
    return currentSharePrice * numberOfShares * conversionRate;
}


@end

main.m

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

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

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

// Create stock pointers
StockHolding *stock1  = [[StockHolding alloc] init];
StockHolding *stock2  = [[StockHolding alloc] init];
StockHolding *stock3  = [[StockHolding alloc] init];
ForeignStockHolding *stock4F = [[ForeignStockHolding alloc] init];

// Create an empty array
NSMutableArray *stocks = [NSMutableArray arrayWithObjects:stock1, stock2, stock3, stock4F, nil];

// Add values to the array
[stock1 setNumberOfShares:40];
[stock1 setPurchaseSharePrice:2.30];
[stock1 setCurrentSharePrice:4.50];

[stock2 setPurchaseSharePrice:12.19];
[stock2 setCurrentSharePrice:10.56];
[stock2 setNumberOfShares:90];

[stock3 setPurchaseSharePrice:45.10];
[stock3 setCurrentSharePrice:49.51];
[stock3 setNumberOfShares:210];

[stock4F setPurchaseSharePrice:2.30];
[stock4F setCurrentSharePrice:4.50];
[stock4F setNumberOfShares:40];
[stock4F setConversionRate:0.94];
    
// Print values in the array
NSLog(@"array = %@", stocks);

[pool drain];
return 0;

}
[/code]

Output:
2011-11-06 16:02:50.936 Stocks[5845:707] array = (
“Stock: cost $92.00 and is now worth $180.00”,
“Stock: cost $1097.10 and is now worth $950.40”,
“Stock: cost $9471.00 and is now worth $10397.10”,
“Stock: cost $86.48 and is now worth $169.20”
)
Program ended with exit code: 0

The numbers calculate correctly, but if there is a more efficient way to do this, I’d love to hear it. One item that comes to mind that seems long toothed, is using “stock#” for each line when inputting values into an array. I feel that I should be able to do something like this (but it blows up):

[stock1 setNumberOfShares:40, setPurchaseSharePrice:2.30, setCurrentSharePrice:4.50];


#7

I created a small function to reduce some code

In StockHolding.m - I added this function

-(void) setAllValues:(int)numShares:(float)purPrice:(float)curPrice
{
    [self setNumberOfShares:numShares];
    [self setPurchaseSharePrice];
    [self setCurrentSharePrice];
     
}

In StockHolding.h - I added this declaration


-(void)setAllValues:(int) numShares:(float) purPrice:(float) curPrice;

Which meant in main.m all I had to do was a one liner to load those figures

In main.m

#import <Foundation/Foundation.h>
#import "ForeignStockHolding.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {
     
        StockHolding *hold1 = [[StockHolding alloc]init];
        StockHolding *hold2 = [[StockHolding alloc]init];
        StockHolding *hold3 = [[StockHolding alloc]init];
        ForeignStockHolding *fHold1 = [[ForeignStockHolding alloc]init];
        ForeignStockHolding *fhold2 = [[ForeignStockHolding alloc]init];
        
        [hold1 setAllValues:250 :1.25 :2.38];
        [hold2 setAllValues:560 :1.05 :1.67];
        [hold3 setAllValues:1000 :1.25 :2.38];
        [fHold1 setAllValues:40 :2.30 :4.50]; 
        [fHold1 setConversionRate:0.94];
        [fhold2 setAllValues:250:1.25 :2.38]; 
        [fhold2 setConversionRate:0.94];
        
  NSMutableArray *sh = [NSMutableArray arrayWithObjects:hold1,hold2,hold3,fHold1,fhold2, nil];

        for (StockHolding *s in sh){
            NSLog(@"This Shareholding has $%.2f Cost and is worth $%.2f",[s costInDollars],[s valueInDollars]);
        }  
    }      
        return 0;
}

But to me, I am sure we could get it a bit more efficient yet!

Regards
Toddy


#8

Hey Toddy,

All you need to do is add a [super init] to your method and you will have a custom initializer. Then you can do something like

and it ends up saving a ton of typing. AAron gets to them later in the book, but you are on the right track.


#9

My solution is pretty much what everyone else came up with. Inspired by Toddy though I went ahead and made a new init method.

In StockHoldings.h I defined this method:

Then constructed it as such in StockHoldings.m:

[code]// Custom init
-(id)initWithValues:(float)pp:(float)cp:(int)ns
{
self = [super init];

// Did we get something back?
if (self) {
    [self setPurchaseSharePrice:pp];
    [self setCurrentSharePrice:cp];
    [self setNumberOfShares:ns];
}

return self;

}[/code]

Now when I call it all I need to do is define everything on one line in main:

// Create our US StockHoldings objects StockHoldings *BNR = [[StockHoldings alloc] initWithValues:10.0 :123.45 :1321];

This makes main much, much shorter and easier to read!


#10

Please give all your arguments labels in the selector. Unlabeled args are frowned on by the community:


#11

[quote=“AaronHillegass”]Please give all your arguments labels in the selector. Unlabeled args are frowned on by the community:

Note taken and will do!


#12

Ok, I’ve read most of the previous posts, but I’m still missing the fact that the foreign stock will be purchased in a foreign currency. So I added new purchase price and current price in Euros to the ForeignStockHolding.
I haven’t changed the StockHolding (.m/.h) from the previous challenge.
It took a while to get my head around the inheritance of stuff from other classes (I can use the number of shares from StockHolding within my ForeignStockHolding and the costInDollar and valueInDollar from there), but I think I’ve got it.

So here’s what I have:

//  ForeignStockHolding.h
//  Challenge_Stocks

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
    float purchaseSharePriceInEuro;
    float currentSharePriceInEuro;
}
@property float conversionRate;
@property float purchaseSharePriceInEuro;
@property float currentSharePriceInEuro;

@end
//  ForeignStockHolding.m
//  Challenge_Stocks

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

@implementation ForeignStockHolding
@synthesize conversionRate;
@synthesize purchaseSharePriceInEuro;
@synthesize currentSharePriceInEuro;

- (float) costInDollars
{
    float costInEuro = purchaseSharePriceInEuro * numberOfShares;
    return costInEuro * conversionRate;
}
- (float) valueInDollars
{
    float valueInEuro = currentSharePriceInEuro * numberOfShares;
    return valueInEuro * conversionRate;
}

@end

[code]//
// main.m
// Challenge_Stocks

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

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

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

//Create instances of StockHolding
StockHolding *sH0 = [[StockHolding alloc]init];
StockHolding *sH1 = [[StockHolding alloc]init];
StockHolding *sH2 = [[StockHolding alloc]init];

//Create instances of foreign Stocks (ok, just one for saving on typing)
ForeignStockHolding *fSH0 = [[ForeignStockHolding alloc]init];
        
//Give the instance variables values
[sH0 setPurchaseSharePrice:2.3];
[sH0 setCurrentSharePrice:4.5];
[sH0 setNumberOfShares:40];

[sH1 setPurchaseSharePrice:12.19];
[sH1 setCurrentSharePrice:10.56];
[sH1 setNumberOfShares:90];

[sH2 setPurchaseSharePrice:45.1];
[sH2 setCurrentSharePrice:49.51];
[sH2 setNumberOfShares:210];

//Give the foreign stock instance variables values
[fSH0 setPurchaseSharePriceInEuro:2];
[fSH0 setCurrentSharePriceInEuro:4];
[fSH0 setNumberOfShares:4];
[fSH0 setConversionRate:10];

//Create a mutable array including the stockholding instances (arrayWithObjects replaces the single lines addObject in my earlier version.
NSMutableArray *stocksArray = [NSMutableArray arrayWithObjects:sH0, sH1, sH2, fSH0, nil];

int i;
for (i=0; i<[stocksArray count]; i++) {
    NSLog(@"cost %.2f, value %.2f", [[stocksArray objectAtIndex:i] costInDollars], [[stocksArray objectAtIndex:i] valueInDollars]);
}

[pool drain];
return 0;

}[/code]

I still love to learn new stuff, even if I’ve been out of school for at least 10 years now.
Great community. Thanks for all the input.


#13

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding
@synthesize conversionRate;

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

}

-(float) valueInDollars
{
return ([self conversionRate] * (currentSharePrice * numberOfShares));

}
@end[/code]

The rest of the code is same as the previous challenge. I’m just showing the overridden methods


#14

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

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

@autoreleasepool {
    
    //create three instances of StockHolding
    StockHolding *stock1 = [[StockHolding alloc] init];
    StockHolding *stock2 = [[StockHolding alloc] init];
    StockHolding *stock3 = [[StockHolding alloc] init];
    
    
    //Set instance variables for each
    [stock1 setNumberOfShares:10];
    [stock1 setPurchaseSharePrice:100.00];
    [stock1 setCurrentSharePrice:101.00];
    [stock2 setNumberOfShares:20];
    [stock2 setPurchaseSharePrice:200.00];
    [stock2 setCurrentSharePrice:100.00];
    [stock3 setNumberOfShares:30];
    [stock3 setPurchaseSharePrice:300.00];
    [stock3 setCurrentSharePrice:1000.00];
    

    //Add each object to the array
    NSMutableArray *myStocks = [[NSMutableArray alloc] init];
    [myStocks addObject:stock1];
    [myStocks addObject:stock2];
    [myStocks addObject:stock3];
    
    //create three instances of subclass ForeignStoxkHolding
    ForeignStockHolding *fstock1 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *fstock2 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *fstock3 = [[ForeignStockHolding alloc] init];
    
    //Set instance variables for each
    [fstock1 setNumberOfShares:10];
    [fstock1 setPurchaseSharePrice:100.00];
    [fstock1 setCurrentSharePrice:101.00];
    [fstock1 setConversionRate:2.00];
    [fstock2 setNumberOfShares:20];
    [fstock2 setPurchaseSharePrice:200.00];
    [fstock2 setCurrentSharePrice:100.00];
    [fstock2 setConversionRate:.5];
    [fstock3 setNumberOfShares:30];
    [fstock3 setPurchaseSharePrice:300.00];
    [fstock3 setCurrentSharePrice:1000.00];
    [fstock3 setConversionRate:5.00];
    
    //Add each object to the array
    [myStocks addObject:fstock1];
    [myStocks addObject:fstock2];
    [myStocks addObject:fstock3];
    
    float totalValue = 0;
    //Iterate through the array and printout data
    for (StockHolding *thisStock in myStocks)
    {
        totalValue = (totalValue + [thisStock valueInDollars]);
       NSLog(@"This stock has %d shares purchased for $%.2f each at a cost of $%.2f", [thisStock numberOfShares], [thisStock purchaseSharePrice], [thisStock costInDollars]);
       NSLog(@"It's current share price is $%.2f and is currently worth $%.2f.", [thisStock currentSharePrice], [thisStock valueInDollars]);
    
    }
    
      NSLog(@"The current value of my holdings is %.2f.", totalValue);
    
    
    
    
}
return 0;

}
[/code]

and the results:

2011-12-07 09:06:05.978 Stocks[2354:707] This stock has 10 shares purchased for $100.00 each at a cost of $1000.00
2011-12-07 09:06:05.979 Stocks[2354:707] It’s current share price is $101.00 and is currently worth $1010.00.
2011-12-07 09:06:05.980 Stocks[2354:707] This stock has 20 shares purchased for $200.00 each at a cost of $4000.00
2011-12-07 09:06:05.980 Stocks[2354:707] It’s current share price is $100.00 and is currently worth $2000.00.
2011-12-07 09:06:05.980 Stocks[2354:707] This stock has 30 shares purchased for $300.00 each at a cost of $9000.00
2011-12-07 09:06:05.980 Stocks[2354:707] It’s current share price is $1000.00 and is currently worth $30000.00.
2011-12-07 09:06:05.981 Stocks[2354:707] This stock has 10 shares purchased for $100.00 each at a cost of $2000.00
2011-12-07 09:06:05.981 Stocks[2354:707] It’s current share price is $101.00 and is currently worth $2020.00.
2011-12-07 09:06:05.982 Stocks[2354:707] This stock has 20 shares purchased for $200.00 each at a cost of $2000.00
2011-12-07 09:06:05.982 Stocks[2354:707] It’s current share price is $100.00 and is currently worth $1000.00.
2011-12-07 09:06:05.982 Stocks[2354:707] This stock has 30 shares purchased for $300.00 each at a cost of $45000.00
2011-12-07 09:06:05.983 Stocks[2354:707] It’s current share price is $1000.00 and is currently worth $150000.00.
2011-12-07 09:06:05.983 Stocks[2354:707] The current value of my holdings is 186030.00.
Program ended with exit code: 0


#15

my solution - I hope I got it right.

Foreign Stock header

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}
@property float conversionRate;

@end

Foreign Stock implementation

#import "ForeignStockHolding.h"


@implementation ForeignStockHolding

@synthesize conversionRate;

-(float) costInDollars
{
    float costBeforeConveration = [super costInDollars];
    return costBeforeConveration * conversionRate;
}

-(float) valueInDollars
{
    float valueBeforeConveration = [super valueInDollars];
    return valueBeforeConveration * conversionRate;
}


@end

#16

I get the basic concept here with adding the foreign stockholdings to the mix, but I am a little confused on the implementation. Here is what I have on the code so far:

//  ForeignStockHolding.h
//  Challenge_Stocks

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}
@property float conversionRate;

@end
//  ForeignStockHolding.m
//  Challenge_Stocks

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

@implementation ForeignStockHolding

@synthesize conversionRate;

-(float)costInDollars
{
    purchaseSharePrice * conversionRate;
}

-(float)valueInDollars
{
    currentSharePrice * conversionRate;
}

@end

[code]
// main.m
// Stocks

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

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

@autoreleasepool {
    
    // Create a mutable array
    NSMutableArray *portfolio = [NSMutableArray array];
    
    // Create our Stockholdings objects
    StockHolding *apple = [[StockHolding alloc] init];
    StockHolding *nikon = [[StockHolding alloc] init];
    StockHolding *keen  = [[StockHolding alloc] init];
    ForeignStockHolding *foreign = [[ForeignStockHolding alloc] init];
    
    // Populate the instance variables in each stock
    [apple setPurchaseSharePrice:250.0];
    [apple setCurrentSharePrice:365.43];
    [apple setNumberOfShares:15435];
    
    [nikon setPurchaseSharePrice:49.0];
    [nikon setCurrentSharePrice:125.43];
    [nikon setNumberOfShares:9876];
    
    [keen setPurchaseSharePrice:20.4];
    [keen setCurrentSharePrice:69.43];
    [keen setNumberOfShares:487];
    
    [foreign setPurchaseSharePrice:950000.0];
    [foreign setNumberOfShares:1000];
    [foreign setConversionRate:6.36810];
    [foreign setCurrentSharePrice:1250000.0];
    
    // Add the shares to the array
    [portfolio addObject:apple];
    [portfolio addObject:nikon];
    [portfolio addObject:keen];
    [portfolio addObject:foreign];
    
    // interate the array and print the valueInDollars of each object
    for (StockHolding *sh in portfolio) {
        NSLog(@"Stock cost $%.2f and is currently worth $%.2f. Foreign stock holdings has a current value of ¥%.2d", [sh costInDollars], [sh valueInDollars], [foreign conversionRate]);
        }
}
return 0;

}[/code]

I know this last part isn’t right because Xcode says so, plus I am not sure how to include the foreign part in the final statement. I could use a little assistance on bringing this together. Thanks.


#17

Take a look at your NSLog statement. You are printing the value of conversionRate rather than calculating the value. Change that call your valueInDollars method and you should be good.

So change [foreign conversionRate]

To [foreign valueInDollars]

And you should be set.


#18

Thanks for the help.


#19

I have my solution with ForeignStockHolding class. If conversionRate is not defined >> assign value 1.0 in method

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding {
    float conversionRate;
}

@property float conversionRate;

@end

and .m file:

#import "ForeignStockHolding.h"

@implementation ForeignStockHolding
@synthesize conversionRate;

-(float) costInDollars {
    float cost = [super costInDollars];
    if (!conversionRate) {
        conversionRate = 1.0;
    } 
    return cost * conversionRate;
}

-(float) valueInDollars {
    float value = [super valueInDollars];
    if (!conversionRate) {
        conversionRate = 1.0;
    } 
    return value * conversionRate; 
}
@end

Main.m file

#import <Foundation/Foundation.h>
#import "ForeignStockHolding.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        // I use ForeignStockHolding for all instances
        ForeignStockHolding *stock1 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding *stock2 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding *stock3 = [[ForeignStockHolding alloc] init];
        
        [stock1 setPurchaseSharePrice:2.30];
        [stock1 setCurrentSharePrice:4.50];
        [stock1 setNumberOfShares:40];

        [stock2 setPurchaseSharePrice:12.19];
        [stock2 setCurrentSharePrice:10.56];
        [stock2 setNumberOfShares:90];
        //I specified conversionRate only for this:
        [stock3 setPurchaseSharePrice:45.10];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberOfShares:210];
        [stock3 setConversionRate:0.9];

        //Create an empty mutable array
        NSMutableArray *stockArray = [NSMutableArray array];
        //Add objects to array;
        [stockArray addObject:stock1];
        [stockArray addObject:stock2];
        [stockArray addObject:stock3];
        //Show data
        for (ForeignStockHolding *elem in stockArray) {
            NSLog(@"Object at index  %lu \n", [stockArray indexOfObject:elem]);
            NSLog(@"PurchaseSharePrice %.2f \n", [elem purchaseSharePrice]);
            NSLog(@"CurrentSharePrice %.2f \n", [elem currentSharePrice]);
            NSLog(@"NumberOfSharePrice %d \n", [elem numberOfShares]);
            NSLog(@"costIndollars = %.2f \n", [elem costInDollars]);
            NSLog(@"valueInDollars = %.2f \n ", [elem valueInDollars]);           
        }

    }
    return 0;
}

#20

IJWTS, if you ever get a Build Failed with error messages beyond your pay grade (e.g. semantic issue in stddef.h where the object you called isn’t a function or a function pointer), then save yourself an hour and first look for a missing comma, e.g. when you’re initializing your array. The compiler REALLY wants that list to end with ‘nil’, not an object…