My Challenge Solution


#1

Here is my code. I hope you like it. :slight_smile:

ForeignStockHolding.h

//  ForeignStockHolding.h
//  NerdCp18Challenge
//
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}

@property float coversionRate;

-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n andConversionRate: (float)r;

// overriding methods from parent class StockHolding
-(float) costInDollars;
-(float) valueInDollars;

@end

ForeignStockHolding.m

//  ForeignStockHolding.m
//  NerdCp18Challenge
//

#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize coversionRate;

-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n andConversionRate: (float)r
{
    [super setPurchaseSharePrice:p andCurrentSharePrice:c andNumberOfShares:n];
    coversionRate = r;
}

-(float) costInDollars
{
    return [super costInDollars] * coversionRate;
}
-(float) valueInDollars
{
    return [super valueInDollars] * coversionRate;
}
@end

StockHolding.h

//  StockHolding.h
//  NerdCp17Challenge

#import <Foundation/Foundation.h>

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

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


-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n;

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

@end

StockHolding.m

//  StockHolding.m
//  NerdCp17Challenge
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

-(void) setPurchaseSharePrice:(float)p andCurrentSharePrice: (float)c andNumberOfShares: (int)n
{
    purchaseSharePrice = p;
    currentSharePrice = c;
    numberOfShares = n;
}

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

Main File

//  main.m
//  NerdCp18Challenge
//

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

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

    @autoreleasepool {
        
        ForeignStockHolding *fStock1 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding *fStock2 = [[ForeignStockHolding alloc] init];
        ForeignStockHolding *fStock3 = [[ForeignStockHolding alloc] init];
        
        
        [fStock1 setPurchaseSharePrice:2.30 andCurrentSharePrice:4.50 andNumberOfShares:40 andConversionRate:1.3];
        [fStock2 setPurchaseSharePrice:12.19 andCurrentSharePrice:10.56 andNumberOfShares:90 andConversionRate:0.7];
        [fStock3 setPurchaseSharePrice:45.10 andCurrentSharePrice:49.51 andNumberOfShares:210 andConversionRate:0.4];
        
        NSMutableArray *foreignStocks = [NSMutableArray arrayWithObjects:fStock1, fStock2, fStock3, nil];
        
        NSUInteger i = 0;
        for (ForeignStockHolding *fStock in foreignStocks)
            NSLog(@"\nstock[%li]: \nPurchase Share Price: %.2f \nCurrent Share Price: %.2f \nNumber Of Shares: %i \nCost in Dollars: %.2f \nValue in Dollars: %.2f \nConversion Rate: %.2f\n\n\n", i++, [fStock purchaseSharePrice], 
                  [fStock currentSharePrice], [fStock numberOfShares], [fStock costInDollars], [fStock valueInDollars], [fStock coversionRate]);   
        
    }
    return 0;
}

And Output:

2012-08-10 17:04:45.395 NerdCp18Challenge[2736:4503] 
stock[0]: 
Purchase Share Price: 2.30 
Current Share Price: 4.50 
Number Of Shares: 40 
Cost in Dollars: 119.60 
Value in Dollars: 234.00 
Conversion Rate: 1.30


2012-08-10 17:04:45.398 NerdCp18Challenge[2736:4503] 
stock[1]: 
Purchase Share Price: 12.19 
Current Share Price: 10.56 
Number Of Shares: 90 
Cost in Dollars: 767.97 
Value in Dollars: 665.28 
Conversion Rate: 0.70


2012-08-10 17:04:45.399 NerdCp18Challenge[2736:4503] 
stock[2]: 
Purchase Share Price: 45.10 
Current Share Price: 49.51 
Number Of Shares: 210 
Cost in Dollars: 3788.40 
Value in Dollars: 4158.84 
Conversion Rate: 0.40