I'm currently teaching myself Objective-C with the Big Nerd Ranch guide (2nd edition) and I have not had a problem up until chapter 21. I checked the book's forum, but it was of no help because most people there are newbies with spaghetti code, and I do not want to adopt bad habits...I am a beginner too, as this is my first major language, but spaghetti code is obvious when you see it.
I have to take my code from a previous chapter and make a tool that creates an instance of a BNRPortfolio class and fills it with stock holdings (must also sum up the portfolio's current value). Then I have to add a symbol property to BNRStockHolding that holds the stock ticker symbol as an NSString.
I am just completely confused as to where to add the code to do this and how to begin. From my research online, this chapter was poorly written and is where most readers have gotten stuck...the authors even agreed that in future editions (if ever because of Swift moving in) they would re-write the entire chapter.
Currently, my code from a previous chapter looks like this:
BNRStockHolding.h
#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
- (float)costInDollars;
- (float)valueInDollars;
@end
BNRStockholding.m
#import "BNRStockHolding.h"
@implementation BNRStockHolding
- (float)costInDollars
{
return _purchaseSharePrice * _numberOfShares;
}
- (float)valueInDollars
{
return _currentSharePrice * _numberOfShares;
}
@end
Next, I had to add foreign stocks with a conversion rate that converted the values and cost into U.S. dollars:
BNRForeignStockHolding.h
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
@interface BNRForeignStockHolding : BNRStockHolding
@property (nonatomic) float conversionRate;
@end
BNRForeignStockHolding.m
#import "BNRForeignStockHolding.h"
@implementation BNRForeignStockHolding : BNRStockHolding
- (float)costInDollars
{
float foreignCostInDollars = super.costInDollars;
return _foreignCostInDollars * _conversionRate;
}
- (float)valueInDollars
{
float foreignValueInDollars = super.valueInDollars;
return _foreignValueInDollars * _conversionRate;
}
@end
My main file: main.m
#import <Foundation/Foundation.h>
#import "BNRForeignStockHolding.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BNRStockHolding *stock0 = [[BNRStockHolding alloc]init];
BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
BNRStockHolding *stock2 = [[BNRStockHolding alloc]init];
BNRForeignStockHolding *stock3 = [[BNRForeignStockHolding alloc]init];
stock0.purchaseSharePrice=2.30;
stock0.currentSharePrice=4.50;
stock0.numberOfShares=40;
stock1.purchaseSharePrice=12.19;
stock1.currentSharePrice=10.56;
stock1.numberOfShares=90;
stock2.purchaseSharePrice=45.10;
stock2.currentSharePrice=49.51;
stock2.numberOfShares=210;
stock3.purchaseSharePrice=43.05;
stock3.currentSharePrice=28.31;
stock3.numberOfShares=15;
stock3.conversionRate=0.3;
NSMutableArray *stocks = [NSMutableArray arrayWithObjects:stock0, stock1, stock2, stock3, nil];
for (BNRForeignStockHolding *s in stocks) {
float a = s.purchaseSharePrice;
float b = s.currentSharePrice;
int c = s.numberOfShares;
if ([s isMemberOfClass:[BNRForeignStockHolding class]]) {
float d = s.foreignCostInDollars;
float e = s.foreignValueInDollars;
NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, d, e);
}
else {
float d = s.costInDollars;
float e = s.valueInDollars;
NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, d, e);
}
}
}
return 0;
}
I tried this:
In BNRStockHolding.h, I created an instance variable:
- (float)currentTotalValue
and in BNRStockHolding.m, I implemented it like this:
-(float)currentTotalValue
{
return _currentTotalValue = _currentSharePrice * _numberOfShares;
}
Then in main.m, I added this to the 'for' function in my stocks array:
float f = s.currentTotalValue;
Then added %.2f@, f to my NSLog print out.
It worked perfectly except for this caveat:
It simply gave me the value of each individual stock without converting the foreign stocks. I somewhat understand why, but the chapter did not explain thoroughly how to attack such a problem, so I am totally lost. I couldn't even attempt adding the stock ticker symbol because I wasn't sure where to implement it.
When adding up the total value of my portfolio by hand, converted foreign values included, the total should be $17,774.895. Keep in mind that these stocks are pretend, as is the conversion rate, etc.
Thank You for any help, it is greatly appreciated!
EDIT TO ADD
Problem solved, I had to rewrite my BNRStock and BNRForeign .h and .m files with overrides for costInDollars and valueInDollars, as well as add an exception for NSLog to print out separate results for each via an [NSString stringWithFormat:] method. Then I added the sum code recommended with a few tweaks and everything worked well. I also added the stock tickers with no problem by defining symbol as a pointer in my .h and .m files and then including them in the [NSString stringWithFormat:] printout command.
Shoutout to danH for noticing that my costInDollars and valueInDollars should have been overridden to work for both BNRStock and BNRForeign classes, as that was the main issue with my program.