Challenge - My Solution

Everything functions as planned, I’m just posting to see if anyone has any thoughts on how I could possibly simplify the code, or just any general thoughts on it. Thanks.

[code]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

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

@autoreleasepool {
    
    
    
    // creating instances of BNRStockHolding
    BNRStockHolding *tsla = [[BNRStockHolding alloc] init];
    BNRStockHolding *aple = [[BNRStockHolding alloc] init];
    BNRStockHolding *twtr = [[BNRStockHolding alloc] init];
    
   
    // setting instance variables
    
    [tsla setCurrentSharePrice:193.63];
    [tsla setNumberOfShares:30];
    [tsla setPurchaseSharePrice:123.77];
    [tsla setNameOfCompany:@"Tesla"];
    
    
    [aple setCurrentSharePrice:537.90];
    [aple setPurchaseSharePrice:200];
    [aple setNumberOfShares:40];
    [aple setNameOfCompany:@"Apple"];
    
    
    [twtr setCurrentSharePrice:55.42];
    [twtr setPurchaseSharePrice:42.05];
    [twtr setNumberOfShares:100];
    [twtr setNameOfCompany:@"Twitter"];
   
    
    //create mutableArray
    NSMutableArray *stocks = [NSMutableArray array];
    
    // add the stocks to the array
    [stocks addObject:tsla];
    [stocks addObject:aple];
    [stocks addObject:twtr];
    
    
    //iterate over the array, send calculation messages
    for (BNRStockHolding *i in stocks) {
        id name = [i nameOfCompany];
        float c = [i currentSharePrice];
        float p = [i purchaseSharePrice];
        int n = [i numberOfShares];
        float cost = [i costInDollars];
        float value = [i valueInDollars];
        
        
        NSLog(@"%@ shares are currently at $%.2f, you originally purchased your shares at $%.2f, and you currently own %d shares. Your inital investment was $%.2f, and your stock's current value is $%.2f", name, c, p, n, cost, value);
        
    }

[/code]

@bugehoobies
is there another way of outputting the name of each class instance other than making special ivar for it?
i was having trouble with it and the only the way i saw was the way you made a _setName variable.

i am not sure about efficiency but i guess we could write a function that we could send the variables (float,float,int,id) and it would store them in BNRStockholding and add it to the array?
that way the main.m file would be three function calls and a for loop that prints the results?

Yea I’m not certain of any ways to have the company name listed other than creating an ivar for it. When you get a description of a pointer the address is returned, and I don’t think you can get it to describe itself by what it is called, but I’m not certain on that one. You could potentially have the program search the web with a symbol (set using an ivar) and find the name of the company like that, but that would be a little advanced at this point in the book, and again I’m not certain how to code that.

As for the loop, in the later chapter where we create the BNRPortfolio class I actually started by using a loop to generate 6 stocks and then load them to the array, but then decided to just log the stocks I’d already created into an instance of BNRPortfolio, this way I could run the math on the portfolio myself and be certain everything was working correctly. One thing that might work though is using a loop and a readline function to allow the user to add stocks, then add their current price, purchase price, number of shares, and so on. Set it to only run x amount of times, then after it’s completed it would move on and calculate the current value and all that.

Would you mind posting your BNRStockHolding.h and .m files? Im curious how you handles the strings for the company name in your class. I really struggled with getting it to work. I tried originally to do it using char instead of NSString, but couldn’t get it going.

Here was my solution:

main.m:

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

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

    @autoreleasepool {
        
        BNRStockHolding *aapl = [[BNRStockHolding alloc]init];
        BNRStockHolding *msft = [[BNRStockHolding alloc]init];
        BNRStockHolding *goog = [[BNRStockHolding alloc]init];
//        NSMutableArray *holdings = [NSArray arrayWithObjects:aapl, msft, goog, nil];
        NSArray *holdings = @[aapl, msft, goog];
        
        [aapl setSymbolOfShares:@"aapl"];
        [aapl setNumberOfShares:5];
        [aapl setPurchaseSharePrice:199.85];
        [aapl setCurrentSharePrice:532.87];
        
        [msft setSymbolOfShares:@"msft"];
        [msft setNumberOfShares:34];
        [msft setPurchaseSharePrice:30.36];
        [msft setCurrentSharePrice:40.16];
        
        [goog setSymbolOfShares:@"goog"];
        [goog setNumberOfShares:2];
        [goog setPurchaseSharePrice:596.42];
        [goog setCurrentSharePrice:1183.04];
        
        for (BNRStockHolding *h in holdings) {
            NSLog(@"Stock symbol:%@", [h symbolOfShares]);
            NSLog(@"----------------");
            NSLog(@"Purchase Price/Share: %.2f", [h purchaseSharePrice]);
            NSLog(@"Current Price/Share: %.2f", [h currentSharePrice]);
            NSLog(@"Number of Shares: %d", [h numberOfShares]);
            NSLog(@"Total Cost in USD: %.2f", [h costInDollars]);
            NSLog(@"Total Value in USD: %.2f", [h valueInDollars]);
            NSLog(@"Profit in USD: %.2f", [h valueInDollars]-[h costInDollars]);
            NSLog(@"\n");
        }
        
    }
    return 0;
}[/code]

