Challenge: Foreign Stock Conversion not working


#1

[code]//
// ForeignStockHolding.m
// Stocks
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

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

-(float)currentSharePrice
{
float convertedCurrentSharePrice = [super currentSharePrice] * conversionRate;
return convertedCurrentSharePrice;
}

@end
[/code]

[code]//
// ForeignStockHolding.h
// Stocks
//

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

@interface ForeignStockHolding : StockHolding
{
int conversionRate;
}

@property int conversionRate;

@end
[/code]

[code]//
// ForeignStockHolding.m
// Stocks
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

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

-(float)currentSharePrice
{
float convertedCurrentSharePrice = [super currentSharePrice] * conversionRate;
return convertedCurrentSharePrice;
}

@end
[/code]

[code]//
// StockHolding.h
// Stocks
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

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

//Accessor Methods

-(void)setPurchaseSharePrice:(float)purchaseSharePrice;
-(void)setCurrentSharePrice:(float)currentSharePrice;

//The other two instance methods
-(float)costInDollars; // purchaseSharePrice * numberOfShares
-(float)valueInDollars; //currentSharePrice * numberOfShares
-(float)profitInDollars;

@end
[/code]

[code]//
// StockHolding.m
// Stocks
//

#import “StockHolding.h”

@implementation StockHolding

@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

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

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

-(float)profitInDollars
{
return [self valueInDollars] - [self costInDollars];
}

@end
[/code]

Output:

Brought stock for $2.30, It is now at $4.50, I have 40 shares, They cost me $92.00, Now they are worth $180.00 Brought stock for $2.30, It is now at $4.50, I have 40 shares, They cost me $92.00, Now they are worth $180.00 Brought stock for $2.30, It is now at $0.00, I have 40 shares, They cost me $0.00, Now they are worth $0.00

The third stock is meant to be foreign, but for some reason, it displays some parameters as 0


#2

hello.

ahye, you didn’t post the main.m code but the ForeignStockHolding.m two times!
but probably you didn’t set your conversionRate properly and you’d mulitplied the values with zero, hadn’t you?