conversionRate challenge


#1

Hello i have finally after few days working on it come up with a solution for this challenge and i just wanted to ask if this solution is acceptable and if it was meant to be solved this way?
What i have done is print out 3 stocks in local currency and then convert them to USD in the next 3 stocks holding the same values in their ivar’s as the first 3 stocks. And in the loop i just added some if statement so compiler knows when to print out the right stocks.

BNRStockHolding.h

[code]#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject {
float _purchaseSharePrice;
float _currentSharePrice;
int numberOfShares;
char *stockAbbreviation;
NSString *currency;
}

// Here’s the method i use for calculation
-(float) costInDollars;
-(float) valueInDollars;
-(float) earningsToDate;

// Sets the amount of numberOfShares to the instance variable nunberOfShares.
-(float) purchaseSharePrice; // Calculates the amount of shares paid for and amount of money.
-(void) setPurchaseSharePrice:(float) setpsp; // Sets the purchaseSharePrice to the value given by user.
-(float) currentSharePrice; // Calculates the amount of money paid and number of shares owned.
-(void) setCurrentSharePrice:(float) setcsp; // Sets the currentSharePrice to the value given by user.
-(int) numberOfShares; // Calculates the number of shares and returns it.
-(void) setNumberOfShares:(int) nos; // Sets numberOfShares to the value given by user.
-(char *) stockAbbreviation; // Returns the stock abbreviation to stockAbbreviation.
-(void) setStockAbbreviation:(char *) sa; // Gets a stock abbreviation from user sets it equal to sa.
-(void) setCurrency: (NSString *) currencyUsed;
-(NSString *) currency;
[/code]

BNRStockHolding.m

[code]@implementation BNRStockHolding

// Returns the value of the stock at the time i bought it.
-(float) costInDollars{
return _purchaseSharePrice * numberOfShares;
}

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

-(char *) stockAbbreviation{
return stockAbbreviation;
}

-(void) setStockAbbreviation: (char *) sa{
stockAbbreviation = sa;
}

// Earnings…
-(float) earningsToDate{
if(_purchaseSharePrice > _currentSharePrice) {
return (_purchaseSharePrice - _currentSharePrice) * numberOfShares;
}
else {
return (_currentSharePrice - _purchaseSharePrice) * numberOfShares;
}
}

// returns the number of shares bought.
-(int) numberOfShares{
return numberOfShares;
}

-(void) setNumberOfShares:(int) setnos {
numberOfShares = setnos;
}

-(float) purchaseSharePrice {
return _purchaseSharePrice;
}

-(void) setPurchaseSharePrice:(float) setpsp {
_purchaseSharePrice = setpsp;
}

-(float) currentSharePrice{
return (_currentSharePrice - _purchaseSharePrice) * numberOfShares;
}

-(void) setCurrentSharePrice:(float) setcsp {
_currentSharePrice = setcsp;
}
-(void) setCurrency: (NSString *) currencyUsed{
currency = currencyUsed;
}
-(NSString *) currency{
return currency;
}

@end[/code]

ForeignStockHolding.h

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

@interface ForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end
[/code]

ForeignStockHolding.m

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

-(float) costInDollars{
return (_purchaseSharePrice * numberOfShares) * _conversionRate;
}

-(float) valueInDollars{
return (_currentSharePrice * numberOfShares) * _conversionRate;
}

/*
[stock1 setNumberOfShares:117];
[stock1 setPurchaseSharePrice:683.21];
[stock1 setCurrentSharePrice:701.66];
[stock1 setStockAbbreviation:“MCSFT”];
[stock1 setCurrency:@“Euro”];
*/
@end[/code]

Main

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

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

@autoreleasepool {
    // Create 3 instances of the class BNRStockHolding.
    BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
    BNRStockHolding *stock2 = [[BNRStockHolding alloc]init];
    BNRStockHolding *stock3 = [[BNRStockHolding alloc]init];
    
    [stock1 setNumberOfShares:117];
    [stock1 setPurchaseSharePrice:683.21];
    [stock1 setCurrentSharePrice:701.66];
    [stock1 setStockAbbreviation:"MCSFT"];
    [stock1 setCurrency:@"Euro"];
    
    [stock2 setNumberOfShares:95];
    [stock2 setPurchaseSharePrice:87.08];
    [stock2 setCurrentSharePrice:81.89];
    [stock2 setStockAbbreviation:"APL"];
    [stock2 setCurrency:@"DKK"];
    
    [stock3 setNumberOfShares:203];
    [stock3 setPurchaseSharePrice:455.02];
    [stock3 setCurrentSharePrice:491.47];
    [stock3 setStockAbbreviation:"FB"];
    [stock3 setCurrency:@"AUD"];
    
    ForeignStockHolding *foreign1 = [[ForeignStockHolding alloc]init];
    ForeignStockHolding *foreign2 = [[ForeignStockHolding alloc]init];
    ForeignStockHolding *foreign3 = [[ForeignStockHolding alloc]init];
    
    [foreign1 setNumberOfShares:117];
    [foreign1 setPurchaseSharePrice:683.21];
    [foreign1 setCurrentSharePrice:701.66];
    [foreign1 setStockAbbreviation:"MCSFT"];
    [foreign1 setCurrency:@"EURO"];
    
    [foreign2 setNumberOfShares:95];
    [foreign2 setPurchaseSharePrice:87.08];
    [foreign2 setCurrentSharePrice:81.89];
    [foreign2 setStockAbbreviation:"APL"];
    [foreign2 setCurrency:@"DKK"];
    
    [foreign3 setNumberOfShares:203];
    [foreign3 setPurchaseSharePrice:455.02];
    [foreign3 setCurrentSharePrice:491.47];
    [foreign3 setStockAbbreviation:"FB"];
    [foreign3 setCurrency:@"AUD"];
    
    
    
    foreign1.conversionRate = 0.8047; // Times this with local currency and you get dollars (Euros)
    foreign2.conversionRate = 0.1670; // Convert DKK To USD
    foreign3.conversionRate = 0.8614; // Convert AUD to USD.
    foreign3.conversionRate = 0.1628; // Convert from CNY(chinese yuan) to USD
    
    NSMutableArray *stockArray = [NSMutableArray array];
    [stockArray addObject:stock1];
    [stockArray addObject:stock2];
    [stockArray addObject:stock3];
    [stockArray addObject];
    [stockArray addObject];
    [stockArray addObject];


    
    

    NSLog(@"Everything converted to USD now");
    for(ForeignStockHolding *s in stockArray) {
        if(s <= stockArray[3]){
        NSLog(@"Stock: %s - Cost: %.2f - Value: %.2f - Shares: %i \n", [s stockAbbreviation],[s costInDollars],[s valueInDollars],[s numberOfShares]);
        }
    if(s >= stockArray[3]){
        NSLog(@"Currency converted: %@ - Stock: %s - Cost in USD: %.2f - Value in USD: %.2f - Shares: %i \n", [s currency], [s stockAbbreviation],[s costInDollars],[s valueInDollars],[s numberOfShares]);
        }
        if(s > stockArray[5]) {
            break;
        }
    }
           
}
return 0;

}[/code]