My solution to the challenge


#1

My first post in this forum. Loving the book and really learning a lot every time I read it.

Can anyone critique my take on this challenge? Most interesting bit is in main.m, where I decided to keep the dollar values as local rates, have the third item in the array as the foreign stock, put in the values as per the book and converted it to British Pounds.

Also, an if statement to complement the foreign stock item in the array.

BNRForeignStockHolding.h

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

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end
[/code]

BNRForeignStockHolding.m

[code]#import “BNRForeignStockHolding.h”

@implementation BNRForeignStockHolding

  • (float)costInDollars
    {
    float cost = [self numberOfShares];
    return ([self purchaseSharePrice] * cost) / _conversionRate;
    }

  • (float)valueInDollars
    {
    float value = [self numberOfShares];
    return ([self currentSharePrice] * value) / _conversionRate;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"[FOREIGN STOCK]"];
    }

@end
[/code]

main.m

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

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

    // Fill array with three instances of BNRStockHolding
    // Iterate through array printing out the value in each
    
    BNRStockHolding *first = [[BNRStockHolding alloc]init];
    BNRStockHolding *second = [[BNRStockHolding alloc]init];
    BNRForeignStockHolding *third = [[BNRForeignStockHolding alloc]init];
    
    first.purchaseSharePrice = 2.30;
    first.currentSharePrice = 4.50;
    first.numberOfShares = 40;
    
    second.purchaseSharePrice = 12.19;
    second.currentSharePrice = 10.56;
    second.numberOfShares = 90;
    
    third.purchaseSharePrice = 45.10;
    third.currentSharePrice = 49.51;
    third.numberOfShares = 210;
    third.conversionRate = 1.62;
    
    
    NSMutableArray *stocks = [[NSMutableArray alloc ]initWithObjects:first, second, third, nil];
    
    int stockNumber = 1;
    
    for (BNRStockHolding *b in stocks)
    {
        if (stockNumber == 3) {
            NSLog(@"Cost of stock %i is £%.2f(Cost) and £%.2f(Value)", stockNumber, b.costInDollars, b.valueInDollars);
            NSLog(@"Stock number 3 is %@", b);
            NSLog(@"Stock 3 pre-conversion value is $%.2f (cost) and $%.2f (value)", b.purchaseSharePrice, b.currentSharePrice);
        } else{
            NSLog(@"Cost of stock %i is $%.2f(Cost) and $%.2f(Value)", stockNumber, b.costInDollars, b.valueInDollars);
        }
        stockNumber++;
    }
}
return 0;

}
[/code]


#2

Additionally,

BNRStockHolding.h

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

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

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

@end
[/code]

BNRStockHolding.m

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float) costInDollars;
    {
    float cost = [self numberOfShares];
    return [self purchaseSharePrice] * cost;
    }

  • (float) valueInDollars;
    {
    float value = [self numberOfShares];
    return [self currentSharePrice] * value;
    }

@end
[/code]


#3

Output:

Cost of stock 1 is $92.00(Cost) and $180.00(Value)
Cost of stock 2 is $1097.10(Cost) and $950.40(Value)
Cost of stock 3 is £5846.30(Cost) and £6417.96(Value)
Stock number 3 is [FOREIGN STOCK]
Stock 3 pre-conversion value is $45.10 (cost) and $49.51 (value)
Program ended with exit code: 0

Trying to figure out how to modify the if-statement to properly output the extra descriptions if a stock is foreign besides telling it to output it for Stock #3.

Help and comments will be appreciated :slight_smile: