Chapter 22 Challenge Solution


#1

Spent a whole day cleaning my code up from Chapter 21. Also changed my code to remove the object by symbol rather than by indexes. Let me know what you think.

main.m

//
//  main.m
//  Stocks
//
//  Created by Adam G. on 5/23/14.
// 
//

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

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        
        BNRPortfolio *stockPortfolio = [[BNRPortfolio alloc]init];
        
        for (int i = 0; i < 3; i++) {
            
        BNRStockHolding *stock = [[BNRStockHolding alloc] init];
            stock.purchasePrice = 2.30;
            stock.currentSharePrice = 4.50;
            stock.numberOfShares = 40;
          
        switch (i) {
            case 0:
                stock.symbol = @("IDEV");
                [stockPortfolio addStockToPortfolio:stock];
                break;
            case 1:
                stock.symbol = @("MPTV");
                [stockPortfolio addStockToPortfolio:stock];
                break;
            case 2:
                stock.symbol = @("TXRH");
                [stockPortfolio addStockToPortfolio:stock];
                break;
            default:
                break;
        }
        
        }
        
        
      BNRForeignStockHolding *foreignStock = [[BNRForeignStockHolding alloc]init];
        
        foreignStock.purchasePrice = 2.30;
        foreignStock.currentSharePrice = 4.50;
        foreignStock.numberOfShares = 40;
        foreignStock.conversionRate = .94;
        foreignStock.symbol = @("RENN");
        [stockPortfolio addStockToPortfolio:foreignStock];
        
        [stockPortfolio portfolioInfo];
        
        [stockPortfolio removeStockFromPortfolio:@"TXRH"];
        [stockPortfolio removeStockFromPortfolio:@"IDEV"];
        
        // Recalculate the new values of the portfolio
        [stockPortfolio portfolioInfo];
   
      
    }
    return 0;
}

BNRPortfolio.h

//
//  BNRPortfolio.h
//  Stocks
//
//  Created by Adam G. on 5/23/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

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


@interface BNRPortfolio : NSObject

- (void)portfolioInfo;

-(void) addStockToPortfolio:(BNRStockHolding *)stock;
-(void) removeStockFromPortfolio:(NSString *)symbol;

@end

BNRPortfolio.m

//
//  BNRPortfolio.m
//  Stocks
//
//  Created by Adam G. on 5/23/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

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


@interface BNRPortfolio ()

{
    float totalCostOfThePortfolio;
    float totalGainOrLossOfThePortfolio;
    float totalValueOfThePortfolio;
}

@property (nonatomic) NSMutableArray *holdings;


@end

@implementation BNRPortfolio


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

-(void) removeStockFromPortfolio:(NSString *)symbol
{
    
    // Remove the stock from the portfolio based on the symbol that was passed
    for (NSUInteger i = [self.holdings count]; i > 0; i--) {
        
        BNRStockHolding *stockToRemove = [[BNRStockHolding alloc]init];
        stockToRemove = [self.holdings objectAtIndex:i - 1];
        
        if ([stockToRemove.symbol isEqualToString:symbol]) {
            [self.holdings removeObjectAtIndex:i - 1];
            break;
        }
        }


}

- (void)portfolioInfo
{
    NSLog(@"Stocks in the Portfolio: ");
for (BNRStockHolding *stock in self.holdings) {
    
    NSLog(@"Stock Ticker Symbol is %@", stock.symbol);
    NSLog(@"Purchase Price $%.2f", stock.purchasePrice);
    NSLog(@"Current Share Price $%.2f", stock.currentSharePrice);
    NSLog(@"Number of Shares Purchased %d\n", stock.numberOfShares);
    NSLog(@"Cost in Dollars $%.2f", [stock costInDollars]);
    NSLog(@"Current Dollar Value $%.2f", [stock valueInDollars]);
    NSLog(@"Gain or Loss value $%.2f\n\r", [stock gainOrLoss]);
    }
    
    
    totalCostOfThePortfolio = 0;
    totalGainOrLossOfThePortfolio = 0;
    totalValueOfThePortfolio = 0;
   
    
    for (BNRStockHolding *stock in self.holdings) {
        
        totalCostOfThePortfolio += [stock costInDollars];
        totalGainOrLossOfThePortfolio += [stock gainOrLoss];
        totalValueOfThePortfolio += [stock valueInDollars];

   }
    NSLog(@"Portfolio Summary: ");
          NSLog(@"Cost of the Portfolio: $%.2f", totalCostOfThePortfolio);
          NSLog(@"Value of the Portfolio: $%.2f", totalValueOfThePortfolio);
          NSLog(@"Gain or Loss Value: $%.2f\n\r", totalGainOrLossOfThePortfolio);
          
          }

@end

BNRStockHolding.h

//
//  BNRStockHolding.h
//  Stocks
//
//  Created by Adam G on 5/17/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchasePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic) NSString *symbol;


- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
- (float)gainOrLoss; //profit or loss valueInDollars - costInDollars

@end

BNRStockHolding.m

//
//  BNRStockHolding.m
//  Stocks
//
//  Created by Adam G. 5/17/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

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

@implementation BNRStockHolding


// Initial cost of purchasing the stock 
- (float)costInDollars
{
    return (self.purchasePrice * self.numberOfShares);
}

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

- (float)gainOrLoss
{
    float profitOrLoss = [self valueInDollars] - [self costInDollars];
    return (profitOrLoss);
}

- (NSString *)description
{
    
    return [NSString stringWithFormat:@"Ticker Symbol: %@", self.symbol];
}


-(void) dealloc
{
    NSLog(@"deallocating BNRStockHolding %@", self);
}
@end

BNRForeignStockHolding.h

//
//  BNRForeignStockHolding.h
//  Stocks
//
//  Created by Adam G. on 5/19/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

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

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end

BNRForeignStockHolding.m

//
//  BNRForeignStockHolding.m
//  Stocks
//
//  Created by Adam G. on 5/19/14.
//  Copyright (c) 2014 Adam. All rights reserved.
//

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding

- (float)costInDollars; // (purchaseSharePrice * numberOfShares) * coversionRate
{
    float foreignPurchasePrice = [super costInDollars] * self.conversionRate;
    
    return (foreignPurchasePrice);
    
}

- (float)valueInDollars // currentSharePrice * numberOfShares
{
    float foreignValue = [super valueInDollars] * self.conversionRate;
    
    return (foreignValue);
}

- (float)gainOrLoss
{
    float foreignProfitOrLoss = [self valueInDollars] - [self costInDollars];
    return (foreignProfitOrLoss);
}

- (NSString *)description
{
    
    return [NSString stringWithFormat:@"Ticker Symbol: %@", self.symbol];
    
}


-(void) dealloc
{
    NSLog(@"deallocating BNRForeignStockHolding %@", self);
}
@end