BNRStockHolding.h:
[code]#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

{
    float _currentSharePrice;
    float _purchaseSharePrice;
    int _numberOfShares;
    NSString *_symbolOfShares;
    
}
- (float)costInDollars;     // purchaseSharePrice * numberOfShares
- (float)valueInDollars;    // currentSharePrice * numberOfShares
- (float)purchaseSharePrice;
- (float)currentSharePrice;
- (int)numberOfShares;
- (NSString*)symbolOfShares;
- (void)setCurrentSharePrice:(float)c;
- (void)setPurchaseSharePrice:(float)p;
- (void)setNumberOfShares:(int)n;
- (void)setSymbolOfShares:(NSString*)s;


@end

BNRStockHolding.m:

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
    {
    return [self purchaseSharePrice] * [self numberOfShares];
    }

  • (float)valueInDollars; // currentSharePrice * numberOfShares
    {
    return [self currentSharePrice] * [self numberOfShares];
    }

  • (float)purchaseSharePrice;
    {
    return _purchaseSharePrice;
    }

  • (float)currentSharePrice;
    {
    return _currentSharePrice;
    }

-(int)numberOfShares;
{
return _numberOfShares;
}

  • (void)setCurrentSharePrice:(float)c;
    {
    _currentSharePrice = c;
    }

  • (void)setPurchaseSharePrice:(float)p;
    {
    _purchaseSharePrice = p;
    }

  • (void)setNumberOfShares:(int)n;
    {
    _numberOfShares = n;
    }

  • (void)setSymbolOfShares:(NSString*)s;
    {
    _symbolOfShares = s;
    }

  • (NSString*)symbolOfShares;
    {
    return _symbolOfShares;
    }

@end
[/code]

Output:

