My recent Challenge Solution


#1

Main.m

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


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        BNRStockHolding *stockTXRH = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockGSAT = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockAAPL = [[BNRStockHolding alloc]init];

        // Made this stock foreign, although it is not really
        BNRForeignStockHolding *stockZNGA = [[BNRForeignStockHolding alloc]init];
        NSMutableArray *stockArray = [[NSMutableArray alloc]init];
         
 /*******************************************************************/
 
        stockTXRH.companyName = (@"Texas Roadhouse");
        stockTXRH.stockSymbol = "TXRH";
        stockTXRH.purchaseSharePrice = 26.62;
        stockTXRH.currentSharePrice = 28.31;
        stockTXRH.numberOfShares = 2556;
        [stockArray addObject:stockTXRH];
        
/*******************************************************************/
        
        stockGSAT.companyName = (@"GlobalStar");
        stockGSAT.stockSymbol = "GSAT";
        stockGSAT.purchaseSharePrice = 2.30;
        stockGSAT.currentSharePrice = 4.50;
        stockGSAT.numberOfShares = 40;
        [stockArray addObject:stockGSAT];
        
        
/*******************************************************************/
        
        stockAAPL.companyName = (@"Apple");
        stockAAPL.stockSymbol = "AAPL";
        stockAAPL.purchaseSharePrice = 10.50;
        stockAAPL.currentSharePrice = 99.62;
        stockAAPL.numberOfShares = 500;
        [stockArray addObject:stockAAPL];
        
/*******************************************************************/
        
        // Made this stock foreign, although it is not really
        
        stockZNGA.companyName = (@"Zynga");
        stockZNGA.stockSymbol = "ZNGA";
        stockZNGA.purchaseSharePrice = 2.30;
        stockZNGA.currentSharePrice = 4.50;
        stockZNGA.numberOfShares = 40;
        stockZNGA.conversionRate = 0.94;
        [stockArray addObject:stockZNGA];
        
    for (BNRStockHolding *s in stockArray)
    {
        // Looks at a custom overridden version of the description methods
        // Both BNRStockHolding and BNRForeignStockholding have their own description method

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

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *companyName;
@property (nonatomic) char *stockSymbol;
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int   numberOfShares;


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

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding

              
- (float) costInDollars
{
 
    return self.purchaseSharePrice * self.numberOfShares;

}
              
- (float) valueInDollars
{
    return self.currentSharePrice * self.numberOfShares;
}

- (NSString *)description
{

    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %s\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n",
            self.companyName, self.stockSymbol, self.numberOfShares,
            self.purchaseSharePrice, self.currentSharePrice,
            [self costInDollars], [self valueInDollars]];
    
}

@end

BNRForeignStockHolding.h

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

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end

BNRForeignStockHolding.m

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding


- (float) costInDollars
{
    return (self.purchaseSharePrice * self.conversionRate) * self.numberOfShares;
}

- (float) valueInDollars
{
    return (self.currentSharePrice * self.conversionRate) * self.numberOfShares;
}

// Description is overriden

- (NSString *)description
{

// PurchaseSharePrice and CurrentSharePrice are converted using the conversion rate.  
// CostInDollars and ValueInDollars uses the conversion rate as well.
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %s\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n     Conversion Rate: %.2f\n",
          self.companyName, self.stockSymbol, self.numberOfShares,
          (self.purchaseSharePrice * self.conversionRate) , (self.currentSharePrice * self.conversionRate),
          [self costInDollars], [self valueInDollars], self.conversionRate];
    
}

@end