SOLUTION Challenge STOCKS


#1

BNRStockHolding.h

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

@interface BNRStockHolding : NSObject

//Instances variables
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;

}

//Getters and Setters

-(float)purchaseSharePrice;
-(void)setPurchaseSharePrice:(float)purchaseSharePrice;

-(float)currentSharePrice;
-(void)setCurrentSharePrice:(float)currentSharePrice;

-(int)numberOfShares;
-(void)setNumberOfShares:(int)numberOfShares;

//Instance Methods

-(float)costInDollars; //PurchaseSharePrice * numberOfShares
-(float)valueInDollars; //currentSharePrice * numberOfShares

@end
[/quote]

BNRStockHolding.m

[quote]#import “BNRStockHolding.h”

@implementation BNRStockHolding

//Purchase Share Price Implementation
-(float)purchaseSharePrice{
return _purchaseSharePrice;
}

-(void)setPurchaseSharePrice:(float)purchaseSharePrice {
_purchaseSharePrice = purchaseSharePrice;
}

//Current Share Price Implementation

-(float) currentSharePrice {
return _currentSharePrice;
}

-(void)setCurrentSharePrice: (float)currentSharePrice {
_currentSharePrice = currentSharePrice;
}

//Number of Shares Implementation

-(int) numberOfShares {
return _numberOfShares;
}

-(void)setNumberOfShares: (int) numberOfShares {
_numberOfShares = numberOfShares;
}

//Cost and value in dollars Methods Implementation

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

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

@end
[/quote]

main.m

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

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

@autoreleasepool {
    
  //Creating Objects 
    
    //Stocks of Felix
    BNRStockHolding *felixStocks = [[BNRStockHolding alloc]init];
    
    //Setting values
    [felixStocks setPurchaseSharePrice:2.30];
    [felixStocks setCurrentSharePrice:4.50];
    [felixStocks setNumberOfShares:40];
   
    
    
    //Stocks of Mirka
    BNRStockHolding *mirkaStocks = [[BNRStockHolding alloc]init];

    //Setting values
    [mirkaStocks setPurchaseSharePrice:12.19];
    [mirkaStocks setCurrentSharePrice:10.56];
    [mirkaStocks setNumberOfShares:90];
    
    //Stocks of Kimberly
    BNRStockHolding *kimberlyStocks = [[BNRStockHolding alloc]init];
    
    //Setting values
    [kimberlyStocks setPurchaseSharePrice:45.10];
    [kimberlyStocks setCurrentSharePrice:49.51];
    [kimberlyStocks setNumberOfShares:210];
    
    //Creating the Array
    NSMutableArray *stockArray = [[NSMutableArray alloc]init];
    
    
    //Adding objects to the array
    [stockArray addObject:felixStocks];
    [stockArray addObject:mirkaStocks];
    [stockArray addObject:kimberlyStocks];
    
    
    //Array counter
    NSUInteger objectCount = [stockArray count];
    
    
    //for Loop
    
    for (int i = 0; i < objectCount; i++) {
        BNRStockHolding *valueOfStocks = [stockArray objectAtIndex:i];
                
        
    //Output for Felix
        
    if (i == 0) {
                    
        NSLog(@" The value in dollars of the stocks of Felix is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
    }
    
    
        //Output for Mirka
                
    if (i == 1) {
                    
     NSLog(@" The value in dollars of the stocks of Mirka is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
    }
        
      //Output for Kimberly
        
    if (i == 2) {
            
     NSLog(@" The value in dollars of the stocks of Kimberly is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
    }
        
    }
    
}
return 0;

}

[/quote]


#2

Very similar to what I did except I used a switch statement to in oder to list the stock descriptor in the log.

for (int i = 0; i < counter; i++) {
BNRStockHolding *s = stockArray[i];
switch (i) {
case 0:
NSLog(@“The first stock has the following values:”);
break;

            case 1:
                NSLog(@"The second stock has the following values:");
                break;
                
            case 2:
                NSLog(@"The third stock has the following values:");
                break;
                
            default:
                break;
        }

#3

The downside to your approach is that you are depending on the stocks to be in a specific order. You would have been better off to have made an instance variable that holds and identifier for each of the stocks, and then just logged the identifier in the for loop. Also, you could have used fast enumeration in your for loop.