My Solution to Challenge - addYourselfToArray class method


#1

Here is my solution to the Chapter 18 challenge, see below in subsequent posts BNRStockHolding.h, BNRStockHolding.m and main.m. The program gives the following output for each share in the portfolio:-

'For company Big Bucks Corporation, you own 100 shares, which were purchased at a price per share of $1.95 for a total purchase cost of $195.00, today the shares have a price of $2.10 giving a total market value of $210.00, meaning you have a potential paper profit of $15.00"

Output message also adjusted if stock is showing a loss.

Appreciate any feedback on improvements I could make.


#2

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

@interface BNRStockHolding : NSObject

// create the instance variables

{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
NSString *_nameOfShare;
}

// create the setter and getter methods

  • (float)purchaseSharePrice;
  • (void)setPurchaseSharePrice:(float)p;
  • (float)currentSharePrice;
  • (void)setCurrentSharePrice:(float)c;
  • (int)numberOfShares;
  • (void)setNumberOfShares:(int)q;
  • (NSString *)nameOfShare;
  • (void)setNameOfShare:(NSString *)n;

// create the BNRStockHolding methods

  • (float)costInDollars;
  • (float)valueInDollars;
  • (void)addYourselfToArray:(NSMutableArray *)array; // method to add instance of BNRStockHolding to mutuable array

@end
[/code]


#3

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)purchaseSharePrice
    {
    return _purchaseSharePrice;
    }

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

  • (float)currentSharePrice
    {
    return _currentSharePrice;
    }

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

  • (int)numberOfShares
    {
    return _numberOfShares;
    }

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

  • (NSString *)nameOfShare
    {
    return _nameOfShare;
    }

  • (void)setNameOfShare:(NSString *)n
    {
    _nameOfShare = n;
    }

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

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

-(void)addYourselfToArray:(NSMutableArray *)array
{
[array addObject:self];
}

@end
[/code]


#4

[code]#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
   
    // create the NSMutableArray to hold stock portfolio
    
    NSMutableArray *stockPortfolio =[NSMutableArray array];
    
    // create 3 instances of BNRStockHolding
    
    BNRStockHolding *stocka =[[BNRStockHolding alloc]init];
    BNRStockHolding *stockb =[[BNRStockHolding alloc]init];
    BNRStockHolding *stockc =[[BNRStockHolding alloc]init];
    
    // set values of instance variables using setters
    
    [stocka setPurchaseSharePrice:1.95];
    [stocka setNumberOfShares:100];
    [stocka setCurrentSharePrice:2.10];
    [stocka setNameOfShare:@"Big Bucks Corporation"];
    [stocka addYourselfToArray:(stockPortfolio)];
    
    [stockb setPurchaseSharePrice:2.95];
    [stockb setNumberOfShares:150];
    [stockb setCurrentSharePrice:3.10];
    [stockb setNameOfShare:@"Mega Bucks Corporation"];
    [stockb addYourselfToArray:(stockPortfolio)];

    [stockc setPurchaseSharePrice:0.95];
    [stockc setNumberOfShares:50];
    [stockc setCurrentSharePrice:0.75];
    [stockc setNameOfShare:@"Small Bucks Corporation"];
    [stockc addYourselfToArray:(stockPortfolio)];
    
    // For loop to iterate through stockPortfolio array
    
    for (BNRStockHolding *i in stockPortfolio){
    
        NSString *name = [i nameOfShare];
        int shares = [i numberOfShares];
        float purchasePrice =[i purchaseSharePrice];
        float totalCost = [i costInDollars];
        float currentPrice = [i currentSharePrice];
        float marketValue = [i valueInDollars];
        float profit = marketValue - totalCost;
        
        // determines if a profit or loss has been made and changes output accordingly.
        
        if (profit < 0) {
            NSLog(@"For company %@, you own %d shares, which were purchased at a price per share of $%.2f for a total purchase cost of $%.2f, today the shares have a price of $%.2f giving a total market value of $%.2f, meaning you have a potential paper loss of $%.2f", name, shares,purchasePrice, totalCost,currentPrice,marketValue, profit);
        } else {
            NSLog(@"For company %@, you own %d shares, which were purchased at a price per share of $%.2f for a total purchase cost of $%.2f, today the shares have a price of $%.2f giving a total market value of $%.2f, meaning you have a potential paper profit of $%.2f", name, shares,purchasePrice, totalCost,currentPrice,marketValue, profit);
        }
        
    
    
    }

    
}
return 0;

}
[/code]