Challenge Solution: ForeignStockHolding

JSForeignStockHolding.h

JSForeignStockHolding.m

[code]- (float)costDollars
{
return [self purchaseSharePrice] * [self numberShares] * [self conversionRate];
}

  • (float)valueDollars
    {
    return [self currentSharePrice] * [self numberShares] * [self conversionRate];
    }

  • (NSString *)description
    {
    NSString *descriptionMethod = [NSString stringWithFormat:@“Stock: %@\nShare Volume: %d\nPurchase Price: %.2f\nShare Price: %.2f\nConversion Rate: %.2f\nCost: %.2f\nValue: %.2f”, [self companyName], [self numberShares], [self purchaseSharePrice], [self currentSharePrice], [self conversionRate], [self costDollars], [self valueDollars]];
    return descriptionMethod;
    }[/code]

JSStockHolding.h

[code]@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) int numberShares;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) NSString *companyName;

  • (float)costDollars; // purchaseSharePrice * numberShares
  • (float)valueDollars; // currentSharePrice * numberShares[/code]

JSStockHolding.m

[code]- (float)costDollars
{
return [self purchaseSharePrice] * [self numberShares];
}

  • (float)valueDollars
    {
    return [self currentSharePrice] * [self numberShares];
    }

  • (NSString *)description
    {
    NSString *description = [NSString stringWithFormat:@“Stock: %@\nShare Volume: %d\nPurchase Price: %.2f\nShare Price: %.2f\nCost: %.2f\nValue: %.2f”, [self companyName], [self numberShares], [self purchaseSharePrice], [self currentSharePrice], [self costDollars], [self valueDollars]];
    return description;
    }[/code]

Main.m

[code]NSMutableArray *stocks = [[NSMutableArray alloc] init];

JSStockHolding *google = [[JSStockHolding alloc] init];
[google setCompanyName:@“Google”];
[google setPurchaseSharePrice:2.30];
[google setCurrentSharePrice:4.50];
[google setNumberShares:40];
[stocks addObject:google];

JSStockHolding *microsoft = [[JSStockHolding alloc] init];
[microsoft setCompanyName:@“Microsoft”];
[microsoft setPurchaseSharePrice:12.19];
[microsoft setCurrentSharePrice:10.56];
[microsoft setNumberShares:90];
[stocks addObject:microsoft];

JSStockHolding *apple = [[JSStockHolding alloc] init];
[apple setCompanyName:@“Apple”];
[apple setPurchaseSharePrice:45.10];
[apple setCurrentSharePrice:49.51];
[apple setNumberShares:210];
[stocks addObject:apple];

JSForeignStockHolding *volvo = [[JSForeignStockHolding alloc] init];
[volvo setCompanyName:@“Volvo”];
[volvo setPurchaseSharePrice:2.30];
[volvo setCurrentSharePrice:4.50];
[volvo setNumberShares:40];
[volvo setConversionRate:0.94];
[stocks addObject:volvo];

NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
NSString *reportDate = [dateFormatter stringFromDate:currentDate];

NSLog(@“FINANCIAL INVESTMENTS ANALYSIS\n(Generated on %@)\n-----\n”, reportDate);
for (JSForeignStockHolding *stock in stocks) {
NSLog(@"%@\n", stock);
}[/code]

Output

[quote]FINANCIAL INVESTMENTS ANALYSIS
(Generated on Jun 2, 2015)

Stock: Google
Share Volume: 40
Purchase Price: $2.30
Share Price: $4.50
Cost: $92.00
Value: $180.00

Stock: Microsoft
Share Volume: 90
Purchase Price: $12.19
Share Price: $10.56
Cost: $1097.10
Value: $950.40

Stock: Apple
Share Volume: 210
Purchase Price: $45.10
Share Price: $49.51
Cost: $9471.00
Value: $10397.10

Stock: Volvo
Share Volume: 40
Purchase Price: $2.30
Share Price: $4.50
Conversion Rate: 0.94
Cost: $86.48
Value: $169.20[/quote]

Hey, just wondering why in JSForeignStockHolding.m you use

- (float)costDollars
{
    return [self purchaseSharePrice] * [self numberShares] * [self conversionRate];
}

instead of

- (float)costDollars
{
    float localCostInDollars = [super costInDollars];
    return self.conversionRate * localCostInDollars;
}

I am no expert in programming, and perhaps my method is wrong, mind telling me?