// BNRStockHolding.h
#ifndef BNRStockHolding_h
#define BNRStockHolding_h
#endif /* BNRStockHolding_h */
#import <Foundation/Foundation.h>
@interface BNRStockHolding: NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
float _costInDollars;
float _valueInDollars;
float _conversionRate;
NSString * _symbol;
}
//getters
-(float) purchaseSharePrice;
-(float) currentSharePrice;
-(int) numberOfShares;
-(NSString *) symbol;
//setters
-(void) setPurchaseSharePrice: (float) purchasePrice;
-(void) setCurrentSharePrice: (float) currentPrice;
-(void) setNumberOfShares: (int) num;
-(void) setSymbol: (NSString *) s;
//methods
-(float) costInDollars;
-(float) valueInDollars;
@end
@interface BNRForeignStockHolding: BNRStockHolding
{
float conversionRate;
}
-(float) conversionRate;
-(void) setConversionRate: (float) cRate;
-(float) costInDollars;
-(float) valueInDollars;
@end
// BNRStockHolding.m
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
@implementation BNRStockHolding
//getters
-(float) purchaseSharePrice;
{
return _purchaseSharePrice;
}
-(float) currentSharePrice
{
return _currentSharePrice;
}
-(int) numberOfShares
{
return _numberOfShares;
}
-(NSString *) symbol;
{
return _symbol;
}
//setters
-(void) setPurchaseSharePrice: (float) purchasePrice
{
_purchaseSharePrice = purchasePrice;
}
-(void) setCurrentSharePrice: (float) currentPrice
{
_currentSharePrice = currentPrice;
}
-(void) setNumberOfShares: (int) num;
{
_numberOfShares = num;
}
-(void) setSymbol: (NSString *) s;
{
_symbol = s;
}
//methods
-(float) costInDollars
{
return _purchaseSharePrice * _numberOfShares;
}
-(float) valueInDollars
{
return _currentSharePrice * _numberOfShares;
}
@end
@implementation BNRForeignStockHolding
-(float) conversionRate;
{
return _conversionRate;
}
-(void) setConversionRate: (float) cRate;
{
_conversionRate = cRate;
}
-(float) costInDollars //overriding method
{
return _purchaseSharePrice * _conversionRate * _numberOfShares;
}
-(float) valueInDollars //overriding method
{
return _currentSharePrice * _conversionRate * _numberOfShares;
}
@end
// BNRPortfolio.h
#ifndef BNRPortfolio_h
#define BNRPortfolio_h
#endif /* BNRPortfolio_h */
#import "BNRStockHolding.h"
@interface BNRPortfolio: BNRStockHolding
{
NSMutableArray *_portfolioStocks;
}
-(NSMutableArray *) portfolioStocks;
-(void) setPortfolioStocks: (NSMutableArray *) arr;
-(void) addPortfolioStocks: (BNRPortfolio *) a;
-(float) sumValues;
@end
// BNRPortfolio.m
#import <Foundation/Foundation.h>
#import "BNRPortfolio.h"
@implementation BNRPortfolio
-(NSMutableArray *) portfolioStocks
{
return [_portfolioStocks copy];
}
-(void) setPortfolioStocks: (NSMutableArray *) arr
{
_portfolioStocks = [arr mutableCopy];
}
-(void) addPortfolioStocks: (BNRPortfolio *) a
{
if (!_portfolioStocks)
{
_portfolioStocks = [[NSMutableArray alloc] init];
}
[_portfolioStocks addObject: a];
}
-(float) sumValues
{
float sum = 0.0;
unsigned long elements;
elements = [_portfolioStocks count];
//NSLog (@"elements: %lu", elements);
unsigned long i;
for (i = 0; i < elements; i++)
{
sum += [_portfolioStocks[i] valueInDollars];
}
return sum;
}
-(NSString *) description
{
return [NSString stringWithFormat:@ "Stock %@: $%.2f" , self.symbol, self.valueInDollars];
}
@end
//
// main.m
// BNRStockHolding
//
// Created by Mark Jensen on 9/23/18.
/* I chose to directly assign my stocks prices and other values as
one would in the real world. The challenge of this problem is
creating an array of objects that is tucked away in a class,
and then trying to access it and its members' values from main.
I feel the book only glosses over arrays, neglecting explanation
on how to deal with them when they aren't defined in main.
I don't know if I've done this exercise correctly, but it
does compile and return the values asked for. I present it to
perhaps help you understand this exercise.
*/
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRPortfolio.h"
int main(int argc, const char * argv[]) {
@autoreleasepool
{
BNRPortfolio *myPortfolio = [[BNRPortfolio alloc] init];
BNRPortfolio *apple = [[BNRPortfolio alloc] init];
BNRPortfolio *disney = [[BNRPortfolio alloc] init];
BNRPortfolio *microsoft = [[BNRPortfolio alloc] init];
BNRPortfolio *visa = [[BNRPortfolio alloc] init];
BNRPortfolio *verizon = [[BNRPortfolio alloc] init];
BNRPortfolio *nike = [[BNRPortfolio alloc] init];
BNRPortfolio *intel = [[BNRPortfolio alloc] init];
BNRPortfolio *catepillar = [[BNRPortfolio alloc] init];
//set symbol:
[apple setSymbol: @"AAPL"];
[disney setSymbol: @"DIS"];
[microsoft setSymbol: @"MSFT"];
[visa setSymbol: @"V"];
[verizon setSymbol: @"VZ"];
[nike setSymbol: @"NKE"];
[intel setSymbol: @"INTL"];
[catepillar setSymbol: @"CAT"];
//set # of shares:
[apple setNumberOfShares: 40];
[disney setNumberOfShares: 70];
[microsoft setNumberOfShares: 150];
[visa setNumberOfShares: 60];
[verizon setNumberOfShares: 20];
[nike setNumberOfShares: 55];
[intel setNumberOfShares: 90];
[catepillar setNumberOfShares: 120];
//set cost:
[apple setPurchaseSharePrice: 210.34];
[disney setPurchaseSharePrice: 116.85];
[microsoft setPurchaseSharePrice: 109.76];
[visa setPurchaseSharePrice: 134.59];
[verizon setPurchaseSharePrice: 60.34];
[nike setPurchaseSharePrice: 67.23];
[intel setPurchaseSharePrice: 46.88];
[catepillar setPurchaseSharePrice: 105.92];
//set value:
[apple setCurrentSharePrice: 220.42];
[disney setCurrentSharePrice: 115.21];
[microsoft setCurrentSharePrice: 113.98];
[visa setCurrentSharePrice: 149.27];
[verizon setCurrentSharePrice: 53.14];
[nike setCurrentSharePrice: 83.70];
[intel setCurrentSharePrice: 45.70];
[catepillar setCurrentSharePrice: 153.17];
[myPortfolio addPortfolioStocks: apple];
[myPortfolio addPortfolioStocks: disney];
[myPortfolio addPortfolioStocks: microsoft];
[myPortfolio addPortfolioStocks: visa];
[myPortfolio addPortfolioStocks: verizon];
[myPortfolio addPortfolioStocks: nike];
[myPortfolio addPortfolioStocks: intel];
[myPortfolio addPortfolioStocks: catepillar];
//NSLog (@"%@ ", [myPortfolio portfolioStocks]);
int i;
for (i = 0; i < [myPortfolio.portfolioStocks count]; i++)
{
NSLog (@"%@ ", [myPortfolio.portfolioStocks objectAtIndex: i]);
}
NSLog (@"Value of portfolio: $%.2f ", [myPortfolio sumValues]);
}
return 0;
}