My current challenge 22 Solution


#1

Main.m

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


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        BNRPortfolio *myStockPortfolio = [[BNRPortfolio alloc]init];
        BNRStockHolding *stockTXRH = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockGSAT = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockAAPL = [[BNRStockHolding alloc]init];
        
         // Made this stock foreign, although not really foreign
        BNRForeignStockHolding *stockZNGA = [[BNRForeignStockHolding alloc]init];
        
         
 /*******************************************************************/
 
        stockTXRH.companyName = (@"Texas Roadhouse");
        stockTXRH.symbol = @"TXRH";
        stockTXRH.purchaseSharePrice = 26.62;
        stockTXRH.currentSharePrice = 28.31;
        stockTXRH.numberOfShares = 2556;
        [myStockPortfolio addStock:stockTXRH];
        
        
/*******************************************************************/
        
        stockGSAT.companyName = (@"GlobalStar");
        stockGSAT.symbol = @"GSAT";
        stockGSAT.purchaseSharePrice = 2.30;
        stockGSAT.currentSharePrice = 4.50;
        stockGSAT.numberOfShares = 40;
        [myStockPortfolio addStock:stockGSAT];
        
        
/*******************************************************************/
        
        stockAAPL.companyName = (@"Apple");
        stockAAPL.symbol = @"AAPL";
        stockAAPL.purchaseSharePrice = 10.50;
        stockAAPL.currentSharePrice = 99.62;
        stockAAPL.numberOfShares = 500;
        [myStockPortfolio addStock:stockAAPL];
        
/*******************************************************************/
        
        // Made this stock foreign, although not really foreign
        
        stockZNGA.companyName = (@"Zynga");
        stockZNGA.symbol = @"ZNGA";
        stockZNGA.purchaseSharePrice = 2.30;
        stockZNGA.currentSharePrice = 4.50;
        stockZNGA.numberOfShares = 40;
        stockZNGA.conversionRate = 0.94;
        
        
        [myStockPortfolio addStock:stockZNGA];
        
        
        // Displays information for each stock in your portfolio.
        // Also displays the current value of the portfolio
        
        // Put a break here and step into for each line to test
        NSLog(@"%@", myStockPortfolio);
        
        [myStockPortfolio removeStock:stockTXRH];
        
        // Show the portfolio info again to see if stock is removed
        NSLog(@"%@", myStockPortfolio);
        
        // Remove stock
        [myStockPortfolio removeStock:stockGSAT];
        
        // Check portfolio again
        NSLog(@"%@", myStockPortfolio);
        
        // Remove stock
        [myStockPortfolio removeStock:stockAAPL];
        
        // Check portfolio again
        NSLog(@"%@", myStockPortfolio);
        
        // Remove stock
        [myStockPortfolio removeStock:stockZNGA];
        
        NSLog(@"%@", myStockPortfolio);
        
    }
 return 0;
}

BNRPortfolio.h


#import <Foundation/Foundation.h>
@class BNRStockHolding;

@interface BNRPortfolio : NSObject

- (float) valueOfPortfolio;
- (void) addStock:(BNRStockHolding *)stock;
- (void) removeStock:(BNRStockHolding *)stock;

@end

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"


// Class Extension
@interface BNRPortfolio ()

// Property is private.  Visible only within implementation file.
@property (nonatomic) NSMutableArray *holdings;

@end

@implementation BNRPortfolio


// Add stocks to the portfolio

- (void) addStock:(BNRStockHolding *)stock
{
    if (self.holdings == nil)
    {
        self.holdings = [[NSMutableArray alloc]init];
    }
    
    [self.holdings addObject:stock];
    
}

- (void) removeStock:(BNRStockHolding *) stock
{
    [self.holdings removeObject:stock];
    
}

- (float) valueOfPortfolio
{
    float sum = 0;
    
    for (BNRStockHolding *stock in self.holdings)
    {
        sum += [stock valueInDollars];
    }
    
    return sum;
}

- (NSString *) description
{
    
    NSLog(@"\nPortfolio Information:");
    
    for (BNRStockHolding *stock in self.holdings)
    {
        // Displays the custom descriptions for both
        // BNRStockHolding and BNRForeignStockholding
        NSLog(@"%@", stock);
        
        
    }
    
    return [NSString stringWithFormat:@"\nThe current value of your portfolio is: $%.2f", [self valueOfPortfolio]];
}

@end

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *companyName;
@property (nonatomic) NSString *symbol;
@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: %@\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.symbol, 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
{
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %@\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.symbol, self.numberOfShares,
          (self.purchaseSharePrice * self.conversionRate) , (self.currentSharePrice * self.conversionRate),
          [self costInDollars], [self valueInDollars], self.conversionRate];
    
}

@end