I spent three days (on and off) trying to understand these 2 challenges (Chapter 21 and 22) and by far these were the most difficult concept for me to grasp.
I’m attaching my code, please verify my solution for me. Thank you!
main.m
//
// main.m
// Stocks
//
// Created by Max Doumkine on 2/17/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Initiating an NSMutableArray to hold
// created stock instances
NSMutableArray *stocks = [[NSMutableArray alloc] init];
// StockHolding first instance
BNRForeignStockHolding *microsoft = [[BNRForeignStockHolding alloc] init];
// Asigning values to first instance
microsoft.purchaseSharePrice = 2.30;
microsoft.currentSharePrice = 4.50;
microsoft.numberOfShares = 40;
microsoft.conversionRate = 2.0;
microsoft.symbol = @"MIC";
[stocks addObject:microsoft];
// StockHolding second instance
BNRForeignStockHolding *apple = [[BNRForeignStockHolding alloc] init];
// Asigning values to second instance
apple.purchaseSharePrice = 12.19;
apple.currentSharePrice = 10.56;
apple.numberOfShares = 90;
apple.conversionRate = 1.2;
apple.symbol = @"APL";
[stocks addObject:apple];
// StockHolding third instance
BNRForeignStockHolding *redhat = [[BNRForeignStockHolding alloc] init];
// Asigning values to third instance
redhat.purchaseSharePrice = 45.10;
redhat.currentSharePrice = 49.51;
redhat.numberOfShares = 210;
redhat.conversionRate = 1.5;
redhat.symbol = @"RDH";
[stocks addObject:redhat];
// Initiating portfolio and adding stocks to it
BNRPortfolio *stockHoldingsPortfolio = [[BNRPortfolio alloc] init];
[stockHoldingsPortfolio addStock:stocks[0]];
[stockHoldingsPortfolio addStock:stocks[1]];
[stockHoldingsPortfolio addStock:stocks[2]];
// Removing stocks from portfolio.
[stockHoldingsPortfolio removeStock:stocks[2]];
NSLog(@"%@", stockHoldingsPortfolio);
}
return 0;
}
BNRPortfolio.h
//
// BNRPortfolio.h
// Stocks
//
// Created by Max Doumkine on 2/20/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BNRPortfolio : NSObject
@property (nonatomic) unsigned int portfolioValue;
@property (nonatomic, copy) NSArray *portfolio;
- (void)addStock:(BNRPortfolio *)p;
- (void)removeStock:(BNRPortfolio *)p;
- (unsigned int)valueOfPortfolio;
- (NSString *)stockSymbol;
@end
BNRPortfolio.m
//
// BNRPortfolio.m
// Stocks
//
// Created by Max Doumkine on 2/20/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
#import "BNRPortfolio.h"
#import "BNRForeignStockHolding.h"
@interface BNRPortfolio ()
{
NSMutableArray *_portfolio;
}
@end
@implementation BNRPortfolio
- (NSString *)description
{
return [NSString stringWithFormat:@"Portfolio is worth $%d dollars and has %lu stocks\n", self.valueOfPortfolio, (unsigned long)[_portfolio count]];
}
- (unsigned int)valueOfPortfolio
{
// Sum up the value of portfolio stocks
unsigned int sum = 0;
for (BNRForeignStockHolding *company in _portfolio){
sum += company.valueInDollars;
}
return sum;
}
- (void)addStock:(BNRPortfolio *)p
{
//Is portfolio nil?
if(!_portfolio) {
// Create the array
_portfolio = [[NSMutableArray alloc] init];
}
//Adding object to the portfolio array
[_portfolio addObject:p];
}
- (void)removeStock:(BNRPortfolio *)p
{
if(_portfolio) {
[_portfolio removeObject:p];
NSLog(@"Removing object from portfolio");
}
}
@end
BNRForeignStockHolding.h
//
// BNRForeignStockHolding.h
// Stocks
//
// Created by Max Doumkine on 2/17/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
#import "BNRStockHolding.h"
@interface BNRForeignStockHolding : BNRStockHolding
@property (nonatomic) float conversionRate;
@property (nonatomic, copy) NSString *symbol;
- (float)costInDollars;
- (float)valueInDollars;
@end
BNRForeignStockHolding.m
//
// BNRForeignStockHolding.m
// Stocks
//
// Created by Max Doumkine on 2/17/14.
// Copyright (c) 2014 BigNerdRanch. All rights reserved.
//
#import "BNRForeignStockHolding.h"
@implementation BNRForeignStockHolding
- (float)costInDollars;
{
return self.purchaseSharePrice * self.numberOfShares * self.conversionRate;
}
- (float)valueInDollars;
{
return self.currentSharePrice * self.numberOfShares * self.conversionRate;
}
@end