My Solution to Challenge: holding portfolio


#1

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 purchaseSharePrice;
@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
- (void)addSelfToArray:(NSMutableArray *)theArray; // This method is not used in this project

@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


- (float)costInDollars
{
    float purSharePrice = [self purchaseSharePrice];
    int numOfShares = [self numberOfShares];
    
    return (purSharePrice * numOfShares);
}
- (float)valueInDollars
{
    float curSharePrice = [self currentSharePrice];
    int numOfShares = [self numberOfShares];
    
    return (curSharePrice * numOfShares);
}

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

- (void)addSelfToArray:(NSMutableArray *)theArray // This method is not used in this project
{
    [theArray addObject: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 foreignProfit = [self valueInDollars] - [self costInDollars];
    return (foreignProfit);
}

@end

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

@property (nonatomic, copy) NSArray *holdings;

-(float)totalValue;

-(void)addStockToPortfolio:(BNRStockHolding *)stock;

@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"


@implementation BNRPortfolio

{
    NSMutableArray *_holdings;
    float totalCostInDollars;
    float totalGainOrLossInDollars;
}

- (void)setHoldings:(NSArray *)holdings
{
    _holdings = [holdings mutableCopy];
    
}

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


- (void)addStockToPortfolio:(BNRStockHolding *)stock
{
    if (!_holdings) {
        _holdings = [[NSMutableArray alloc]init];
    }
    
    [_holdings addObject:stock];
    
}

- (float) totalValue
{
    float totalValueInDollars = 0;
   
    
    for (BNRStockHolding *stocks in _holdings) {
        
        totalValueInDollars += [stocks valueInDollars];
        totalCostInDollars += [stocks costInDollars];
        totalGainOrLossInDollars += [stocks gainOrLoss];
    }
    
    return totalValueInDollars;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"\nValue of the Portfolio: $%.2f\n Cost of the Portfolio $%.2f\n The Current Gain Or Loss of the Portfolio $%.2f",
            [self totalValue], totalCostInDollars, totalGainOrLossInDollars];

}

@end

main.m

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

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

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

    @autoreleasepool {
        
        BNRStockHolding *idev = [[BNRStockHolding alloc] init];
        BNRStockHolding *mptv = [[BNRStockHolding alloc]init];
        BNRStockHolding *txrh = [[BNRStockHolding alloc] init];
        BNRForeignStockHolding *renn = [[BNRForeignStockHolding alloc]init];
        BNRPortfolio *stockPortfolio = [[BNRPortfolio alloc]init];
                
     
        idev.purchaseSharePrice = 2.30;
        idev.currentSharePrice = 4.50;
        idev.numberOfShares = 40;
        idev.symbol = @("IDEV");
        [stockPortfolio addStockToPortfolio:idev];
        
        mptv.purchaseSharePrice = 2.30;
        mptv.currentSharePrice = 4.50;
        mptv.numberOfShares = 40;
        mptv.symbol = @("MPTV");
        [stockPortfolio addStockToPortfolio:mptv];
        
        
        txrh.purchaseSharePrice = 2.30;
        txrh.currentSharePrice = 4.50;
        txrh.numberOfShares = 40;
        txrh.symbol = @("TXRH");
        [stockPortfolio addStockToPortfolio:txrh];
        
        renn.purchaseSharePrice = 2.30;
        renn.currentSharePrice = 4.50;
        renn.numberOfShares = 40;
        renn.conversionRate = .94;
        renn.symbol = @("RENN");
        [stockPortfolio addStockToPortfolio:renn];
        
      
      
        for (BNRStockHolding *stockPort in stockPortfolio.holdings)
        {
            
            NSLog(@"The Stock Ticker Symbol is %@", [stockPort symbol]);
            NSLog(@"The Purchase Price $%.2f", [stockPort purchaseSharePrice]);
            NSLog(@"The Current Share Price $%.2f", [stockPort currentSharePrice]);
            NSLog(@"The Number of Shares Purchased %d\n", [stockPort numberOfShares]);
            NSLog(@"The Cost in Dollars $%.2f", [stockPort costInDollars]);
            NSLog(@"The Current Dollar Value $%.2f", [stockPort valueInDollars]);
            NSLog(@"Gain or Loss value $%.2f\n\n\r", [stockPort gainOrLoss]);
            
            
        }
        
        NSLog(@"%@", stockPortfolio);
    }
    return 0;
}