2014-03-21 22:26:23.649 Chapter18-Challenge1-Stocks[7761:303] Stock symbol:aapl 2014-03-21 22:26:23.656 Chapter18-Challenge1-Stocks[7761:303] ---------------- 2014-03-21 22:26:23.658 Chapter18-Challenge1-Stocks[7761:303] Purchase Price/Share: 199.85 2014-03-21 22:26:23.659 Chapter18-Challenge1-Stocks[7761:303] Current Price/Share: 532.87 2014-03-21 22:26:23.659 Chapter18-Challenge1-Stocks[7761:303] Number of Shares: 5 2014-03-21 22:26:23.660 Chapter18-Challenge1-Stocks[7761:303] Total Cost in USD: 999.25 2014-03-21 22:26:23.660 Chapter18-Challenge1-Stocks[7761:303] Total Value in USD: 2664.35 2014-03-21 22:26:23.660 Chapter18-Challenge1-Stocks[7761:303] Profit in USD: 1665.10 2014-03-21 22:26:23.661 Chapter18-Challenge1-Stocks[7761:303] 2014-03-21 22:26:23.661 Chapter18-Challenge1-Stocks[7761:303] Stock symbol:msft 2014-03-21 22:26:23.661 Chapter18-Challenge1-Stocks[7761:303] ---------------- 2014-03-21 22:26:23.662 Chapter18-Challenge1-Stocks[7761:303] Purchase Price/Share: 30.36 2014-03-21 22:26:23.662 Chapter18-Challenge1-Stocks[7761:303] Current Price/Share: 40.16 2014-03-21 22:26:23.663 Chapter18-Challenge1-Stocks[7761:303] Number of Shares: 34 2014-03-21 22:26:23.664 Chapter18-Challenge1-Stocks[7761:303] Total Cost in USD: 1032.24 2014-03-21 22:26:23.664 Chapter18-Challenge1-Stocks[7761:303] Total Value in USD: 1365.44 2014-03-21 22:26:23.665 Chapter18-Challenge1-Stocks[7761:303] Profit in USD: 333.20 2014-03-21 22:26:23.665 Chapter18-Challenge1-Stocks[7761:303] 2014-03-21 22:26:23.666 Chapter18-Challenge1-Stocks[7761:303] Stock symbol:goog 2014-03-21 22:26:23.666 Chapter18-Challenge1-Stocks[7761:303] ---------------- 2014-03-21 22:26:23.667 Chapter18-Challenge1-Stocks[7761:303] Purchase Price/Share: 596.42 2014-03-21 22:26:23.667 Chapter18-Challenge1-Stocks[7761:303] Current Price/Share: 1183.04 2014-03-21 22:26:23.668 Chapter18-Challenge1-Stocks[7761:303] Number of Shares: 2 2014-03-21 22:26:23.668 Chapter18-Challenge1-Stocks[7761:303] Total Cost in USD: 1192.84 2014-03-21 22:26:23.669 Chapter18-Challenge1-Stocks[7761:303] Total Value in USD: 2366.08 2014-03-21 22:26:23.669 Chapter18-Challenge1-Stocks[7761:303] Profit in USD: 1173.24 2014-03-21 22:26:23.670 Chapter18-Challenge1-Stocks[7761:303]

I’ve managed to name the company info by using the @property value in the .h file (below the instance variables):

[code]@interface BNRStockHoldings : NSObject

{
// BNRStockHoldings has three instance variables

float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;

}
@property (copy) NSString *nameOfCompany;

[/code]
Then set the name when you set the other values:

// Setup stock3
        [stock3 setNameOfCompany:@"aapl"];
        [stock3 setPurchaseSharePrice:45.10];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberOfShares:210];

Then setting the company name to a variable as part of the loop:

[code]for (BNRStockHoldings *a in anArray) {
float cid = [a costInDollars];
float vid = [a valueInDollars];
NSString *cmpny = [a nameOfCompany];

        NSLog(@"\n\n%@\n----------------\nThe Cost in Dollars is: %.2f\nThe Value in Dollars is: %.2f\n\n", cmpny, cid, vid);

[/code]

This prints out like so:

aapl

The Cost in Dollars is: 9471.00
The Value in Dollars is: 10397.10

Hope this helps.

kernelG

Hey I’ll definitely post my files but just a couple of tips. I noticed from the output of your program that you’re using a different NSLog for each instance variable. Consider either overriding description or just using multiple variables in one NSLog statement. Also, I would definitely recommend using properties for all of this. I’ve completed this book and moved on to a different one so I can’t recall but I think you guys should be into properties by this chapter. They will make things a lot easier for you. Hope this helps.

This is my BNRStockHolding.h file. For the nameOfCompany property, I used the type id rather than a string.

[code]#import <Foundation/Foundation.h>
@class BNRPortfolio;

@interface BNRStockHolding : NSObject
// settign properties
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic) id nameOfCompany;

//instance methods to perform calculations

  • (float)costInDollars; //purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares

@end[/code]

And here is my BNRStockHolding.m file

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)costInDollars
    {
    return _purchaseSharePrice * _numberOfShares;
    }

  • (float)valueInDollars
    {
    return _numberOfShares * _currentSharePrice;
    }

//remove the comment bars to override description
//- (NSString *)description
//{
// return [NSString stringWithFormat:@“Stock %@ is worth $%.2f”, self.nameOfCompany, self.valueInDollars];
//}
@end[/code]

It’s been quite some time since I visited this program, I have my description override commented out but you can remove the comment bars and change it so that the description fits what you want. Then in main.m you could log something by using:

NSLog(@"%@", [someStock description]);

Then your output would look like:

Stock someStock is worth $xxx.xx

Hope all this helps.