Challenge 18 Shortest Solution


#1

[code]//
// main.m
// Challenge 18 inheritance
//
// Created by Bernardo Mondragón Brozon on 07/08/13.
// Copyright © 2013 Bernardo Mondragón Brozon. All rights reserved.
//

#import <Foundation/Foundation.h>

//StockHolding.h
@interface StockHolding : NSObject
@property float purchaseSharePrice, currentSharePrice;
@property int numberOfShares;
-(float)costInDollars;
-(float)valueInDollars;
@end

//StockHolding.m
@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;
-(float)costInDollars { return self.numberOfSharesself.purchaseSharePrice; }
-(float)valueInDollars { return self.numberOfShares
self.currentSharePrice; }
@end

//ForeignStockHolding.h
@interface ForeignStockHolding : StockHolding
@property float conversionRate;
-(float)FcostInDollars;
-(float)FvalueInDollars;
@end

//ForeignStockHolding.m
@implementation ForeignStockHolding
-(float)FcostInDollars { return super.costInDollarsself.conversionRate; }
-(float)FvalueInDollars { return super.valueInDollars
self.conversionRate; }
@end

//main
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *array = [NSMutableArray new];
for (int i =0; i<10; i++) {
ForeignStockHolding *fstock1 = [ForeignStockHolding new];
fstock1.purchaseSharePrice = random()%17;
fstock1.currentSharePrice = random()%17;
fstock1.numberOfShares = random()%17;
fstock1.conversionRate = .5;
[array addObject:fstock1];
}
int i = 1;
for (ForeignStockHolding *fs in array) {
NSLog(@“Stock %i local currency: Cost: %.2f Value: %.2f”, i, fs.costInDollars, fs.valueInDollars);
NSLog(@“Stock %i foreign currency: Cost: %.2f Value: %.2f”, i, fs.FcostInDollars, fs.FvalueInDollars);
i++;
}
}
return 0;
}[/code]

Conversion rate = .5

This is the output:

2013-08-08 00:50:39.827 Challenge 18 inheritance[1122:303] Stock 1 local currency: Cost: 90.00 Value: 135.00
2013-08-08 00:50:39.829 Challenge 18 inheritance[1122:303] Stock 1 foreign currency: Cost: 45.00 Value: 67.50
2013-08-08 00:50:39.830 Challenge 18 inheritance[1122:303] Stock 2 local currency: Cost: 0.00 Value: 45.00
2013-08-08 00:50:39.830 Challenge 18 inheritance[1122:303] Stock 2 foreign currency: Cost: 0.00 Value: 22.50
2013-08-08 00:50:39.831 Challenge 18 inheritance[1122:303] Stock 3 local currency: Cost: 33.00 Value: 154.00
2013-08-08 00:50:39.832 Challenge 18 inheritance[1122:303] Stock 3 foreign currency: Cost: 16.50 Value: 77.00
2013-08-08 00:50:39.833 Challenge 18 inheritance[1122:303] Stock 4 local currency: Cost: 28.00 Value: 70.00
2013-08-08 00:50:39.833 Challenge 18 inheritance[1122:303] Stock 4 foreign currency: Cost: 14.00 Value: 35.00
2013-08-08 00:50:39.834 Challenge 18 inheritance[1122:303] Stock 5 local currency: Cost: 36.00 Value: 24.00
2013-08-08 00:50:39.835 Challenge 18 inheritance[1122:303] Stock 5 foreign currency: Cost: 18.00 Value: 12.00
2013-08-08 00:50:39.835 Challenge 18 inheritance[1122:303] Stock 6 local currency: Cost: 65.00 Value: 143.00
2013-08-08 00:50:39.840 Challenge 18 inheritance[1122:303] Stock 6 foreign currency: Cost: 32.50 Value: 71.50
2013-08-08 00:50:39.841 Challenge 18 inheritance[1122:303] Stock 7 local currency: Cost: 6.00 Value: 14.00
2013-08-08 00:50:39.842 Challenge 18 inheritance[1122:303] Stock 7 foreign currency: Cost: 3.00 Value: 7.00
2013-08-08 00:50:39.843 Challenge 18 inheritance[1122:303] Stock 8 local currency: Cost: 40.00 Value: 10.00
2013-08-08 00:50:39.844 Challenge 18 inheritance[1122:303] Stock 8 foreign currency: Cost: 20.00 Value: 5.00
2013-08-08 00:50:39.845 Challenge 18 inheritance[1122:303] Stock 9 local currency: Cost: 112.00 Value: 40.00
2013-08-08 00:50:39.846 Challenge 18 inheritance[1122:303] Stock 9 foreign currency: Cost: 56.00 Value: 20.00
2013-08-08 00:50:39.849 Challenge 18 inheritance[1122:303] Stock 10 local currency: Cost: 44.00 Value: 154.00
2013-08-08 00:50:39.850 Challenge 18 inheritance[1122:303] Stock 10 foreign currency: Cost: 22.00 Value: 77.00