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]