Challenge 21.01 from Russia


#1
**BNRStockHolding.h**

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *nameOfCompany;
@property (nonatomic) float purchasedSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic) NSString *ticker;

- (float)costInDollars;  // purchaseSharePrice * numberOfShares
- (float)valueInDollars;    // currentSharePrice * numberfShares

@end

**BNRStockHolding.m**

#import "BNRStockHolding.h"

@implementation BNRStockHolding

- (float)costInDollars
{
    float p = [self purchasedSharePrice];
    int n = [self numberOfShares];
    return p * n;
}

- (float)valueInDollars
{
    float c = [self currentSharePrice];
    int n = [self numberOfShares];
    return c * n;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"You paid %.2f for %d shares in %@ (%@), valued at %.2f", self.costInDollars, self.numberOfShares, self.nameOfCompany, self.ticker, self.valueInDollars];
}

@end

**BNRForeignStockHolding.h**

#import "BNRStockHolding.h"

@interface BNRForeignStockHolding : BNRStockHolding

//  To multiply purchasePrice and currentPrice in local price by to get a price in US dollars
@property (nonatomic) float conversionRate;

@end

**BNRForeignStockHolding.m**

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding


- (float)valueInDollars
{
    float c = [self currentSharePrice]  * self.conversionRate;
    int n = [self numberOfShares];
    return c * n;
}

- (float)costInDollars
{
    float p = [self purchasedSharePrice] * self.conversionRate;
    int n = [self numberOfShares];
    return p * n;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"You paid %.2f.Conv rate %.2f for %d shares in %@ (%@), valued at %.2f", self.costInDollars, self.conversionRate, self.numberOfShares, self.nameOfCompany, self.ticker, self.valueInDollars];
}

@end

**BNRPortfolio.h**

#import "BNRStockHolding.h"

@interface BNRPortfolio : BNRStockHolding
{
    NSMutableArray *_holdings;
}

@property (nonatomic, copy) NSArray *holdings;

- (float)totalValue;
- (float)totalCost;
- (void)addHoldings:(BNRStockHolding *)h;
- (void)addForeignHoldings:(BNRStockHolding *)hF;

@end

**BNRPortfolio.m**

#import "BNRPortfolio.h"

@implementation BNRPortfolio

// Accessors for holdings properties
- (void)setHoldings:(NSArray *)h
{
    _holdings = [h mutableCopy];
}

- (NSArray *)holdings
{
    return [_holdings mutableCopy];
}

- (void)addHoldings:(BNRStockHolding *)h
{
    // Is holdings nil?
    if (!_holdings) {
        
        // Create the array
        _holdings = [[NSMutableArray alloc] init];
    }
    [_holdings addObject:h];
}

- (void)addForeignHoldings:(BNRStockHolding *)hF
{
    // Is holdings nil?
    if (!_holdings) {
        
        // Create the array
        _holdings = [[NSMutableArray alloc] init];
    }
    [_holdings addObject:hF];
}

- (float)totalValue
{
    // Sum up total value of the StockHoldings
    float sum = 0;
    for (BNRStockHolding *h in _holdings) {
        sum += [h valueInDollars];
    }
    return sum;
}

- (float)totalCost
{
    // Sum up total cost of the StockHoldings
    float sum = 0;
    for (BNRStockHolding *h in _holdings) {
        sum += [h costInDollars];
    }
    return sum;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"<Total value is %.2f and total cost is %.2f>",
            self.totalValue, self.totalCost];
}

@end


**main.m**

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        // Create an empty mutable array
        BNRPortfolio *holdings = [[BNRPortfolio alloc] init];
        
        // Generate names for stocks and ticker labels
        NSArray *names = @[@"Aeroflot", @"Alrosa", @"Gazprom"];
        NSArray *ticker = @[@"ARF", @"ALR", @"GZP"];
        
        for (int i = 0; i < [names count]; i++) {
            BNRStockHolding *newStock = [[BNRStockHolding alloc] init];
            
            NSString *stockName = [names objectAtIndex:i];
            newStock.nameOfCompany = stockName;
            newStock.purchasedSharePrice = 2.30 + i * (arc4random() % 3);
            newStock.currentSharePrice = 4.50 + i * (arc4random() % 2);
            newStock.numberOfShares = 15 + i * (arc4random() % 4);
            newStock.ticker = [ticker objectAtIndex:i];
            
            [holdings addHoldings:newStock];
            NSLog(@"%@", newStock);
            
        }
        
        // Generate names for foreign stocks and ticker labels
        NSArray *fNames = @[@"Aeroflot-Foreign", @"Alrosa-Foreign", @"Gazprom-Foreign"];
        NSArray *fTickers = @[@"AERO-F", @"ALROSA-F", @"Gazprom-F"];
        
        for (int i = 0; i < [fNames count]; i++) {
            BNRForeignStockHolding *foreignStock = [[BNRForeignStockHolding alloc] init];
            
            NSString *foreignStockName = [fNames objectAtIndex:i];
            foreignStock.nameOfCompany = foreignStockName;
            foreignStock.purchasedSharePrice = 3.15 + i * (arc4random() % 3);
            foreignStock.currentSharePrice = 5.15 + i * (arc4random() % 2);
            foreignStock.numberOfShares = 10 + i * (arc4random() % 4);
            foreignStock.conversionRate = 0.50 + (0.15 * i);
            foreignStock.ticker = [fTickers objectAtIndex:i];
            
            [holdings addForeignHoldings:foreignStock];
            NSLog(@"%@", foreignStock);
        }

        NSLog(@"%@", holdings);
    }
    return 0;
}

#2

Next time, try posting your code between a pair of three back tick characters to make it look nicer.

Enter this:
```
func fubar () {
// failed unibus address register
}
```

To get this:

func fubar () {
// failed unibus address register
}

#3

Gotcha. Thank you. I didn’t know this.:joy: